From f34288812cfb7322f3775c9c3d6f26f5e87f67d8 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 13 Jun 2026 12:38:46 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Avoid=20redundant=20getenv?= =?UTF-8?q?=20calls=20and=20string=20formats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Store the results of `getenv()` and formatted strings into local variables instead of calling them multiple times for the same values. Repeatedly formatting strings and accessing `os.getenv` adds unnecessary function call and string allocation overheads. Caching the result in a local variable is faster and more readable. Signed-off-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: manupawickramasinghe <73810867+manupawickramasinghe@users.noreply.github.com> --- .jules/bolt.md | 8 ++++++++ scripts/json_add_image_info.py | 27 ++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 376c1c98d11cb0..56de9683c349dc 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -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. diff --git a/scripts/json_add_image_info.py b/scripts/json_add_image_info.py index c5cdfc30c51347..d67bdfdba0223c 100755 --- a/scripts/json_add_image_info.py +++ b/scripts/json_add_image_info.py @@ -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) @@ -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=(",", ":")))