Skip to content

perf(unsaved): Ask the copy rule once per branch, not once per remote ref - #720

Draft
blooop wants to merge 4 commits into
mainfrom
fix/ls-unpushed-check-quadratic
Draft

blooop wants to merge 4 commits into
mainfrom
fix/ls-unpushed-check-quadratic

Conversation

@blooop

@blooop blooop commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

The unpushed-work check now asks git one question per local branch, not one per branch and remote ref. On one kinisi_ros clone, the row for dl --ls falls from 50.4 s to 23.9 s, and the answer is the same.

In short

dl rm and dl --ls both count the commits in a clone that no remote holds. The copy rule clears a commit when a remote ref holds a copy of it (a rebase, a cherry-pick, or a one-commit squash).

The copy rule compared each local branch with a list of remote refs: the upstream of every local branch, origin/<branch>, and origin/HEAD. It ran two rev-list --cherry-mark commands for each branch and each ref. The list grows with the branches, so the cost grew with the square of the branches. On a kinisi_ros clone with 36 branches, the copy rule spent 20 s in 146 of these commands. dl --ls --json on this host did not finish in 100 s.

Now the copy rule runs one rev-list for each branch, with one tip...ref range for each ref. The confirmation of each copy does not change: the patch must be the same byte for byte, and merge-tree must replay the commit as the copy.

Evidence

The test is account_of (the function that makes one dl --ls row) on a cp -a copy of the .git of the clone kinisi-ros-proto-kinisi-ros-12125-sim-rc-e3pm. The clone has 36 local branches, 41 tags and 3981 remote refs. The copy has its work tree from HEAD. A wrapper on PATH timed each git command. Debug build, git 2.56.1.

  • Before (main, 86081fa): 50.4 s for the row, 393 git spawns.
    • 146 rev-list --cherry-mark: 19.68 s
    • 6 merge-tree --stdin (the squash rule): 19.76 s
  • After (this branch): 23.9 s to 25.4 s for the row, 111 git spawns.
    • 8 rev-list --ignore-missing --cherry-mark: 0.46 s
    • 6 merge-tree --stdin (the squash rule): 20.21 s
  • The verdict is the same. The two runs print the same holds value (md5 7277c0862ec816463c716559923ac65c), with 265 commits counted.

Before the change to the code, a script compared the two forms of the query directly on the copy, for each of the 36 branches. For each branch, the set of unpushed commits that the query marks = was the same.

Why the answer stays the same

A walk over all the ranges tip...ref1 tip...ref2 ... leaves out each merge base, so it leaves out all that the tip shares with any of the refs.

  1. The left side is the commits on the tip that no ref reaches. Each unpushed commit is in this side, because no remote ref reaches it.
  2. The right side is the commits that one of the refs reaches and the tip does not. This is the union of the right sides of the old per-ref questions.

So an unpushed commit has a copy on the right side here if and only if it had a copy on one ref before. A commit that one of the refs already reaches drops off the left side. No count holds such a commit, so nothing asks about it.

--ignore-missing drops a range whose ref does not exist, for example origin/<branch> for a branch that was never pushed. Before, git refused that one ref, and the other refs still gave their answer. Now git drops that one range, and the other ranges still give their answer. The test a_backup_made_before_the_remote_rebase_holds_nothing_unsaved has this shape.

What changes

  • Git::patches_already_on in rust/devlaunch-core/src/clients/git.rs takes &[RemoteRef], not one &RemoteRef. It asks rev-list --ignore-missing --cherry-mark --left-only --no-merges with one range for each ref. It asks --right-only only when the left side has a match.
  • already_on_a_remote in rust/devlaunch-core/src/domain/workspace_state.rs calls it once for each branch. It skips a branch when the rules already cleared all of the unpushed commits on that branch.
  • The doc comments say why one question gives the same answer.
  • CHANGELOG.md has an entry under [Unreleased].

Tests

  • New: each_branch_is_compared_with_all_its_remote_refs_in_one_question. It gives 12 branches with an upstream each and checks for 12 questions, one for each branch. Each question has 13 ranges, and the arguments are pinned. The old code made 156 questions here.
  • Changed: a_patch_match_is_a_copy_only_when_git_replays_it_as_the_copy pins the new rev-list arguments.
  • cargo test --locked --workspace: 3295 passed, 0 failed.
  • cargo clippy --locked --all-targets -- -D warnings and cargo fmt --check: clean. prek on the changed files: clean.

What this does not change

  • dl --ls still runs the full check. unsaved in --ls --json is part of the contract that wf reap reads to decide what to remove, so a cheaper and weaker status there is not safe. The check is now cheap enough to keep.
  • The squash rule (squashed_onto_a_remote) is now the largest cost: 20 s for this clone, in 6 merge-tree --stdin commands of up to 8 points times 18 refs each. It is a batch, not a spawn for each pair, and MERGE_BUDGET bounds it. A fix for it needs its own change.

🤖 Generated with Claude Code

Summary by Sourcery

