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
8 changes: 8 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@
## 2025-05-23 - [Optimize SBOM parsing speed]
**Learning:** When parsing tens of thousands of machine-generated RFC 822-style blocks (like opkg status files), avoid using `str.splitlines()` on block slices and avoid generic dictionary allocations for all fields. Instead, use fast, localized string searches (e.g., `str.find('\nPackage: ', start, end)`) to extract only the specific required fields directly. This drastically reduces intermediate object allocations and execution time compared to full-block dictionary parsing. OPKG index and status fields have strictly fixed, standardized casing (e.g., 'Package:', 'Version:'). When applying codebase-established optimizations like `str.find()` for targeted field extraction, concerns regarding case-sensitivity regressions (e.g., handling 'package: ' vs 'Package: ') are invalid for this domain format.
**Action:** Replace `splitlines()` and generic dict parsing with `str.find()` loops when extracting specific fields from large text blocks like opkg indexes.

## 2024-06-07 - [Python `shutil.copyfileobj` optimization]
**Learning:** In CPython, `shutil.copyfileobj()` is implemented in pure Python and executes an almost identical `while True: read()/write()` loop internally. Unlike `shutil.copyfile` (which may use OS-level fast copy), replacing a manual chunked file copy loop with `copyfileobj` on file-like streams provides zero measurable performance improvement and should not be used as a micro-optimization.
**Action:** Do not replace manual `while True:` chunk-reading and writing loops with `shutil.copyfileobj()` for performance reasons, as it provides no measurable improvement. Focus on reducing I/O, reducing memory allocation, or moving loops to native C code instead.

## 2024-06-07 - [Optimize redundant lookups]
**Learning:** Repeatedly formatting strings and accessing `os.getenv` adds unnecessary function call and string allocation overheads.
**Action:** Store the results of `getenv()` and formatted strings into local variables instead of calling them multiple times for the same values.
27 changes: 16 additions & 11 deletions scripts/json_add_image_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def get_titles():
for prefix in ["", "ALT0_", "ALT1_", "ALT2_", "ALT3_", "ALT4_", "ALT5_"]:
title = {}
for var in ["vendor", "model", "variant"]:
if getenv("DEVICE_{}{}".format(prefix, var.upper())):
title[var] = getenv("DEVICE_{}{}".format(prefix, var.upper()))
# Optimization: Avoid redundant getenv calls and string formats by storing
# the result in a local variable. This saves function call overhead.
env_val = getenv("DEVICE_{}{}".format(prefix, var.upper()))
if env_val:
title[var] = env_val

if title:
titles.append(title)
Expand Down Expand Up @@ -95,20 +98,22 @@ def get_numerical_size(image_size):
},
}

if getenv("IMAGE_SIZE") or getenv("KERNEL_SIZE"):
image_size = getenv("IMAGE_SIZE")
kernel_size = getenv("KERNEL_SIZE")
if image_size or kernel_size:
file_info["profiles"][device_id]["file_size_limits"] = {}
if getenv("IMAGE_SIZE"):
if image_size:
# Optimization: Avoid redundant getenv calls by using local variable.
file_info["profiles"][device_id]["file_size_limits"]["image"] = get_numerical_size(
getenv("IMAGE_SIZE")
image_size
)
if getenv("KERNEL_SIZE"):
if kernel_size:
file_info["profiles"][device_id]["file_size_limits"]["kernel"] = get_numerical_size(
getenv("KERNEL_SIZE")
kernel_size
)

if getenv("FILE_FILESYSTEM"):
file_info["profiles"][device_id]["images"][0]["filesystem"] = getenv(
"FILE_FILESYSTEM"
)
file_filesystem = getenv("FILE_FILESYSTEM")
if file_filesystem:
file_info["profiles"][device_id]["images"][0]["filesystem"] = file_filesystem

json_path.write_text(json.dumps(file_info, separators=(",", ":")))