diff --git a/.jules/bolt.md b/.jules/bolt.md index 7edb3f3b..db1453e8 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -25,3 +25,11 @@ SPDX-License-Identifier: MIT OR Apache-2.0 ## 2025-04-12 - Walrus Operator Optimization **Learning:** Using the walrus operator inside a list comprehension to avoid redundant execution of string methods (like `.strip()`) is an effective and safe micro-optimization. The result of the assignment inside the list comprehension will intentionally leak into the scope of the caller function, but this standard Python behavior does not cause naming conflicts in non-recursive or non-global scopes. **Action:** Always favor using the walrus operator `:=` in list comprehensions or conditionals when identical string manipulations (e.g., `.strip()`) or expensive evaluation calls appear repeatedly within the identical expression branch. + +## 2026-04-18 - Replacing Generators with Standard Loops for Linear Lookups +**Learning:** In Python performance optimization, replacing a generator expression wrapped in `next()` (e.g., `next((x for x in iterable if condition), default)`) with a standard `for` loop that uses an early `return` can significantly speed up linear lookups by eliminating generator frame allocation overhead. +**Action:** When performing linear lookups or finding the first matching element in a collection on hot paths, prioritize using a standard `for` loop instead of `next(...)` with a generator expression. + +## 2026-04-18 - Short-circuiting and Eliminating Generator Overhead in String Matching +**Learning:** When evaluating sequential string match conditions in hot loops (e.g., matching a start pattern, then an end pattern), using early returns to short-circuit the function if the first condition fails prevents unnecessary computations. Additionally, converting generator comprehensions (e.g., `target in (s.lower() for s in collection)`) into `any()` statements combined with pre-computed invariant variables (like `target.lower()`) eliminates the object overhead of generators and executes much faster. +**Action:** Use early returns to short-circuit logic, and replace generator comprehensions with `any()` checks and pre-computed loop invariants when validating complex conditions sequentially. diff --git a/src/codeweaver/engine/chunker/delimiters/patterns.py b/src/codeweaver/engine/chunker/delimiters/patterns.py index 34f4798f..c46da7bc 100644 --- a/src/codeweaver/engine/chunker/delimiters/patterns.py +++ b/src/codeweaver/engine/chunker/delimiters/patterns.py @@ -100,13 +100,18 @@ def matches_pattern(start: str, end: str, pattern: DelimiterPattern) -> bool: >>> matches_pattern("class", ":", pattern) False """ - # Case-insensitive start matching - start_match = start.lower() in (s.lower() for s in pattern.starts) + # Performance Optimization: Cache lowercased start/end values and replace generator expressions + # with inline `any()` equality checks and early returns to eliminate generator allocation overhead. + start_lower = start.lower() + start_match = any(start_lower == s.lower() for s in pattern.starts) + if not start_match: + return False - # Handle "ANY" end wildcard or specific end matching - end_match = True if pattern.ends == "ANY" else end.lower() in (e.lower() for e in pattern.ends) + if pattern.ends == "ANY": + return True - return start_match and end_match + end_lower = end.lower() + return any(end_lower == e.lower() for e in pattern.ends) # Core patterns extracted from inference methods @@ -648,14 +653,13 @@ def kind_from_delimiter_tuple( start, end = delimiter if start is None or end is None: raise ValueError("Both start and end must be provided") - return next( - ( - pattern.kind - for pattern in ALL_PATTERNS - if (start, end) == (pattern.starts, pattern.ends) - ), - DelimiterKind.UNKNOWN, - ) + + # Performance Optimization: Iterate using a standard `for` loop instead of a + # generator expression with `next()` to eliminate generator allocation overhead. + for pattern in ALL_PATTERNS: + if pattern.starts == start and pattern.ends == end: + return pattern.kind + return DelimiterKind.UNKNOWN __all__ = (