Skip to content

perf(picker): cache log/branch-diff/upstream-diff renders to disk - #2628

Merged
max-sixty merged 3 commits into
mainfrom
picker-list
May 7, 2026
Merged

max-sixty merged 3 commits into
mainfrom
picker-list

Conversation

@max-sixty

@max-sixty max-sixty commented May 7, 2026 •

Copy link
Copy Markdown
Owner

The picker pre-computes four previews per item at skeleton time. Three of them — Log, BranchDiff, UpstreamDiff — are derivable from git object SHAs at a given terminal width, so rendering them on every `wt switch` duplicates work the prior invocation already did. This adds a SHA-keyed disk cache alongside the existing `sha_cache` so warm-cache invocations short-circuit the git subprocesses.

What's cached, by mode

  • BranchDiff / UpstreamDiff: the rendered diff string itself. Pure functions of (base_sha, head_sha, width) — cache value is what the existing compute_*_preview returns. Default-branch and upstream SHAs are resolved via rev-parse so the key stays stable across git fetch.
  • Log: a small struct (raw git log --graph output + per-commit (insertions, deletions) map). The render path recomputes merge-base + rev-list for the dim/bright split and runs format_log_output against epoch_now() on every call. Why split: dim/bright shifts as main advances, and relative-time strings drift with wall-clock — neither is in the SHA-deterministic part. Caching just the raw log + stats means the cache key stays (branch_head_sha, w, h) (no main dimension, no time bucket) while output stays correct.
  • WorkingTree is intentionally not cached — its inputs include the mutable working tree, which has no cheap stable hash. Summary already has its own cache.

Layout: .git/wt/cache/picker-preview/{mode}-{sha}[-{sha}]-{w}[-{h}].json. Diff modes cache pre-pager strings (the pager step in compute_and_page_preview runs on every read, so changing the configured pager invalidates nothing). LRU bound is 500 entries — rendered diffs are tens of KB each, vs the 80-byte SHA-pair entries sha_cache is sized for at 5000.

State management

Bundled into the existing "git commands cache" user-facing category in `wt config state get` and `state clear --all` — both `sha_cache` and the new picker preview cache surface as one count and clear together. Implementation stays in `commands/picker/preview_cache.rs` (picker-owned, distinct LRU bound) rather than mixing into `git/repository/sha_cache.rs`. Because the picker module is #[cfg(unix)], two cfg-conditional shims in state.rs keep the JSON/table shape platform-agnostic on Windows.

Key files

  • `src/commands/picker/preview_cache.rs` — new module, primitives + `LogCacheEntry` + state integration
  • `src/commands/picker/items.rs` — cache integration in `compute_log_preview` / `compute_branch_diff_preview` / `compute_upstream_diff_preview`; the log path splits cacheable raw work from per-call dim/format
  • `src/commands/config/state.rs` — bundled clear/show plus the cfg-conditional shims
  • `src/cli/config.rs` — help text for the bundled category

Tests

Round-trip + width/SHA invalidation in the new module, plus per-mode short-circuit / writeback tests through each `compute_*_preview`. Includes a regression test for worktrunk-bot's review feedback (`log_cache_dim_split_tracks_main_advance`): branches feature off main, advances main to include feature's commit, asserts the cached log re-dims via the stripped bold-green branch decoration.

This was written by Claude Code on behalf of Maximilian Roos

max-sixty and others added 2 commits May 6, 2026 19:35
The picker pre-computes four previews per item at skeleton time. Three of
them — Log, BranchDiff, UpstreamDiff — are pure functions of git object
SHAs at a given terminal width, so rendering them on every `wt switch`
duplicates work that the prior invocation already did. Add a SHA-keyed
disk cache alongside the existing `sha_cache` so warm-cache invocations
short-circuit the git subprocess.

Layout: `.git/wt/cache/picker-preview/{mode}-{sha}[-{sha}]-{w}[-{h}].json`.
Cached values are pre-pager strings, so changing the configured pager
invalidates nothing; the pager step in `compute_and_page_preview` runs on
every read. Default-branch and upstream SHAs are resolved via `rev-parse`
inside the BranchDiff and UpstreamDiff compute paths so the cache key
stays stable across `git fetch`. WorkingTree mode is not cached — its
inputs include the mutable working tree, which has no cheap stable hash.
Summary mode already has its own cache.

LRU bound is 500 entries (rendered diffs are tens of KB, much larger
than the 80-byte SHA-pair entries `sha_cache` is sized for).

Bundled into the existing `git commands cache` user-facing category in
`wt config state get` and `state clear --all` — both `sha_cache` and the
new picker preview cache surface as one count and clear together. The
implementation stays in `commands/picker/preview_cache.rs` (picker-owned,
distinct LRU bound) rather than mixing into `git/repository/sha_cache.rs`.

Tests: round-trip + width/SHA invalidation in the new module, plus
short-circuit and write-back tests through each `compute_*_preview`
covering pre-populated cache hits and post-miss writeback.

Co-Authored-By: Claude <[email protected]>
`commands::picker` is `#[cfg(unix)]` so its preview cache module doesn't
exist on Windows. The bundled "git commands cache" reporting in state.rs
referenced the picker cache directly, breaking the Windows build.

