Repository navigation
perf(list): dedup commit→tree resolution via in-memory cache - #3288
Merged
Merged
Conversation
`wt list --full` peeled the shared default-branch tip to its tree once per worktree/branch row inside the integration probe — N identical `git rev-parse <sha>^{tree}` subprocesses for one value.
Add a commit→tree memo to the in-memory `RepoCache` (a `DashMap` get-or-create, mirroring `merge_base_by_sha`): the `Entry` lock serializes the first miss so parallel rows sharing the target spawn one process, not one each. Route both `would_merge_add_to_target` and `trees_match_by_sha` through it, so the shared target tree resolves exactly once per run.
In-memory rather than the persistent `sha_cache`: the resolution is ~1ms, so cross-run persistence buys little, while the in-memory layer eliminates the cold-run race the disk cache would have. Document the in-memory-vs-disk decision (and the front-over-back variant) in the `# Caching` module section, which previously left it implicit.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
worktrunk-bot
approved these changes
Jun 28, 2026
Collaborator
|
Approved. Required checks that have finished are green — |
max-sixty
added a commit
that referenced
this pull request
Jun 28, 2026
…bes (#3289) ## What A cold `wt list` runs `git merge-tree --write-tree <target> <branch>` **twice per row**: once from `MergeTreeConflictsTask` (which wants the conflict bit, for the `WouldConflict` column, via `has_merge_conflicts_by_sha` → `run_merge_tree`) and once from `WouldMergeAddTask` (which wants the resulting tree, for the integration column, via `merge_integration_probe_by_sha` → `would_merge_add_to_target`). When the local default branch is also the integration primary — the common case — both calls are the *byte-identical* subprocess `git merge-tree --write-tree <default> <branch>`. They differ only in which field of the outcome each reads (conflict flag vs. clean-merge tree). The two answers are cached in two different persistent kinds (`merge-tree-conflicts` vs. `merge-add-probe`), keyed separately, and the persistent caches don't hold a lock across compute, so on a cold run neither probe sees the other's spawn — the same merge fires twice. `git merge-tree` is the single most expensive command in `wt list` (in a busy-repo profile: 156 calls, ~25 s total, ~162 ms avg). ## Fix Add an in-memory `RepoCache::merge_tree` cache and route `Repository::merge_tree_outcome` through it with the entry-lock get-or-create pattern — the same shape `merge_base_by_sha` already uses. The shard lock is held across the subprocess, so parallel rows sharing a `(target, branch)` pair (and, crucially, the two probes on the *same* row) collapse to one spawn. The key is `(a, b)` in call order; both dedup sites pass `(target, branch)`, and the cached outcome doesn't depend on argument order, so a symmetric key would add nothing. Scope: in-memory, command-lifetime only — warm runs already short-circuit at the higher-level persistent caches (`merge-tree-conflicts` / `merge-add-probe`) and never reach `merge_tree_outcome`. This only removes the cold-run duplicate. ## Evidence Cold `wt list --full` on a 16-worktree repo, counting authoritative `[wt-trace] cmd=` records: ``` before: 33 merge-tree spawns, 18 distinct pairs (15 exact duplicates) after: 18 merge-tree spawns, 18 distinct pairs (0 duplicates) ``` Every duplicated pair was `(main_HEAD, <branch>)` — confirming the two probes, not a cold-run race. Output (integration symbols, conflict markers) is unchanged: the cached `MergeTreeOutcome` is identical to a fresh compute. This is orthogonal to and composes with #3288 (which dedups commit→tree resolution): that change caches `commit_to_tree_sha`; this one caches the `merge-tree` outcome itself. ## Testing Added `conflict_and_integration_probes_share_one_merge_tree_spawn`, which pins the load-bearing invariant: after running both probes for one row, exactly one `merge_tree` entry exists, keyed `(target, branch)`. An argument-order regression on either call site would split it into two entries (two spawns) and fail the test. Full pre-merge gate green (4273 tests). > _This was written by Claude Code on behalf of max_ Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
social4hyq
pushed a commit
to social4hyq/homebrew-core
that referenced
this pull request
Sep 20, 2026
worktrunk 0.63.0 Created-by: HarmonybrewBot Commit-by: HarmonybrewBot Merged-by: HarmonybrewBot Description: Created by `brew bump` --- Created with `brew bump-formula-pr`.<details> <summary>release notes</summary> <pre>## Release Notes ### Improved - **`[list] columns` can force a column on past `--full`**: Listing a column now overrides the `--full` / `[list] summary` presets — `columns = ["branch", "ci"]` shows the CI column without `--full`. Hard data-source prerequisites still apply: a listed `summary` with no `[commit.generation]` command, or `url` with no template, stays hidden, since listing can't conjure data that isn't configured. ([#3295](max-sixty/worktrunk#3295)) - **`Alt-r` in the picker refreshes the preview panes, not just the rows**: `Alt-r` re-collected the rows but kept serving cached preview content, so editing a tracked file and refreshing still showed the pre-edit diff. It now clears the preview cache too, recomputing the working-tree / log / branch-diff / upstream / summary tabs and re-fetching `pr` / `comments`. Unchanged branches re-read from the content-keyed on-disk cache, so only genuinely changed content pays a recompute. ([#3293](max-sixty/worktrunk#3293)) - **A narrowed `wt list` is now actually faster**: A narrowed `[list] columns` selection (e.g. `["branch", "path"]`) now runs only the git work its columns need. It previously ran every per-worktree `git status`, diff, and ahead/behind walk regardless, then discarded the unselected results — so a trimmed view was no faster than the full table, and on a repo with many dirty worktrees that discarded work was the bulk of the wall-clock cost. ([#3274](max-sixty/worktrunk#3274), thanks @jtaby for reporting) - **Fewer duplicate git calls in `wt list --full`**: The two integration probes per row (the conflict bit for `main-state` and the clean-merge tree for the integration column) issued a byte-identical `git merge-tree`, and the shared default-branch tip was peeled to its tree once per row. Both are now deduplicated through the in-memory cache, so each resolves once per run instead of once per worktree. ([#3288](max-sixty/worktrunk#3288), [#3289](max-sixty/worktrunk#3289)) - **Picker `comments` tab avoids redundant forge fetches**: The picker's `comments` preview tab gained an on-disk cache keyed by the PR's `updatedAt` (which rides for free on the CI fetch the picker already makes), so a repeat `wt switch` skips the per-row `gh pr view --json comments` fetch when the thread is unchanged and paints the tab instantly instead of showing "Loading comments…". The cache is also primed from the `gh pr list` call the picker already makes, so the tab skips its own fetch even on a session's first open (including PRs with no comments). GitHub only. ([#3294](max-sixty/worktrunk#3294), [#3299](max-sixty/worktrunk#3299)) - **Statusline width comes from `COLUMNS`, not a parent-process walk**: `wt list statusline` used to spawn up to 10 `ps` calls plus `stty` per render to recover a terminal width, because Claude Code piped the subprocess with no inherited TTY. Claude Code now sets `COLUMNS`/`LINES` to the terminal dimensions before running the script (since v2.1.153), so the width comes straight from there — less a fixed 5-column margin for Claude Code's own UI — and the `ps`/`stty` walk is gone. On an older Claude Code that doesn't set `COLUMNS`, the line renders untruncated rather than walking the process tree. ([#3286](max-sixty/worktrunk#3286), closes [#2950](max-sixty/worktrunk#2950)) ### Fixed - **`wt list` diff and ahead/behind columns use the upstream default tip**: The `main↕` (ahead/behind) and `main…±` (diff) columns measured every branch against the *local* default-branch tip, so in a fork whose local `main` lagged its upstream they reported inflated counts — one fork branch showed `↑44` and `+∞ / -5K` when it was ~2 commits past the real upstream tip. They now diff against the same upstream-aware base the integration column already uses. ([#3280](max-sixty/worktrunk#3280)) - **Integrated branches no longer flash `✗` in `wt list`**: A squash-merged branch whose default branch later re-edited the same lines showed `✗` (would-conflict), even though `wt step prune` classified it as `⊂` (fully integrated) and removed it. The list now ranks the integration verdict above the downstream conflict, matching prune; a genuinely un-integrated conflict still shows `✗`. ([#3278](max-sixty/worktrunk#3278)) - **`wt list` divergence-overflow marker uses one emphasis level**: When an ahead/behind count overflows its digit budget, the `main↕` column's compact `C`/`K`/`∞` marker rendered the "behind" subcolumn as dim + bold (reading as bold red) instead of a clean one-level step. It now steps exactly one level — dim red → normal red — matching the "ahead" subcolumn, across `wt list` and the picker. ([#3303](max-sixty/worktrunk#3303)) - **Branch deletion on removal is atomic**: `wt remove` and prune now delete a branch with a compare-and-swap (`git update-ref -d <ref> <expected-sha>`) against the SHA the integration check already read, closing the window where a branch whose tip moved in between (e.g. a hook landing a commit) could be deleted. Such a branch is now retained with a clear message and a `wt remove -D <branch>` recovery hint. This also unifies the previously divergent safe-delete paths; explicit force-delete still uses `git branch -D`. ([#2903](max-sixty/worktrunk#2903)) - **`wt list` and picker Age/Message columns paint as soon as the commit batch lands**: These columns carry no async task — their data arrives with the initial `git log` batch — but stayed on the `·` placeholder until some slower task happened to redraw the row, so the commit message lagged behind a cache-warm Summary preview. They now paint the moment the batch lands. ([#3287](max-sixty/worktrunk#3287)) - **Picker `Alt-x` removal: no cursor flash, `--prs` rows preserved**: Removing a row with `Alt-x` flashed the `>` pointer to the top of the list for a frame, and in `--prs` mode made the streamed PR/MR rows vanish until the next `Alt-r`. Removal is now a synchronous in-place pool resync: the cursor lands on the row that slid up with no flash, and the PR/MR rows survive. ([#3268](max-sixty/worktrunk#3268), [#3275](max-sixty/worktrunk#3275)) - **Removable rows stay gray when selected in the picker**: A safe-to-delete worktree (integrated, or clean and even with the default branch) renders its row gray, but the gray vanished under the selection highlight — exactly when you're about to act on it. The gray now survives selection (selected row only; `wt list` and unselected rows are unchanged). ([#3267](max-sixty/worktrunk#3267)) - **Picker preview keeps its scroll when CI status arrives**: Scrolling down a diff and waiting a couple of seconds snapped it back to the top when the live CI fetch landed and re-rendered the pane. The re-render is now precise — a tab re-runs only when its own content would actually change — so a CI update no longer throws away the scroll position of an unrelated tab. ([#3292](max-sixty/worktrunk#3292)) - **Picker summary tab dims when there's nothing to summarize**: The summary preview tab (`5`) stayed lit on a clean branch with no commits ahead, unlike the diff tabs (1/3/4), which dim once their diff is known empty. It now dims in concert with them once both the branch diff and working tree are known empty. ([#3291](max-sixty/worktrunk#3291)) - **Picker default view keeps collect order**: With no query typed, the `wt switch` picker reordered rows by where each name's last `/` falls, so slash-bearing branches (`feature/…`, `perf/…`) sank toward the bottom and intermixed with other row kinds. The default view now preserves collect order. ([#3301](max-sixty/worktrunk#3301)) - **Picker branch-diff preview and summary use the upstream-aware base**: Like the `wt list` columns above, the picker's branch-diff preview pane and the LLM branch summary diffed against the raw local default branch, so a fork whose local default lagged upstream made them describe dozens of already-merged commits. They now use the same upstream-aware comparison base. ([#3305](max-sixty/worktrunk#3305)) - **Picker comment previews render fenced code blocks cleanly**: A fenced code block inside a PR/MR comment rendered as a garbled double gutter in the `wt switch` comments preview — alternating bar/no-bar lines with broken alignment. The code block now renders without the nested gutter. ([#3306](max-sixty/worktrunk#3306)) - **First-run hints show the config path wt actually loads from**: The picker's disabled-summary tab and the commit-generation setup prompt hardcoded `~/.config/worktrunk/config.toml`, so a user with `--config`, `WORKTRUNK_CONFIG_PATH`, or a non-default `$XDG_CONFIG_HOME` was told to edit a file wt never reads. Both now show the resolved path. ([#3290](max-sixty/worktrunk#3290), [#3298](max-sixty/worktrunk#3298)) ### Internal - **`-vv` trace and profiler accuracy**: `trace.log` is now purely human-readable, with the machine-parseable `[wt-trace]` fields living only in `trace.jsonl`; and the performance profile's cache analysis no longer reports stdin-driven commands (LLM `claude -p` calls, `git patch-id`) as duplicate re-runs, since their real input isn't captured in the command string. ([#3296](max-sixty/worktrunk#3296), [#3297](max-sixty/worktrunk#3297)) ## Install worktrunk 0.63.0 ### Install prebuilt binaries via shell script ` See merge request: Harmonybrew/homebrew-core!13092
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 list --fullpeeled the shared default-branch tip to its tree once per worktree/branch row inside the integration probe — N identicalgit rev-parse <sha>^{tree}subprocesses for one value (the largest duplicate-call line in a profiled run).This adds a commit→tree memo to the in-memory
RepoCache(aDashMapget-or-create, mirroringmerge_base_by_sha): theEntrymatch holds the shard lock across check-and-insert, so parallel rows sharing the target spawn one process, not one each. Bothwould_merge_add_to_targetandtrees_match_by_shanow resolve commit→tree through it, so the shared target tree resolves exactly once per run (verified against[wt-trace]records).In-memory rather than the persistent
sha_cache: the resolution is ~1 ms, so cross-run persistence buys little, while the in-memory layer eliminates the cold-run race the disk cache would have. The codebase already had this pattern (merge_base,diff_stats) but the in-memory-vs-disk decision was implicit — so this also documents it in the# Cachingmodule section, with the three exemplars.The one snapshot change is an improvement: the
committed-trees-matchfailure now surfaces the clean--verifyerror (Needed a single revision) instead of the confusingambiguous argument …, matching every otherrev-parsecall site.