Repository navigation
refactor(repo): consolidate branch-enumeration scans into a single inventory - #2368
Merged
Merged
Conversation
…ventory
`src/git/repository/branches.rs` had five overlapping `for-each-ref`
accessors, each with a subtly different format string: `all_branches`,
`list_local_branches`, `list_remote_branches`, `list_tracked_upstreams`,
`fetch_all_upstreams`. On `wt list --branches --remotes` at least three
of them fired in one invocation. And `Branch` exposed both
`upstream()` (bulk scan, lazy) and `upstream_single()` (one-off) to let
callers pick between them — two paths to the same answer.
Replaces the whole surface with two scans cached on `RepoCache`:
- `refs/heads/` → `LocalBranchInventory` (Vec<LocalBranch> + name→index
map for O(1) lookups). Carries upstream info + SHA so no follow-up
scan is needed.
- `refs/remotes/` → `Vec<RemoteBranch>`.
Public accessors: `Repository::local_branches() -> &[LocalBranch]`,
`Repository::remote_branches() -> &[RemoteBranch]`. `Branch::upstream()`
is now a single canonical lookup through the inventory; `upstream_single`
is gone.
`collect/mod.rs` primes the two cells in the existing parallel phase;
downstream consumers read from the cache. The picker estimate, plain
`all_branches`, `available_branches`, and `branches_for_completion` all
route through the inventory.
Subprocess count on `wt list --branches --remotes` drops from ≥3
`for-each-ref` scans of `refs/heads/` (list_local_branches,
list_tracked_upstreams, fetch_all_upstreams) + 1 of `refs/remotes/` down
to exactly 2. Skeleton benchmark (`skeleton/warm/8`) drops from ~89ms on
main to ~27ms here, well under the <60ms target.
Format string uses `%00` as the field separator so NUL-containing args
never hit `Command::arg` (Rust rejects them before exec).
> _This was written by Claude Code on behalf of @max-sixty_
Co-authored-by: Claude <noreply@anthropic.com>
worktrunk-bot
approved these changes
Apr 22, 2026
This was referenced Apr 22, 2026
max-sixty
added a commit
that referenced
this pull request
Apr 22, 2026
…h inventory (#2371) Drops the per-call `git for-each-ref refs/remotes/*/<name>` subprocess in `Branch::remotes()` and replaces it with an in-memory filter over `Repository::remote_branches()`. The inventory was introduced in #2368; this is follow-up #1 — the one accessor left outside the consolidation. On `wt switch <branch>` for a remote-only branch, the old code spawned a duplicate `refs/remotes/` scan after the inventory scan had already run. After this change there is one canonical `for-each-ref refs/remotes/` per Repository, shared by every caller. ### Behavioral note When multiple remotes have the same branch, `remotes()[0]` now reflects the inventory's committer-timestamp-desc order rather than git's alphabetical default. The only affected sites are informational — the remote name shown in a warning at `switch.rs:417` and the `RemoteOnlyBranch` error at `repository_ext.rs:156`. No correctness impact, and nearly all users have a single remote. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
max-sixty
added a commit
that referenced
this pull request
Apr 22, 2026
…cesses (#2372) `Repository::strip_remote_prefix` ran `git rev-parse --verify refs/remotes/<ref>` followed by `git remote` every call — two subprocesses to satisfy `wt switch origin/<branch>` in the picker path. The remote-branch inventory from #2368 already splits each ref into `short_name`/`remote_name`/`local_name`, so the lookup collapses to a direct search in the cached slice. The adjacent TODO that was explicitly waiting on this inventory shape is satisfied and removed. Behavioral equivalence holds on every realistic input: local branch names and SHAs still return `None` (not in the inventory), and non-existent refs under an existing remote prefix still return `None`. The one subtle change is that `origin/HEAD` now returns `None` instead of `Some("HEAD")` — `parse_remote_branch_line` drops the symref. The switch call site would have mishandled `"HEAD"` as a branch name anyway, so this is a correctness improvement. Co-authored-by: Claude <noreply@anthropic.com>
max-sixty
added a commit
that referenced
this pull request
Apr 22, 2026
`wt switch` (picker mode) was firing `git worktree list --porcelain` twice sequentially on the critical path to skeleton — once on the main thread for `num_items_estimate` (which sizes skim's preview window), once on the background thread inside `collect::collect`. The picker already shares an `Arc<RepoCache>` between the two calls, so lifting the scan into a `OnceCell<Vec<WorktreeInfo>>` there makes the second call a cache hit. On the worktrunk dev repo the pre-skeleton subprocess count for this cluster goes 2 → 1. The invariant follows the same rule PR #2368 established for branch inventories: no caller reads through the cache after its own worktree mutation. I audited every `list_worktrees` caller and its internal helpers (`worktree_for_branch`, `worktree_at_path`, `current_worktree_info`, `resolve_worktree`, `available_branches`, `branches_for_completion`) — mutating paths either snapshot once up front and thread the slice through (`validate_remove_targets`, `step_prune`), skip re-reading post-mutation (`handle_switch`, `handle_promote`, `step_for_each`, `relocate`), or rebuild a fresh `Repository::at(...)` on a background thread (`PickerCollector::do_removal`). The `execute_switch` post-create `worktree_for_branch(base)` at `switch.rs:892` reads the unchanged base branch's worktree, not the newly-created one. The `-> anyhow::Result<Vec<WorktreeInfo>>` signature stays — clones from the cache on each call — rather than migrating to `&[WorktreeInfo]` like `local_branches` / `remote_branches`. That keeps ~20 callers untouched; the clone cost (~7 `WorktreeInfo` per call) is microseconds vs. the 5–10ms subprocess it replaces. Future parity work is tracked informally. Updated the `# Caching` module docstring to move `list_worktrees` out of the "NOT cached" list and document the invariant. Closes the `TODO(picker-perf)` at `src/commands/picker/mod.rs:81-88`. > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude <noreply@anthropic.com>
2 tasks done
3 tasks done
max-sixty
added a commit
that referenced
this pull request
May 9, 2026
## Summary The TODO in `src/commands/picker/mod.rs` asking to "dedupe `list_worktrees` calls between `num_items_estimate` and `collect::collect`" has already been resolved. `Repository::list_worktrees` has been cached on `RepoCache` since 2114fda (#2375), and the picker shares that cache across its main thread and the bg `picker-collect` thread via `Arc<RepoCache>` — `repo.clone()` shares the `OnceCell<Vec<WorktreeInfo>>`, so the second call is a memory hit. The `RepoCache::worktrees` docstring already states the contract: *"The picker warms this on the main thread (for its preview-window sizing estimate) so the background `collect::collect` pass hits memory instead of respawning the subprocess on the critical path to skeleton."* The TODO was originally added in #2250 and partially walked back in #2368 (the parenthetical noting the branch-inventory cache), but the body was never updated when #2375 closed the worktree half. ## Verification `RUST_LOG=worktrunk=debug WORKTRUNK_PICKER_DRY_RUN=1 wt -C <picker-test> switch` against the `wt-perf setup picker-test` repo shows exactly one `git worktree list --porcelain` subprocess per invocation (1 `wt-trace` record, 1 spawn debug line). No behavior change — pure doc-comment deletion (9 lines). ## Test plan - [x] `pre-commit run --files src/commands/picker/mod.rs` — passes - [x] `cargo check` — passes - [x] Empirical: 1 `git worktree list --porcelain` subprocess per `wt switch` > _This was written by Claude Code on behalf of Maximilian Roos_ Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/git/repository/branches.rshad five overlappingfor-each-refaccessors, each with a subtly different format string:all_branches,list_local_branches,list_remote_branches,list_tracked_upstreams,fetch_all_upstreams. Onwt list --branches --remotesat least three of them fired in one invocation.Branchalso exposed bothupstream()(bulk scan, lazy) andupstream_single()(one-off) — two paths to the same answer.Replaces the whole surface with two scans cached on
RepoCache:refs/heads/→LocalBranchInventory(Vec<LocalBranch>+ name → index map for O(1) lookups). Carries upstream info + SHA so no follow-up scan is needed.refs/remotes/→Vec<RemoteBranch>.Public accessors:
Repository::local_branches() -> &[LocalBranch],Repository::remote_branches() -> &[RemoteBranch].Branch::upstream()is now a single canonical lookup through the inventory;upstream_singleis gone.Navigating the diff
src/git/repository/branches.rs— new inventory types, scan functions, and the surviving public accessors (all_branches,available_branches,branches_for_completion).src/git/repository/branch.rs—Branch::upstreamnow reads from the inventory viaRepository::local_branch(name).src/git/mod.rs— newLocalBranch/RemoteBranchpublic types.src/git/repository/mod.rs— two newOnceCells onRepoCache(replaces the oldupstreamscell).src/commands/list/collect/mod.rs— parallel phase primes the inventory; later consumers read from the cache. The oldlist_untracked_remote_brancheshelper is inlined at its sole caller.src/commands/picker/mod.rs,src/commands/worktree/switch.rs,src/commands/command_executor.rs,src/git/repository/integration.rs— callers updated to the canonicalupstream()/ inventory accessors.Results
wt list --branches --remotesdrops from ≥3for-each-refscans ofrefs/heads/+ 1 ofrefs/remotes/down to exactly 2 (one per ref namespace).skeleton/warm/8benchmark: ~89ms on main → ~27ms here, well under the 60ms target.%00as the field separator so NUL-containing args never hitCommand::arg(Rust rejects them before exec).Test plan
RUST_LOG=worktrunk=debug wt list --branches --remotes— exactly 2 inventoryfor-each-refcalls (plus the unrelated ahead-behind batch that already existed).