Skip to content

perf(repo): cache list_worktrees on RepoCache - #2375

Merged
max-sixty merged 1 commit into
mainfrom
picker-dedupe-worktrees
Apr 22, 2026
Merged

max-sixty merged 1 commit into
mainfrom
picker-dedupe-worktrees

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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

`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]>
@max-sixty
max-sixty merged commit 2114fda into main Apr 22, 2026
25 checks passed
@max-sixty
max-sixty deleted the picker-dedupe-worktrees branch April 22, 2026 02:28
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]>
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]>
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.

2 participants