Skip to content

perf(prewarm): fold cold-path rev-parses into one fork - #2541

Merged
max-sixty merged 2 commits into
mainfrom
rev-parse-merge
May 3, 2026
Merged

max-sixty merged 2 commits into
mainfrom
rev-parse-merge

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

A plain wt <alias> invocation used to spawn two git rev-parse forks before alias dispatch: --git-common-dir from init_command_log and the prewarm_info batch (--is-inside-work-tree --show-toplevel --git-dir --symbolic-full-name HEAD) from project_config_path. Visible in [wt-trace] output:

[wt-trace] cmd=\"git rev-parse --git-common-dir\" dur_us=10139
[wt-trace] cmd=\"git rev-parse --is-inside-work-tree --show-toplevel --git-dir --symbolic-full-name HEAD\" dur_us=10564

This adds Repository::prewarm() at the top of main (after the logger is registered, before init_command_log and alias dispatch). One merged git rev-parse --git-common-dir --is-inside-work-tree --show-toplevel --git-dir --symbolic-full-name HEAD fork populates GIT_COMMON_DIR_CACHE and the per-worktree git-discovery maps in one shot. Both downstream callers then hit memory.

After the change, the trace shows one rev-parse and pure cache hits afterward:

[wt-trace] span=\"prewarm\" dur_us=5917
[wt-trace] cmd=\"git rev-parse --git-common-dir --is-inside-work-tree --show-toplevel --git-dir --symbolic-full-name HEAD\" dur_us=5159
[wt-trace] span=\"init_command_log\" dur_us=39          # was 14144 (cache hit)
[wt-trace] span=\"project_config_load\" dur_us=25       # was 10714 (cache hit)

Cache consolidation

Promoting the maps to process-wide is necessary for the optimization to land: every Repository::current() builds a fresh RepoCache, so writing to per-Repo maps from prewarm would warm a Repository nobody used. While doing this, worktree_roots / git_dirs / current_branches move out of RepoCache to LazyLock<DashMap> statics next to GIT_COMMON_DIR_CACHE. Per-RepoCache scoping was a historical accident — the data is filesystem-keyed and process-invariant, and the same staleness contract carries over (snapshot-at-first-read, no invalidation). WorkingTree::{root,git_dir,branch} and prewarm_info now read/write those statics directly. The intermediate PREWARM_INFO_CACHE snapshot type is gone — one cache layer instead of two.

Caching spec doc updated to describe both layers (RepoCache per-Repository, plus the process-wide git-discovery statics).

Bench

benches/alias.rs, stub variants, 30s measurement:

Variant Before After Change
dispatch/wt_version 3.48 ms 3.54 ms unchanged (clap exits before prewarm)
dispatch/warm/1 22.4 ms 18.2 ms −18.5%
dispatch/warm/100 24.3 ms 18.2 ms −24.1%
dispatch/cold/1 22.6 ms 19.3 ms −17.2%
dispatch/cold/100 28.9 ms 20.1 ms −27.2%

All four worktree variants regress to ~18–20 ms median regardless of cold/warm or worktree count. wt_version is unchanged because --version is handled by clap inside parse_cli before Repository::prewarm() is called — same code path as before.

Edge cases

  • Bare repo at the bare root — --show-toplevel errors but --git-common-dir lands first, so we cache the common dir. WORKTREE_ROOTS stays empty for that path (its membership invariant — contains_key ⇔ inside a worktree — must hold), so prewarm_info reforks once. Net cost: same as the unoptimized baseline. The prewarm doc spells this out.
  • Unborn HEAD — every selector emits a line but rev-parse exits non-zero. We populate WORKTREE_ROOTS and GIT_DIRS but leave CURRENT_BRANCHES to the symbolic-ref fallback in WorkingTree::branch, matching the existing prewarm_info behaviour.
  • Outside any work tree (wt from a non-repo dir) — merged batch fails entirely, nothing cached. Repository::at later runs its own rev-parse and surfaces the discovery error normally.

Test plan

  • cargo test --lib --bins (1076 + 607 passing, all 4 prewarm tests pass)
  • pre-commit run --all-files (fmt + clippy + lychee + custom checks)
  • Trace inspection on cold cache: subprocess Test CI fixes and improvements #2 absent, both downstream callers hit cache
  • Bench compare on dispatch/{warm,cold}/{1,100}
  • Manual smoke tests: unborn HEAD repo, bare repo, linked worktree

🤖 Generated with Claude Code

Adds Repository::prewarm() at the top of main: one merged
`git rev-parse --git-common-dir --is-inside-work-tree --show-toplevel
--git-dir --symbolic-full-name HEAD` fork populates the process-wide
git-discovery caches up front, so init_command_log and
try_alias/project_config_path hit memory instead of each spawning their
own rev-parse.

Consolidates the per-RepoCache `worktree_roots` / `git_dirs` /
`current_branches` maps into process-wide LazyLock<DashMap> statics next
to GIT_COMMON_DIR_CACHE. Per-Repo scoping was only ever a historical
accident — the data is filesystem-keyed and process-invariant, and
making it process-wide is what lets one eager prewarm at startup warm
every later Repository::current() (each builds a fresh RepoCache).
WorkingTree::{root,git_dir,branch} read/write the same statics.

Bench (dispatch/{warm,cold}/{1,100}, 30s measurement):

  warm/1     22.4 ms → 18.2 ms   −18.5%
  warm/100   24.3 ms → 18.2 ms   −24.1%
  cold/1     22.6 ms → 19.3 ms   −17.2%
  cold/100   28.9 ms → 20.1 ms   −27.2%

wt_version unchanged (clap exits before prewarm runs).

Co-Authored-By: Claude <noreply@anthropic.com>
`cargo doc -Dwarnings` (run by `pre-merge project:doc`) refuses to
render links from public items to private ones. The prewarm doc and the
docs on `WorkingTree::{prewarm_info, branch, root, git_dir}` linked to
the new `pub(super)` statics — switch those references to plain
backticks; readers of the public API don't need clickable links to
internal caches.

Co-Authored-By: Claude <noreply@anthropic.com>
@max-sixty
max-sixty merged commit fd78f60 into main May 3, 2026
26 checks passed
@max-sixty
max-sixty deleted the rev-parse-merge branch May 3, 2026 05:04
@max-sixty max-sixty mentioned this pull request May 3, 2026
max-sixty added a commit that referenced this pull request May 3, 2026
Cuts v0.47.0. Highlights:

- **Integration detection fix** across `wt list`, `wt remove`, `wt
merge`, `wt step prune` for the local/upstream-diverged case (#2507,
#2513, #2515).
- **Faster shell-command dispatch** (~10ms per invocation, 1.49× on
trivial aliases) via event-driven signal forwarding (#2537, #2538), plus
parallel config load (#2543) and merged cold-start prewarm rev-parse
(#2541).
- **`wt switch <number>` suggests `pr:N` / `mr:N`** instead of just
`--create` (#2516).
- **Library API breaking changes**: `RefSnapshot` cutover (#2528, #2530)
and the `for-each` direct-exec change (#2465). Full list in
`CHANGELOG.md`.

See `CHANGELOG.md` for the complete entry.

> _This was written by Claude Code on behalf of @max-sixty_

---------

Co-authored-by: Claude <noreply@anthropic.com>
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