Add two cfg-conditional shim functions (`picker_preview_count`,
`picker_preview_clear`) that forward to the picker cache on unix and
return 0 / Ok(0) elsewhere. Call sites in `handle_state_show_json`,
`handle_state_show_table`, and `handle_state_clear_all` use the shims so
the JSON/table shape and clear messaging stay platform-agnostic.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The branch-diff and upstream-diff caches look right — both keys cover every input that feeds the rendered output. One concern on the log cache: see inline comment.

Comment thread src/commands/picker/items.rs Resolved
Per worktrunk-bot's review on PR #2628: the log preview cache value was
the final rendered string, but `compute_log_preview_uncached` reads two
inputs that aren't in the cache key:

- `merge-base(default_branch, head)` drives the dim/bright commit split.
  When `main` advances to contain feature's commits, merge-base moves and
  `unique_commits` empties — every commit should render dimmed. A cached
  entry kept the pre-advance bright styling until LRU eviction.
- `format_log_output` calls `format_relative_time_short(epoch_now())` on
  the `width >= 50` path, so cached "5m" / "2h" / "3d" strings drift with
  wall-clock. With no TTL, a cached "5m" could read "5m" days later.

Split the cached payload into a `LogCacheEntry { raw_log, stats }` —
just the SHA-deterministic outputs of `git log --graph` and
`batch_fetch_stats`. The render path runs `merge-base` + `rev-list` for
fresh `unique_commits`, then `process_log_with_dimming` and
`format_log_output` against `epoch_now()`. Cache key stays
`(branch_head_sha, w, h)` — keeping `main`'s SHA out means a `git fetch`
that advances `origin/main` doesn't invalidate every entry; branches are
stable far more often than main, so hit rate is higher across long time
windows. Net hit savings: skips `git log --graph` + `batch_fetch_stats`
(~20–100 ms), pays the merge-base + rev-list refresh (~10–20 ms).

Tests:

- `log_cache_writeback_on_miss`: cache is populated after first compute;
  different width still misses.
- `log_cache_dim_split_tracks_main_advance`: regression for the original
  reviewer concern. Feature branches off main with a unique commit, then
  main fast-forwards to include it. Same `branch_head_sha` (cache hit),
  but the dim/bright split must update — asserted via the bold-green
  branch decoration `\x1b[1;32m`, which `process_log_with_dimming`'s dim
  path strips from the cached raw log.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 5a654fb into main May 7, 2026
27 checks passed
@max-sixty
max-sixty deleted the picker-list branch May 7, 2026 03:33
@max-sixty max-sixty mentioned this pull request May 10, 2026
3 tasks done
max-sixty added a commit that referenced this pull request May 10, 2026
Bump worktrunk to **0.49.0** — minor bump (semver-checks reports a
breaking change in `GitError::UncommittedChanges`, which gained a
`dirty_files: Vec<String>` field).

## Highlights

**Improved**
- New `codename(n)` template filter — deterministic friendly worktree
names from a ~1.26M-combo pool (`{{ branch | codename(2) }}` →
`malleable-opah`).
[#2641](#2641), thanks
@endigma
- Picker preview disk cache for Log / BranchDiff / UpstreamDiff, with
background refresh for stale Log decorations.
[#2628](#2628),
[#2646](#2646)
- Picker shares one `default_branch_sha` lookup across BranchDiff items
instead of forking N rev-parse subprocesses.
[#2658](#2658)
- "X has uncommitted changes" errors now list the dirty files inline.
[#2653](#2653)

**Fixed**
- `wt switch` integrates with `cd` aliases like zoxide (uses `builtin
cd`). [#2644](#2644), thanks
@xkumiyu for reporting
- Empty hook tables no longer panic during `wt switch --create`.
[#2635](#2635), thanks @topit
for reporting
- `Repository::current_worktree()` and five sibling helpers no longer
silently leak the process CWD when called via `Repository::at(p)`.
[#2652](#2652)
- `WorkingTree::is_linked` tolerates non-git CWDs (e.g. Nix sandbox).
[#2625](#2625), thanks
@DArtagan for reporting
- Picker no longer overlays `collect` warnings on the active TUI; clears
its frame on exit in inline mode.
[#2627](#2627),
[#2626](#2626)
- Failed `git log` no longer poisons the picker's preview disk cache.
[#2651](#2651)
- `wt step prune` says "removing branch" for branch-only candidates.
[#2619](#2619)
- Claude Code Windows integration uses a `wt.sh` wrapper to avoid
colliding with Windows Terminal's `wt.exe`.
[#1754](#1754), thanks
@lucaspimentel

**Documentation**
- cmux workspace integration recipe.
[#1907](#1907), thanks
@alvistar

See
[CHANGELOG.md](https://github.com/max-sixty/worktrunk/blob/release/CHANGELOG.md)
for the full list including internal CI / test-isolation changes.

## Test plan

- [x] `cargo run -- hook pre-merge --yes` (tests + lints clean)
- [x] `cargo semver-checks check-release -p worktrunk` (one breaking
change confirmed → minor bump)
- [x] CHANGELOG entries verified by subagent against actual diffs

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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