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