Skip to content

⚡ Bolt: [performance improvement] Optimize Tarjan DFS allocations#234

Open
bashandbone wants to merge 1 commit into
mainfrom
bolt/perf-tarjan-dfs-allocations-15864650468603263492
Open

⚡ Bolt: [performance improvement] Optimize Tarjan DFS allocations#234
bashandbone wants to merge 1 commit into
mainfrom
bolt/perf-tarjan-dfs-allocations-15864650468603263492

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented May 15, 2026

💡 What: Cached v.to_path_buf() to a local variable v_buf and reused it, while replacing map lookups to use the borrowed reference v. Also cleaned up explicit lifetimes in check_var.rs as identified by clippy.
🎯 Why: Repeated calls to v.to_path_buf() caused excessive O(E) heap allocations during SCC DAG traversal within tarjan_dfs, degrading incremental build performance.
📊 Impact: Significantly reduces memory churn and O(E) heap allocations to O(V) during graph invalidation computations by utilizing zero-cost Borrow traits for Map lookups.
🔬 Measurement: Observe memory allocations and execution time of cargo test -p thread-flow --test invalidation_tests.


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

Summary by Sourcery

Optimize incremental invalidation Tarjan DFS to reduce path allocation overhead and apply minor code cleanups across AST and rule engine modules.

Enhancements:

  • Reduce heap allocations in Tarjan DFS by caching path conversions and using borrowed keys for map lookups.
  • Simplify function signatures in the rule engine by removing unnecessary explicit lifetimes.
  • Apply minor readability and formatting improvements in AST and rule engine utilities.

Eliminates unnecessary `PathBuf` heap allocations during DAG traversals
in `crates/flow/src/incremental/invalidation.rs`.

- Cached `v.to_path_buf()` into `v_buf` for required insertions.
- Replaced map lookups `get(&v.to_path_buf())` with direct borrowed reference `get(v)`.
- Replaced `get_mut(&v.to_path_buf())` with `get_mut(v)`.
- Fixed implicit lifetimes in `crates/rule-engine/src/check_var.rs` to satisfy clippy `needless_lifetimes`.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
@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.

Copilot AI review requested due to automatic review settings May 15, 2026 18:09
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 15, 2026

Reviewer's Guide

Optimizes Tarjan DFS by reusing a cached PathBuf and switching map lookups to borrow-based access, while also applying minor Clippy-driven cleanup and formatting changes across AST and rule-engine modules.

File-Level Changes

Change Details Files
Reduce allocations and leverage borrowed keys in Tarjan DFS implementation used for invalidation.
  • Cache v.to_path_buf() into a local v_buf and reuse it instead of recomputing for each map/stack operation.
  • Change indices/lowlinks/stack/on_stack insertions to use cloned v_buf rather than new PathBuf allocations per call.
  • Switch hash map lookups in lowlinks/indices from using &v.to_path_buf() to borrowing &Path (v) to take advantage of the map’s Borrow implementation.
  • Use borrowed v in indices/lowlinks lookups when computing v_index and v_lowlink to avoid extra allocations.
crates/flow/src/incremental/invalidation.rs
Apply minor refactors and style cleanups suggested by Clippy and rustfmt in AST and rule-engine code.
  • Reformat a FromUtf8 error-handling expression into a single chained call for readability.
  • Reformat an assert_eq! call in tests over multiple lines for clarity.
  • Remove unnecessary explicit lifetime parameter from check_var_in_constraints and accept a simple shared reference instead.
  • Remove unnecessary explicit lifetime parameter from check_var_in_transform and accept a simple shared reference instead.
  • Format Rule::Pattern branch to break the iterator chain across multiple lines.
  • Inline the read lock unwrap_or_else call in Registration::read into a single expression.
crates/ast-engine/src/tree_sitter/mod.rs
crates/rule-engine/src/check_var.rs
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 found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="crates/flow/src/incremental/invalidation.rs" line_range="348-350" />
<code_context>

     /// DFS helper for Tarjan's algorithm
     fn tarjan_dfs(&self, v: &Path, state: &mut TarjanState, sccs: &mut Vec<Vec<PathBuf>>) {
+        let v_buf = v.to_path_buf();
         // Initialize node
         let index = state.index_counter;
-        state.indices.insert(v.to_path_buf(), index);
-        state.lowlinks.insert(v.to_path_buf(), index);
+        state.indices.insert(v_buf.clone(), index);
+        state.lowlinks.insert(v_buf.clone(), index);
         state.index_counter += 1;
-        state.stack.push(v.to_path_buf());
-        state.on_stack.insert(v.to_path_buf());
+        state.stack.push(v_buf.clone());
+        state.on_stack.insert(v_buf.clone());

         // Visit all successors (dependencies)
</code_context>
<issue_to_address>
**suggestion (performance):** Reduce redundant cloning of `v_buf` when initializing Tarjan state for a node.

`v_buf` is now allocated once, which is good, but it’s still cloned four times (for `indices`, `lowlinks`, `stack`, and `on_stack`). You can avoid one clone by moving `v_buf` into the last insertion:

```rust
let v_buf = v.to_path_buf();
state.indices.insert(v_buf.clone(), index);
state.lowlinks.insert(v_buf.clone(), index);
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf);
```

```suggestion
        state.index_counter += 1;
        state.stack.push(v_buf.clone());
        state.on_stack.insert(v_buf);
```
</issue_to_address>

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.

Comment on lines 348 to +350
state.index_counter += 1;
state.stack.push(v.to_path_buf());
state.on_stack.insert(v.to_path_buf());
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf.clone());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Reduce redundant cloning of v_buf when initializing Tarjan state for a node.

v_buf is now allocated once, which is good, but it’s still cloned four times (for indices, lowlinks, stack, and on_stack). You can avoid one clone by moving v_buf into the last insertion:

let v_buf = v.to_path_buf();
state.indices.insert(v_buf.clone(), index);
state.lowlinks.insert(v_buf.clone(), index);
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf);
Suggested change
state.index_counter += 1;
state.stack.push(v.to_path_buf());
state.on_stack.insert(v.to_path_buf());
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf.clone());
state.index_counter += 1;
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf);

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 reduces unnecessary heap allocations during Tarjan SCC traversal in the incremental invalidation detector by caching PathBuf conversions and leaning on borrowed lookups, plus applies small clippy/rustfmt cleanups in the rule engine and tree-sitter utilities.

Changes:

  • Optimize tarjan_dfs by caching v.to_path_buf() and switching map/set lookups to use borrowed &Path.
  • Remove redundant explicit lifetimes in check_var.rs per clippy.
  • Apply formatting-only refactors in rule engine and tree-sitter tests/utility code.

Reviewed changes

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

Show a summary per file
File Description
crates/flow/src/incremental/invalidation.rs Reduces PathBuf allocations in Tarjan DFS and uses borrowed lookups for RapidMap/RapidSet.
crates/rule-engine/src/check_var.rs Simplifies function signatures by removing unnecessary explicit lifetimes.
crates/rule-engine/src/rule/referent_rule.rs Minor refactor (single-expression chain) in Registration::read.
crates/rule-engine/src/rule/mod.rs Formatting-only change to defined_vars for Rule::Pattern.
crates/ast-engine/src/tree_sitter/mod.rs Minor formatting refactors in UTF-8 conversion and a test assertion.

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

state.stack.push(v.to_path_buf());
state.on_stack.insert(v.to_path_buf());
state.stack.push(v_buf.clone());
state.on_stack.insert(v_buf.clone());
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