Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
## 2024-05-28 - [Python dict get with default vs `in` check for list iterations]
**Learning:** When parsing large JSON index files containing lists of dictionaries (like APK package indexes), iterating over a list field by calling `package.get("tags", [])` forces the creation of a new empty list on every miss. A simple `if "tags" in package:` check avoids this allocation entirely and is measurably faster. Additionally, using string slicing (`tag[19:]`) instead of `tag.split("=")[-1]` for a known prefix is considerably faster.
**Action:** In high-volume parsing loops over dictionaries (e.g., thousands of packages), avoid using `.get(key, [])` if the key is frequently missing. Use an explicit `if key in dict:` check instead. For extracting values past known string prefixes, use slicing (e.g., `s[len(prefix):]`) instead of `.split()`.
## 2024-04-02 - [Make OPKG Parsing 2x Faster]
**Learning:** In Python, string `.find()` followed by string slicing is significantly faster and more memory-efficient than calling `.split("\n")` within loops because it avoids repeated creation and teardown of list structures. Finding `\n` anchors correctly bypasses `.startswith` issues on inner chunks.
**Action:** When extracting multiple text fields from a large multi-line string block, avoid `.splitlines()` or `.split("\n")` per block. Instead, use `str.find()` with slice indexing `str[start:end]` to extract targets in-place.
52 changes: 36 additions & 16 deletions scripts/make-index-json.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,42 @@ def parse_opkg(text: str) -> dict:
# as it avoids the overhead of full RFC 822/2822 compliance checks.
chunks: list[str] = text.strip().split("\n\n")
for chunk in chunks:
package_name = ""
package_version = ""
package_abi = ""

for line in chunk.split("\n"):
if line.startswith("Package: "):
package_name = line[9:].strip()
elif line.startswith("Version: "):
package_version = line[9:].strip()
elif line.startswith("ABIVersion: "):
package_abi = line[12:].strip()

if package_name:
if package_abi:
package_name = removesuffix(package_name, package_abi)
packages[package_name] = package_version
pkg_idx = chunk.find("\nPackage: ")
if pkg_idx == -1:
if chunk.startswith("Package: "):
pkg_idx = 0
else:
continue
else:
pkg_idx += 1

end_idx = chunk.find("\n", pkg_idx)
package_name = chunk[pkg_idx+9:end_idx if end_idx != -1 else len(chunk)].strip()

ver_idx = chunk.find("\nVersion: ")
if ver_idx == -1:
if chunk.startswith("Version: "):
ver_idx = 0

if ver_idx != -1:
ver_idx += 1 if chunk[ver_idx] == '\n' else 0
end_idx = chunk.find("\n", ver_idx)
package_version = chunk[ver_idx+9:end_idx if end_idx != -1 else len(chunk)].strip()
else:
package_version = ""

abi_idx = chunk.find("\nABIVersion: ")
if abi_idx == -1:
if chunk.startswith("ABIVersion: "):
abi_idx = 0

if abi_idx != -1:
abi_idx += 1 if chunk[abi_idx] == '\n' else 0
end_idx = chunk.find("\n", abi_idx)
package_abi = chunk[abi_idx+12:end_idx if end_idx != -1 else len(chunk)].strip()
package_name = removesuffix(package_name, package_abi)

packages[package_name] = package_version

return packages

Expand Down
Loading