Skip to content

⚡ Bolt: [Performance: Defer Allocation during Traversal]#246

Open
bashandbone wants to merge 1 commit into
mainfrom
bolt-optimize-tarjan-dfs-16447134283470556383
Open

⚡ Bolt: [Performance: Defer Allocation during Traversal]#246
bashandbone wants to merge 1 commit into
mainfrom
bolt-optimize-tarjan-dfs-16447134283470556383

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented May 19, 2026

💡 What: Refactored tarjan_dfs in crates/flow/src/incremental/invalidation.rs to eliminate redundant heap allocations when checking HashMaps inside the hot path. Leverages Rust's Borrow<Path> trait by passing the reference v instead of calling v.to_path_buf() repeatedly.

🎯 Why: During the graph traversal algorithm, the string/path was being allocated on the heap inside a loop just to check against existing HashMap keys. This caused unnecessary memory allocations O(E) instead of O(V).

📊 Impact: Reduces memory churn and heap allocations significantly during DAG traversals, speeding up incremental analysis by removing O(E) allocations per component.

🔬 Measurement: Verified that tests pass via cargo test -p thread-flow --test invalidation_tests and ensured correct behavior with no performance regressions.


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

Summary by Sourcery

Reduce heap allocations in incremental invalidation DFS traversal and apply minor style cleanups across AST and rule engine modules.

Enhancements:

  • Optimize Tarjan DFS in the invalidation detector to avoid repeated PathBuf allocations during graph traversal by reusing a single allocation and leveraging borrowed lookups.

Chores:

  • Apply consistent Rust formatting and style adjustments in AST engine string handling, rule variable collection, and referent rule registration code paths.

Refactors `tarjan_dfs` inside `crates/flow/src/incremental/invalidation.rs` to defer `PathBuf` heap allocations. Allocates `PathBuf` exactly once upon node initialization and replaces `v.to_path_buf()` on inner loop lookups with the borrowed reference `v`. This eliminates redundant `O(E)` memory heap allocations per node visited.

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

Refactors the Tarjan DFS invalidation traversal to avoid repeated PathBuf allocations by borrowing Path keys, and applies minor formatting/unwrap_or_else style cleanups in AST and rule engine modules.

File-Level Changes

Change Details Files
Optimize Tarjan DFS invalidation traversal by deferring PathBuf allocation and using borrowed Path keys for map/set lookups.
  • Create a single PathBuf (v_buf) per DFS call instead of repeatedly calling v.to_path_buf() in the function.
  • Insert cloned PathBufs into indices, lowlinks, and stack once, while using the original &Path v as a borrowed key for HashMap lookups via Borrow.
  • Update lowlink and index retrieval to use state.lowlinks.get_mut(v) and state.indices.get(v), eliminating heap allocations inside inner loops.
  • Use the borrowed &Path v for on_stack membership checks and final root-node checks, reducing allocations from O(E) to O(V) in the hot path.
crates/flow/src/incremental/invalidation.rs
Apply minor formatting and readability adjustments in AST engine String editing and tree-sitter tests.
  • Reformat a long unwrap_or_else chain in ContentExt for String into a single, more compact expression.
  • Reformat a long assert_eq! in a tree-sitter test into a multi-line form for readability without changing behavior.
crates/ast-engine/src/tree_sitter/mod.rs
Tidy rule engine code formatting for consistency and readability.
  • Reformat Rule::defined_vars match arm to a multi-line iterator chain without changing logic.
  • Condense Registration::read lock handling and unwrap_or_else into a single expression line while preserving behavior.
crates/rule-engine/src/rule/mod.rs
crates/rule-engine/src/rule/referent_rule.rs

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

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 reviewed your changes and they look great!


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.

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

This PR optimizes Tarjan SCC traversal in the incremental invalidation detector by avoiding repeated PathBuf allocations during hot-path map lookups (leveraging borrowed &Path lookups against RapidMap<PathBuf, _> / RapidSet<PathBuf>). It also includes a few rustfmt-style formatting cleanups in unrelated modules.

Changes:

  • Refactor tarjan_dfs to reuse an owned PathBuf for inserts and use borrowed &Path for repeated get/get_mut lookups in the DFS inner loop.
  • Minor formatting-only adjustments in rule-engine and tree-sitter modules/tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/flow/src/incremental/invalidation.rs Reduce allocations in Tarjan DFS by avoiding v.to_path_buf() in repeated HashMap/HashSet lookups.
crates/rule-engine/src/rule/referent_rule.rs Formatting-only refactor (single-line chaining).
crates/rule-engine/src/rule/mod.rs Formatting-only refactor for defined_vars match arm.
crates/ast-engine/src/tree_sitter/mod.rs Formatting-only refactor of unwrap_or_else and a test assertion.

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

let index = state.index_counter;
state.indices.insert(v.to_path_buf(), index);
state.lowlinks.insert(v.to_path_buf(), index);
// ⚡ Bolt: Defer allocation by allocating the PathBuf exactly once instead of on every map/set operation
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