Skip to content

⚡ Bolt: Use pre-computed tuples for static iterations#346

Open
bashandbone wants to merge 1 commit into
mainfrom
bolt-optimize-static-iteration-overhead-2745804533802310033
Open

⚡ Bolt: Use pre-computed tuples for static iterations#346
bashandbone wants to merge 1 commit into
mainfrom
bolt-optimize-static-iteration-overhead-2745804533802310033

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented May 7, 2026

💡 What: Pre-computed the self._classification_methods tuple of classification methods within the __init__ constructor instead of creating an inline list structure every time the classify loop was executed.
🎯 Why: When a static collection of instance methods needs to be repeatedly iterated over, constructing the collection over and over incurs double overhead: the allocation of the list memory and the creation of the bound instance method objects themselves.
📊 Impact: Reduces object creation time and decreases list allocation processing within the AST-based Node classification hotloop, improving raw iteration evaluation speeds.
🔬 Measurement: Verified using targeted tests (mise //:test tests/unit/engine/chunker/) to ensure no regressions during classifications.


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

Summary by Sourcery

Pre-compute and reuse the classifier’s bound method collection to reduce allocation overhead in the classification hot loop and document this optimization pattern in the Bolt guidelines.

Enhancements:

  • Pre-compute the tuple of classification methods in the classifier initializer and reuse it during thing classification to avoid per-call list construction and bound method creation.

Documentation:

  • Add a Bolt guideline documenting the performance benefits of pre-computing bound method collections for hot loops.

…n in hot loop

Constructing a collection of bound methods inside `_classify_known_exceptions` incurred overhead from both list allocation and repeated bound method creation. This moves the bound methods out into the `__init__` constructor as a pre-computed tuple to enhance iteration performance.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 7, 2026 12:50
@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 7, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Precomputes and stores the classifier’s bound classification methods as a tuple during initialization and then reuses it in the hot classification loop, plus documents this performance pattern in the Bolt guidelines.

Class diagram for precomputed classification_methods tuple in classifier

classDiagram
    class SemanticClassifier {
        - _classification_map
        - _classification_methods
        + __init__()
        + classify_thing(thing, language)
        - _classify_known_exceptions(thing, language)
        - _classify_by_can_be_anywhere(thing, language)
        - _classify_from_token_purpose(thing, language)
        - _classify_multi_tier_things(thing, language)
        - _classify_from_category(thing, language)
        - _classify_from_direct_connections(thing, language)
        - _classify_by_cross_language_lookup(thing, language)
    }

    class GrammarClassificationResult {
    }

    SemanticClassifier ..> GrammarClassificationResult : uses

    note for SemanticClassifier "_classification_methods = (_classify_known_exceptions, _classify_by_can_be_anywhere, _classify_from_token_purpose, _classify_multi_tier_things, _classify_from_category, _classify_from_direct_connections, _classify_by_cross_language_lookup)"
Loading

Flow diagram for classify_thing using precomputed method tuple

flowchart TD
    A[classify_thing called] --> B[Initialize results list]
    B --> C[Iterate over self._classification_methods]
    C --> D[Call current method with thing and language]
    D --> E{classification value returned?}
    E -->|No| C
    E -->|Yes| F{Is instance of GrammarClassificationResult?}
    F -->|Yes| G[Append classification to results]
    F -->|No| H[Unpack classification tuple into results]
    G --> C
    H --> C
    C --> I[Loop ends]
    I --> J[Return results]
Loading

File-Level Changes

Change Details Files
Precompute and reuse a tuple of bound classification methods to eliminate per-call list and bound method allocation in the classification hot loop.
  • Add _classification_methods tuple attribute in init containing the ordered sequence of classification methods
  • Refactor classify_thing to iterate over self._classification_methods instead of constructing an inline list on each call
src/codeweaver/semantic/classifier.py
Document the precomputation pattern for bound method collections in Bolt micro-optimization guidelines.
  • Add a new guideline entry describing the cost of constructing bound method collections in hot paths
  • Recommend precomputing static method collections in init as tuples and reusing them
.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

github-actions Bot commented May 7, 2026

🤖 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:

  • Storing bound methods in __init__ means subclasses that override any of the _classify_* methods will keep using the base implementation unless they rebuild _classification_methods; consider either documenting this explicitly or building the tuple dynamically in a way that respects overrides (e.g., via a helper that subclasses can extend).
  • Since the order of _classification_methods controls classification behavior, it may be worth centralizing that ordering in a single helper or clearly documenting it to reduce the risk of future changes accidentally altering classification precedence.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Storing bound methods in `__init__` means subclasses that override any of the `_classify_*` methods will keep using the base implementation unless they rebuild `_classification_methods`; consider either documenting this explicitly or building the tuple dynamically in a way that respects overrides (e.g., via a helper that subclasses can extend).
- Since the order of `_classification_methods` controls classification behavior, it may be worth centralizing that ordering in a single helper or clearly documenting it to reduce the risk of future changes accidentally altering classification precedence.

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

github-actions Bot commented May 7, 2026

🤖 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 the grammar-based semantic node classification hot loop by hoisting a static set of classifier steps out of classify_thing() and into GrammarBasedClassifier.__init__, reducing repeated allocations during frequent classifications.

Changes:

  • Precomputes self._classification_methods once in GrammarBasedClassifier.__init__ and iterates it in classify_thing().
  • Updates the Jules “bolt” log with the micro-optimization learning/action note.

Reviewed changes

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

File Description
src/codeweaver/semantic/classifier.py Hoists the classification method iteration list into a precomputed tuple on the classifier instance.
.jules/bolt.md Documents the optimization pattern (precomputing bound method collections) for future reference.
Comments suppressed due to low confidence (1)

src/codeweaver/semantic/classifier.py:862

  • The inline comment claims GrammarClassificationResult is a NamedTuple/tuple, but it’s defined in this module as a BasedModel (Pydantic model). This is misleading given the branching here is really distinguishing a single result vs a tuple of results from some classifiers; consider updating the comment to reflect the actual types returned by classification methods.
            if classification := method(thing, language):
                # Check if it's a tuple but NOT a GrammarClassificationResult (which is a NamedTuple, hence a tuple)
                if not isinstance(classification, GrammarClassificationResult):
                    results.extend(classification)

💡 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