Skip to content

⚡ Optimize PathBuf clones in graph traversal#133

Merged
bashandbone merged 4 commits intomainfrom
optimize-graph-traversal-clones-1411038259116233466
Mar 26, 2026
Merged

⚡ Optimize PathBuf clones in graph traversal#133
bashandbone merged 4 commits intomainfrom
optimize-graph-traversal-clones-1411038259116233466

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented Mar 23, 2026

  • Analyze reviewer feedback on find_affected_files traversal
  • Apply fix: skip enqueuing dependents already present in affected set
  • Verify fix is logically correct (prevents duplicate queue entries in fan-in/diamond graphs)
  • Confirm pre-existing thread-language compilation error is unrelated to changes

⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.

Refactor `find_affected_files` to reduce memory allocations:
- Remove redundant `visited` set (use `affected` set instead).
- Use `VecDeque<&PathBuf>` to avoid cloning paths during queue processing.
- Ensure only one `clone()` per unique affected node is performed.

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

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Optimizes the DependencyGraph::find_affected_files BFS traversal to avoid redundant PathBuf cloning and removes the separate visited set by operating on references and merging state into a single affected set.

Class diagram for DependencyGraph and related types in optimized traversal

classDiagram
    class DependencyGraph {
        +find_affected_files(changed_files: RapidSet_PathBuf) RapidSet_PathBuf
        +get_dependents(file: PathBuf_ref) Iterator_DependencyEdge
    }

    class RapidSet_PathBuf {
    }

    class DependencyEdge {
        +from: PathBuf
        +effective_strength() DependencyStrength
    }

    class DependencyStrength {
        <<enumeration>>
        Strong
        Weak
    }

    class PathBuf {
    }

    class Iterator_DependencyEdge {
    }

    class VecDeque_PathBuf_ref {
    }

    DependencyGraph --> RapidSet_PathBuf : uses
    DependencyGraph --> Iterator_DependencyEdge : iterates
    DependencyGraph --> VecDeque_PathBuf_ref : queue
    DependencyEdge --> PathBuf : owns
    DependencyEdge --> DependencyStrength : labeled_by
Loading

File-Level Changes

Change Details Files
Optimize BFS queue to avoid PathBuf allocations by working with references instead of owned paths.
  • Change queue type from VecDeque to VecDeque<&PathBuf> initialized from changed_files.iter() without cloning.
  • Update get_dependents calls to take &PathBuf directly instead of an owned PathBuf.
  • Enqueue dependent nodes as &edge.from instead of cloning PathBuf instances into the queue.
crates/flow/src/incremental/graph.rs
Merge visited and affected sets to reduce bookkeeping and ensure only one PathBuf clone per unique affected node.
  • Remove the separate visited RapidSet and use the affected set for both visited tracking and result collection.
  • Change the BFS loop to insert into affected first and skip processing when insertion fails, guaranteeing each path is processed once.
  • Clone PathBuf only when inserting into affected, reducing total PathBuf allocations from per-traversal-step to per-unique-node.
crates/flow/src/incremental/graph.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

Optimizes DependencyGraph::find_affected_files traversal by reducing PathBuf cloning during BFS over reverse dependency edges in the incremental analysis graph.

Changes:

  • Replaced the owned VecDeque<PathBuf> queue with a reference-based VecDeque<&PathBuf>.
  • Removed the separate visited set by using affected as the visited/output set.
  • Avoided cloning when enqueuing dependents by pushing references to edge.from.

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

Comment on lines +279 to 282
for edge in self.get_dependents(file) {
if edge.effective_strength() == DependencyStrength::Strong {
queue.push_back(edge.from.clone());
queue.push_back(&edge.from);
}
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

Dependents are enqueued unconditionally (queue.push_back(&edge.from)), so the same path can be pushed many times in fan-in/diamond graphs, causing extra queue churn and (due to the clone-on-pop) extra allocations. To keep the traversal closer to “one clone per unique affected node”, skip enqueuing when affected already contains edge.from (or maintain a separate lightweight visited check before pushing).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot apply changes based on this feedback

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.

Fixed in a91cd43. Dependents are now only enqueued when affected doesn't already contain them, eliminating duplicate queue entries in fan-in/diamond graphs. The guard is applied consistently alongside the existing contains check at dequeue time.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Adam Poulemanos <89049923+bashandbone@users.noreply.github.com>
… churn

Agent-Logs-Url: https://github.com/knitli/thread/sessions/92b7ba36-beb7-4460-8dfd-629b39468071

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
…6233466

Signed-off-by: Adam Poulemanos <89049923+bashandbone@users.noreply.github.com>
@bashandbone bashandbone merged commit b8e96f0 into main Mar 26, 2026
11 of 13 checks passed
@bashandbone bashandbone deleted the optimize-graph-traversal-clones-1411038259116233466 branch March 26, 2026 23:49
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.

3 participants