Make unpushed-work detection scale with local branches instead of branch and remote-reference pairs.

Bug Fixes:

  • Reduce the cost of checking unpushed work while preserving the existing copy-detection results, including when remote references are missing.

Enhancements:

  • Consolidate remote-reference comparisons into one Git query per local branch and skip branches whose unpushed commits are already accounted for.

Tests:

  • Add coverage ensuring each branch is compared against all relevant remote references in a single query.

… ref

The copy rule compared each local branch with every local branch's
upstream, one `rev-list --cherry-mark` pair per ref. That is quadratic in
the branches: a kinisi_ros clone with 36 branches spent 20 s of its
`dl --ls` row in 146 of those spawns.

Ask about all of a branch's remote refs in one `rev-list`, one symmetric
range per ref. For a commit no remote ref reaches, the walk gives the same
candidates as one question per ref, and the byte-for-byte patch check and
the replay check that confirm each copy are unchanged. `--ignore-missing`
drops a range whose ref is not there, as one question per ref refused
that ref alone.
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Review the batched patches_already_on interface and its Git query together, including the explanation of why querying all remote refs preserves the copy candidates and how missing refs are handled.

  • rust/devlaunch-core/src/clients/git.rs L794-795, L818-836, L849-868

Implementation

Review how unpushed branches now skip work once their commits are accounted for and otherwise make one copy-rule call with all relevant remote refs.

  • rust/devlaunch-core/src/domain/workspace_state.rs L863-869, L893-898

Documentation

Review the unreleased changelog entry describing the reduction in per-branch Git queries.

  • CHANGELOG.md L10-18

Tests

Review the Git-client test updates for the new arguments and slice-based call, alongside the workspace-state coverage for missing refs and one query per branch.

  • rust/devlaunch-core/src/clients/git/tests.rs L1369-1383, L1412, L1485
  • rust/devlaunch-core/src/domain/workspace_state/tests.rs L722-723, L2428-2486

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

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.78%. Comparing base (6cdd320) to head (3b0acc1).

Files with missing lines Patch % Lines
rust/devlaunch-core/src/clients/git.rs 93.75% 1 Missing ⚠️
Additional details and impacted files
Flag Coverage Δ
python 42.98% <ø> (ø)
rust 94.97% <95.45%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
shipped code (rust) 94.97% <95.45%> (-0.02%) ⬇️
harness and tooling (python) 42.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@blooop blooop left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

This was generated by AI during review.

wf-review of 2b8f8036 against merge-base with main, fresh context.

Standards

No blocking findings. The change is small (one signature, one loop), the names say what they do, and none of the Fowler smells applies. The doc comments on patches_already_on and already_on_a_remote say why one question gives the same answer, and the repo's citation and changelog rules are kept.

Correctness, checked against real git 2.56.1, not only read:

  • Same answer. In a scratch repo, a branch with unpushed X, Y, a copy of X on origin/other, an unrelated origin/third, and a missing origin/missing: the per-ref questions mark X = only against origin/other, and refuse origin/missing. The one combined question (--ignore-missing, four ranges) marks X = and Y +, and its --right-only side holds exactly the copy plus the unrelated commit. This matches the argument in the PR body: every common ancestor of tip and a ref is behind one of that pair's merge bases, so the left side is tip minus all refs and the right side is the union of the per-ref right sides.
  • --ignore-missing. A range with a missing right end is dropped alone, and a question whose every range is missing prints nothing with exit 0 (old: a refusal). Both mean "nothing cleared", so the count is unchanged.
  • Tests. cargo test --locked --workspace --no-fail-fast: 3295 passed, 0 failed. cargo clippy --locked --all-targets -- -D warnings: clean. The real-git test a_backup_made_before_the_remote_rebase_holds_nothing_unsaved covers the missing-ref range end to end; the new scripted test pins one question per branch.

Non-blocking notes:

  1. (info) A git refusal of the combined question now clears nothing for the whole branch, where it used to lose one ref. That is the safe side, as the "Every failure here counts less as copied" comment promises, so no change is asked for.
  2. (info) --ignore-missing also drops a range whose left end is missing, silently. tip comes from for-each-ref a moment earlier, so only a race with a branch delete reaches this, and the result is again "nothing cleared".
  3. (nit) The CHANGELOG.md headline says dl --ls "no longer spends most of a minute". On the clone in the evidence the row still takes about 24 s, now in the squash rule. The body of the entry is exact; the headline could say "half a minute less" or name the check.

PR body: describes the code at this head, gives the why, the evidence, and the scope limits (squash rule left for its own change). A reader who has not seen a ticket gets the change from it alone.

Spec

No spec available. The PR closes no issue, and its commits reference none. Behavior was checked against the PR body's own claim (same answer, one question per branch) under Standards above.

Verdict

Approve. No blocking findings. Note 3 is optional. (Posted as a comment review, since GitHub refuses an approval from the PR's own account.)

The squash rule still takes most of a --ls row after this change.

This branch has not been deployed

No deployments
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.

1 participant