Skip to content

⚡ Bolt: Optimize allocation in Tarjan's SCC algorithm#156

Open
bashandbone wants to merge 1 commit intomainfrom
bolt-optimize-tarjan-dfs-10362869827034348044
Open

⚡ Bolt: Optimize allocation in Tarjan's SCC algorithm#156
bashandbone wants to merge 1 commit intomainfrom
bolt-optimize-tarjan-dfs-10362869827034348044

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented Apr 17, 2026

💡 What: Optimized tarjan_dfs inside crates/flow/src/incremental/invalidation.rs to stop constantly re-allocating v.to_path_buf() inside the graph traversal inner loops. Instead, it creates the owned copy exactly once during initialization and directly uses the borrowed reference v for all state.indices and state.lowlinks HashMap lookups.

🎯 Why: Graph traversals are performance-critical. Re-allocating the PathBuf for every lookup creates enormous amounts of memory churn and limits traversal speed, significantly increasing runtime on large module trees.

📊 Impact: Considerably reduces heap allocations by substituting O(E) object creations with zero-cost borrowed queries (&Path). Graph iteration and change-propagation phases should execute noticeably faster for deep project trees.

🔬 Measurement: Verify via cargo test -p thread-flow --test invalidation_tests and cargo test -p thread-flow --lib. The benchmarks running compute_invalidation_set on the large topological graphs in the suite should indicate reduced wall-time variance.


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

Summary by Sourcery

Enhancements:

  • Reduce PathBuf allocations in tarjan_dfs by reusing a single owned path per node and using borrowed lookups for indices and lowlinks.

Removes multiple redundant `PathBuf` heap allocations during the strongly connected component graph traversal in `tarjan_dfs`. Replaces `v.to_path_buf()` with a reused clone during setup, and uses the borrowed `&Path` representation for subsequent `HashMap::get` and `get_mut` calls. This optimizes critical O(E+V) code paths.

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 April 17, 2026 17:51
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 17, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the Tarjan SCC DFS implementation in the invalidation logic to avoid repeated PathBuf allocations by creating a single owned PathBuf per traversal and reusing references for hashmap lookups and stack/sets, improving performance of graph traversals.

Class diagram for TarjanState and InvalidationDetector tarjan_dfs changes

classDiagram
    class InvalidationDetector {
        +tarjan_dfs(v_path, state, sccs)
    }

    class TarjanState {
        +indices
        +lowlinks
        +stack
        +on_stack
        +index_counter
    }

    InvalidationDetector --> TarjanState : uses

    class Path {
    }

    class PathBuf {
    }

    TarjanState "1" --> "*" PathBuf : keys_in_indices
    TarjanState "1" --> "*" PathBuf : keys_in_lowlinks
    TarjanState "1" --> "*" PathBuf : elements_in_stack
    TarjanState "1" --> "*" PathBuf : elements_in_on_stack

    InvalidationDetector ..> Path : borrows
    InvalidationDetector ..> PathBuf : allocates_once_per_call

    InvalidationDetector : +tarjan_dfs(v_path Path, state TarjanState, sccs Vec_PathBuf)
    InvalidationDetector : -create_v_buf_once
    InvalidationDetector : -reuse_v_in_hash_lookups
Loading

File-Level Changes

Change Details Files
Remove repeated PathBuf allocations for a node within Tarjan's DFS by creating one owned PathBuf up front and reusing it in state tracking structures while switching hashmap lookups to use the borrowed Path reference.
  • Introduce a single v_buf PathBuf created from v at the start of tarjan_dfs
  • Use v_buf.clone() when inserting into indices, lowlinks, and stack to reuse the initial allocation rather than calling v.to_path_buf() repeatedly
  • Insert v_buf (without cloning) into the on_stack set to track membership
  • Replace lowlinks.get_mut(&v.to_path_buf()) calls with lowlinks.get_mut(v) to perform borrowed lookups
  • Replace indices.get(&v.to_path_buf()) and lowlinks.get(&v.to_path_buf()) calls with borrowed lookups using v
crates/flow/src/incremental/invalidation.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 left some high level feedback:

  • You can avoid one of the PathBuf clones by reordering initialization so that you move v_buf into one of the maps/sets and then clone from that (e.g., insert v_buf into indices, then clone from that binding for lowlinks and stack) rather than cloning v_buf multiple times in a row.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- You can avoid one of the `PathBuf` clones by reordering initialization so that you move `v_buf` into one of the maps/sets and then clone from that (e.g., insert `v_buf` into `indices`, then `clone` from that binding for `lowlinks` and `stack`) rather than cloning `v_buf` multiple times in a row.

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 the inner-loop behavior of Tarjan’s SCC traversal in the incremental invalidation logic by removing repeated PathBuf allocations during map lookups, using borrowed &Path queries instead.

Changes:

  • Allocate v.to_path_buf() once per DFS entry and reuse it for initial state insertion.
  • Replace repeated &v.to_path_buf() lookups with borrowed &Path lookups for indices/lowlinks access.

💡 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