From 6272bfc0cb09d20104e4b616b51c015f63c0d4e6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 02:40:47 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Inline=20block=20string=20c?= =?UTF-8?q?hecks=20to=20avoid=20allocations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced function calls and `line.strip() == ""` in `split_blocks` with inlined `line.isspace() or not line` to avoid string allocations and function overhead in the hot `splitlines` loop. Co-authored-by: coredipper <1830173+coredipper@users.noreply.github.com> --- .jules/bolt.md | 8 ++++++++ scrip/src/scrip/blocks.py | 12 ++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 0752361..ebb8e9d 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -31,3 +31,11 @@ ## 2024-05-24 - Streaming NDJSON parsing avoids O(N) memory allocation **Learning:** Loading large NDJSON files entirely into memory using `.read_text()` before splitting into lines causes significant memory overhead. **Action:** Always process large text files line-by-line using `with open(...) as f: for line in f:` to maintain O(1) memory usage. + +## 2024-11-21 - Python Function Call Overhead in Hot Loops +**Learning:** In CPython, invoking functions (even simple ones like `_is_blank` or `_is_heading`) adds measurable overhead in hot loops parsing large texts line-by-line (e.g., inside `for line in splitlines()`). By inlining the conditional logic (`line.isspace() or not line` and `line.lstrip().startswith("#")`) directly into the loop, execution time improved significantly without drastically sacrificing code readability. +**Action:** In a hot loop processing strings (e.g., iterating lines with `splitlines()`), inlining straightforward string checks (like `.isspace()` or simple indexing) instead of invoking helper functions avoids Python function call overhead and provides significant, measurable speedups. + +## 2024-11-21 - Memory overhead of `strip() == ""` +**Learning:** In CPython, checking for a blank line using `line.strip() == ""` forces the interpreter to allocate a brand new string object in memory on every iteration. On loops scaling to thousands or millions of lines, this introduces massive, unnecessary garbage collection overhead and slows down the loop. +**Action:** In CPython, checking for a blank line using `line.strip() == ""` allocates a new string object in memory. In hot loops, replace this with `line.isspace() or not line` to avoid the allocation overhead and execute significantly faster. diff --git a/scrip/src/scrip/blocks.py b/scrip/src/scrip/blocks.py index 63ea5cc..504c73a 100644 --- a/scrip/src/scrip/blocks.py +++ b/scrip/src/scrip/blocks.py @@ -25,14 +25,6 @@ from . import hashing -def _is_heading(line: str) -> bool: - return line.lstrip().startswith("#") - - -def _is_blank(line: str) -> bool: - return line.strip() == "" - - def split_blocks(text: str) -> list[dict]: """Return a list of ``{"block_id", "span": [start, end], "hash"}``.""" # Use splitlines(keepends=True): its boundary set (form feed, NEL, U+2028/9, @@ -59,11 +51,11 @@ def emit(s: int, e: int) -> None: for line in text.splitlines(keepends=True): end = start + len(line) - if _is_blank(line): + if line.isspace() or not line: if cur_start is not None: emit(cur_start, cur_end) cur_start = None - elif _is_heading(line): + elif line.lstrip().startswith("#"): if cur_start is not None: emit(cur_start, cur_end) cur_start = None