⚡ Bolt: Optimize allocation in Tarjan's SCC algorithm#156
⚡ Bolt: Optimize allocation in Tarjan's SCC algorithm#156bashandbone wants to merge 1 commit intomainfrom
Conversation
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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors 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 changesclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- You can avoid one of the
PathBufclones by reordering initialization so that you movev_bufinto one of the maps/sets and then clone from that (e.g., insertv_bufintoindices, thenclonefrom that binding forlowlinksandstack) rather than cloningv_bufmultiple 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
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&Pathlookups forindices/lowlinksaccess.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
💡 What: Optimized
tarjan_dfsinsidecrates/flow/src/incremental/invalidation.rsto stop constantly re-allocatingv.to_path_buf()inside the graph traversal inner loops. Instead, it creates the owned copy exactly once during initialization and directly uses the borrowed referencevfor allstate.indicesandstate.lowlinksHashMap lookups.🎯 Why: Graph traversals are performance-critical. Re-allocating the
PathBuffor 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_testsandcargo test -p thread-flow --lib. The benchmarks runningcompute_invalidation_seton 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: