From 7940a08267595763d3254315cbdebee29f50fafb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 19 May 2026 12:47:56 +0000 Subject: [PATCH] perf: optimize linear lookups in file extension checks Replaced generator expressions inside `next()` with standard `for` loops and early returns for the `is_doc` and `is_data` properties in `src/codeweaver/core/metadata.py`. This eliminates generator frame allocation overhead and speeds up linear file extension checks, adding a measurable performance improvement for frequent property access. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com> --- .jules/bolt.md | 3 +++ src/codeweaver/core/metadata.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 7edb3f3bf..b0c0ad41a 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -25,3 +25,6 @@ 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-14 - Replace Generator Expressions with For Loops in 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:** Replace `next((...))` generator patterns with explicit `for` loops and early returns in hot paths or linear lookups. Suppress linting rules like `SIM110` using `noqa` when necessary to maintain this optimization. diff --git a/src/codeweaver/core/metadata.py b/src/codeweaver/core/metadata.py index 6428b352b..753390cab 100644 --- a/src/codeweaver/core/metadata.py +++ b/src/codeweaver/core/metadata.py @@ -247,7 +247,12 @@ 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) + # Performance: Replace generator expression with explicit loop and early return + # to eliminate generator frame allocation overhead during linear lookup. + 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 +264,12 @@ 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) + # Performance: Replace generator expression with explicit loop and early return + # to eliminate generator frame allocation overhead during linear lookup. + 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: