Repository navigation
perf(repo): cache list_worktrees on RepoCache - #2375
Merged
Merged
Conversation
`wt switch` (picker mode) fired `git worktree list --porcelain` twice sequentially on the critical path to skeleton: once on the main thread for `num_items_estimate` (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 means the second call becomes a cache hit. The same invariant PR #2368 established for branch inventories applies: no caller reads through the cache after its own worktree mutation. Audited every `list_worktrees` caller (plus internal helpers like `worktree_for_branch`, `worktree_at_path`, `current_worktree_info`, `resolve_worktree`) — mutating paths either snapshot the list once up front and thread the slice through, skip re-reading post-mutation, or rebuild a fresh `Repository::at(...)` on a background thread. Kept the `-> anyhow::Result<Vec<WorktreeInfo>>` signature (clones from the cache) to avoid touching ~20 callers; the clone cost is microseconds vs. the 5–10ms subprocess it replaces. Co-authored-by: Claude <[email protected]>
worktrunk-bot
approved these changes
Apr 22, 2026
max-sixty
added a commit
that referenced
this pull request
Apr 22, 2026
Follows #2375. That PR cached `list_worktrees` on `RepoCache` but kept the `Vec<WorktreeInfo>` return type (cloning from the cache on each call) to minimize churn — explicitly deferring this signature change. The sibling inventories `local_branches` and `remote_branches` already return `&[T]`; this brings `list_worktrees` in line and eliminates the per-call clones. The implementation mirrors `remote_branches` in `src/git/repository/branches.rs`: `get_or_try_init(...).map(Vec::as_slice)`. Call-site migration is mostly mechanical — slice ops (`.iter()`, `[0]`, `.is_empty()`, `.len()`) work unchanged. Callers that need ownership switch from `.into_iter()` to `.iter().cloned()`; one call to `sort_worktrees_with_cache` uses `.to_vec()`. In `list/collect`, the old `OnceCell<Result<Vec<_>>>` carrier inside the rayon scope is gone — the scope just warms `repo.list_worktrees()`, which already caches on `RepoCache`, and the value is re-read after the scope. In `repository_ext::prepare_worktree_removal`, the `owned_worktrees` scoped-local dance collapses to a single match binding. Also deletes unused `Repository::current_worktree_info()` (zero callers, noticed while reviewing the diff). Subprocess count for `wt switch` is unchanged — still 1 `git worktree list` call per command, confirmed via `WORKTRUNK_PICKER_DRY_RUN=1 RUST_LOG=worktrunk=debug wt switch`. > _This was written by Claude Code on behalf of @max-sixty_ Co-authored-by: Claude <[email protected]>
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) <[email protected]>
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.
wt switch(picker mode) was firinggit worktree list --porcelaintwice sequentially on the critical path to skeleton — once on the main thread fornum_items_estimate(which sizes skim's preview window), once on the background thread insidecollect::collect. The picker already shares anArc<RepoCache>between the two calls, so lifting the scan into aOnceCell<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_worktreescaller 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 freshRepository::at(...)on a background thread (PickerCollector::do_removal). Theexecute_switchpost-createworktree_for_branch(base)atswitch.rs:892reads 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]likelocal_branches/remote_branches. That keeps ~20 callers untouched; the clone cost (~7WorktreeInfoper call) is microseconds vs. the 5–10ms subprocess it replaces. Future parity work is tracked informally.Updated the
# Cachingmodule docstring to movelist_worktreesout of the "NOT cached" list and document the invariant. Closes theTODO(picker-perf)atsrc/commands/picker/mod.rs:81-88.