Skip to content

⚡ Bolt: Optimize linear lookups in file extension checks#354

Open
bashandbone wants to merge 1 commit into
mainfrom
bolt/optimize-extension-lookup-7415917703769671111
Open

⚡ Bolt: Optimize linear lookups in file extension checks#354
bashandbone wants to merge 1 commit into
mainfrom
bolt/optimize-extension-lookup-7415917703769671111

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented May 19, 2026

💡 What: Optimized the is_doc and is_data property getters in src/codeweaver/core/metadata.py by replacing next((...)) generator expressions with standard for loops that use early returns. Suppressed the ruff rule SIM110 with # noqa to ensure the manual loop is preserved over an any() generator.

🎯 Why: In CPython, evaluating generator comprehensions inside functions like next() or any() involves allocating a generator object and evaluating its frames. When performing a simple linear search to find a matching extension, manually writing out a for loop that returns early avoids this overhead completely. These properties are accessed frequently during file discovery, making this a small but compounding speedup.

📊 Impact: Reduces evaluation overhead in file metadata extension lookups by entirely bypassing generator frame allocations, yielding faster linear lookups.

🔬 Measurement: The optimization can be verified by profiling calls to FileMetadata.is_doc and FileMetadata.is_data over a large list of files compared to their previous implementations, seeing a drop in generator allocation overhead.


PR created automatically by Jules for task 7415917703769671111 started by @bashandbone

Summary by Sourcery

Optimize file metadata extension classification checks and document the performance guideline in the optimization playbook.

Enhancements:

  • Improve performance of documentation and data file extension checks by replacing generator-based lookups with explicit loops and early returns.

Documentation:

  • Extend the Bolt optimization guide with a new rule recommending explicit for-loops over generator expressions in hot-path linear lookups.

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>
Copilot AI review requested due to automatic review settings May 19, 2026 12:48
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Optimizes FileMetadata extension classification by replacing generator-based lookups with explicit for-loop early returns in hot paths, and documents this pattern in the Bolt optimization guide.

Flow diagram for optimized FileMetadata extension lookups (is_doc / is_data)

flowchart TD
    A[Start is_doc / is_data] --> B[Import DOC_FILES_EXTENSIONS or DATA_FILES_EXTENSIONS]
    B --> C[for ext in DOC_FILES_EXTENSIONS / DATA_FILES_EXTENSIONS]
    C --> D{ext.ext == self.ext}
    D -- Yes --> E[return True]
    D -- No --> F{More extensions?}
    F -- Yes --> C
    F -- No --> G[return False]
    E --> H[End]
    G --> H[End]
Loading

File-Level Changes

Change Details Files
Optimize FileMetadata.is_doc linear extension lookup to avoid generator allocation overhead.
  • Replaced next()-wrapped generator expression with an explicit for loop and early return when checking documentation file extensions.
  • Added a performance-focused comment explaining the rationale for avoiding generator frame allocation in this hot path.
  • Suppressed ruff SIM110 for the loop to prevent automatic refactoring back to a comprehension or any().
src/codeweaver/core/metadata.py
Optimize FileMetadata.is_data linear extension lookup in the same way as is_doc for consistency and performance.
  • Replaced next()-wrapped generator expression with an explicit for loop and early return when checking data file extensions.
  • Added a performance comment mirroring is_doc to justify the manual loop.
  • Suppressed ruff SIM110 for the loop to preserve the optimization.
src/codeweaver/core/metadata.py
Document the generator-to-for-loop optimization pattern in the Bolt micro-optimizations guide.
  • Added a new dated entry describing why replacing next()-wrapped generator expressions with for loops can speed up linear lookups.
  • Recorded an action item recommending this pattern in hot paths and noting the need to suppress SIM110 where applicable.
.jules/bolt.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link
Copy Markdown
Contributor

🤖 Hi @bashandbone, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Given that DOC_FILES_EXTENSIONS and DATA_FILES_EXTENSIONS appear to be static collections, consider using a precomputed set or mapping of ext values for O(1) membership checks instead of repeated linear scans, which will likely outperform the micro-optimization from removing the generator.
  • The is_doc and is_data implementations are now almost identical; consider extracting a shared helper (e.g., _has_ext(self.ext, EXT_COLLECTION)) to avoid duplication and keep future changes to the lookup logic in one place.
  • If you keep the # noqa: SIM110 suppression, it may be worth tightening the comment to explicitly reference this being a measured hot path (or add a short note about the specific context) so future readers understand why the usual readability preference is intentionally bypassed here.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Given that `DOC_FILES_EXTENSIONS` and `DATA_FILES_EXTENSIONS` appear to be static collections, consider using a precomputed set or mapping of `ext` values for O(1) membership checks instead of repeated linear scans, which will likely outperform the micro-optimization from removing the generator.
- The `is_doc` and `is_data` implementations are now almost identical; consider extracting a shared helper (e.g., `_has_ext(self.ext, EXT_COLLECTION)`) to avoid duplication and keep future changes to the lookup logic in one place.
- If you keep the `# noqa: SIM110` suppression, it may be worth tightening the comment to explicitly reference this being a measured hot path (or add a short note about the specific context) so future readers understand why the usual readability preference is intentionally bypassed here.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@github-actions
Copy link
Copy Markdown
Contributor

🤖 I'm sorry @bashandbone, but I was unable to process your request. Please see the logs for more details.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes frequent file-extension category checks in ExtLangPair by replacing next((...)) generator expressions with explicit for loops and early returns, reducing generator allocation overhead during linear lookups.

Changes:

  • Rewrote ExtLangPair.is_doc and ExtLangPair.is_data to use explicit loops with early returns (and added # noqa: SIM110 to preserve this pattern).
  • Added a Bolt learning entry documenting the “for-loop over generator/next” optimization pattern for hot paths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/codeweaver/core/metadata.py Replaces generator/next()-based linear lookups with early-return loops for is_doc/is_data.
.jules/bolt.md Documents the optimization rationale/pattern for future reference.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants