diff --git a/.jules/bolt.md b/.jules/bolt.md index 7edb3f3b..81e0e6b4 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -25,3 +25,7 @@ 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-05-30 - Generator Expression Allocation Overhead in Hot Loops +**Learning:** Evaluating generator expressions using `next((True for x in col if cond), False)` inside hot paths is slower than an equivalent standard `for` loop with an early `return`. Creating generator object instances repeatedly inside loops carries non-trivial overhead relative to the logic, slowing operations roughly ~20-30%. +**Action:** When evaluating simple lookups across collections sequentially, write explicit standard `for` loops combined with early `return` checks and apply `# noqa: SIM110` to avoid automated ruff simplification, rather than combining `next()` with a generator expression. diff --git a/src/codeweaver/core/language.py b/src/codeweaver/core/language.py index 1907510e..1f175b10 100644 --- a/src/codeweaver/core/language.py +++ b/src/codeweaver/core/language.py @@ -129,7 +129,10 @@ def from_extension(cls, ext: str) -> ConfigLanguage | None: """ ext = ext.lower() if ext.startswith(".") else ext if ext in cls.all_extensions(): - return next((language for language in cls if ext in language.extensions), None) + # Optimization: A standard for loop with an early return avoids generator frame allocation overhead + for language in cls: + if ext in language.extensions: + return language return None @property diff --git a/src/codeweaver/core/metadata.py b/src/codeweaver/core/metadata.py index 6428b352..669e153e 100644 --- a/src/codeweaver/core/metadata.py +++ b/src/codeweaver/core/metadata.py @@ -247,7 +247,11 @@ def is_doc(self) -> bool: """Check if the extension is a documentation file.""" from codeweaver.core.file_extensions import DOC_FILES_EXTENSIONS - return next((True for doc_ext in DOC_FILES_EXTENSIONS if doc_ext.ext == self.ext), False) + # Optimization: A standard for loop with an early return avoids generator frame allocation overhead + for doc_ext in DOC_FILES_EXTENSIONS: # noqa: SIM110 + if doc_ext.ext == self.ext: + return True + return False @property def is_code(self) -> bool: @@ -259,7 +263,11 @@ def is_data(self) -> bool: """Check if the extension is a data file.""" from codeweaver.core.file_extensions import DATA_FILES_EXTENSIONS - return next((True for data_ext in DATA_FILES_EXTENSIONS if data_ext.ext == self.ext), False) + # Optimization: A standard for loop with an early return avoids generator frame allocation overhead + for data_ext in DATA_FILES_EXTENSIONS: # noqa: SIM110 + if data_ext.ext == self.ext: + return True + return False @property def as_source(self) -> ChunkSource: diff --git a/src/codeweaver/providers/types/vectors.py b/src/codeweaver/providers/types/vectors.py index 46d5f0e1..fd795e25 100644 --- a/src/codeweaver/providers/types/vectors.py +++ b/src/codeweaver/providers/types/vectors.py @@ -315,7 +315,11 @@ def by_name(self, name: str) -> VectorConfig | None: Returns: Matching VectorConfig or None """ - return next((v for v in self.vectors.values() if v.name == name), None) + # Optimization: A standard for loop with an early return avoids generator frame allocation overhead + for v in self.vectors.values(): + if v.name == name: + return v + return None def by_key(self, key: str) -> VectorConfig | None: """Get vector by its logical dict key.