fix(focus-history): resolve each history entry once per menu snapshot
focusHistoryMenuSnapshot resolves every history entry three times. The compactMap calls resolvedFocusHistoryEntry(for:), then focusHistoryEntryIsNavigable resolves the same entry again, and that calls focusHistoryEntryResolvesToCurrent, which resolves it a third time. Each resolution runs a workspaceExists check plus resolvedFocusHistoryPanelId, which reaches PaneTreeModel.panelId(forSurfaceId:) through the host.
Against the 50-entry cap across both directions, one History menu build costs up to 300 host resolutions to render 10 rows. The History CommandMenu rebuilds on every App body evaluation rather than on open (Sources/cmuxApp+HistoryMenu.swift:9-11), so this is paid continuously, not per-open.
Found while profiling an idle app holding 48-56% of one core: #10348.
Change
Add an overload of focusHistoryEntryIsNavigable that takes the already-resolved entry plus the hoisted scope, and call it from the menu compactMap. The existing single-argument version delegates to it, which also drops one redundant resolution for its five other callers.
focusHistoryEntryResolvesToCurrent had no other caller, so its two lines are folded into the overload.
The snapshot output is unchanged. Only the number of host lookups moves.
Equivalence, since the overload skips two guards the old path ran:
- The
resolvedFocusHistoryEntry(for:) != nilguard is subsumed by the caller already holding a non-nilresolvedEntry. focusHistoryEntryResolvesToCurrentreduced toresolvedEntry == currentEntrywhencurrentEntryis non-nil, which is what the overload does inline.navigationScope()is a closure property read once per snapshot; it cannot change mid-compactMap, so hoisting it is safe.
Verification
Two commits, red then green, per the regression policy in CLAUDE.md.
The test counts workspaceExists calls on the fake host across one snapshot of 9 navigable entries:
before: Expectation failed: (host.workspaceExistsCalls → 27) == 9
after: ✔ Test menuSnapshotResolvesEachEntryOnce() passed
swift test in Packages/macOS/CmuxWorkspaces: 209 tests in 30 suites pass.
I verified at package level only. I did not run a tagged Xcode app build; the change is confined to two private methods inside CmuxWorkspaces and adds no new user-facing strings, so there is no localization surface.
What this does not fix
This trims one hot path, not the loop in #10348. The App body still re-evaluates continuously and still rebuilds the whole History menu each time, and AppDelegate.makeMainMenu still calls ViewGraphRootValueUpdater.invalidateProperties inside the same update. Those need someone who knows which publisher is driving the App body; I could not name it from a stripped release build.
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Resolve each focus history entry once per History menu snapshot to remove redundant host lookups while keeping the snapshot identical. Previously the menu resolved each entry up to three times; now it resolves once, reducing continuous CPU cost because the History CommandMenu rebuilds on every App body evaluation.
- Add an overload of
focusHistoryEntryIsNavigablethat takesresolvedEntryand hoistedscope, and call it from the menucompactMap; the existing method delegates to it. - Inline and remove the unused
focusHistoryEntryResolvesToCurrent; compare againstcurrentEntrydirectly. - Hoist
navigationScope()to one read per snapshot. - Add a regression test that counts host
workspaceExistscalls: for 9 navigable entries, 27 → 9. - No public API or menu output changes; changes are confined to private code in
CmuxWorkspaces.
Summary by CodeRabbit
-
Bug Fixes
- Improved back-navigation menu generation to reduce redundant workspace lookups and keep entries consistent.
- Ensured history menu building uses pre-resolved context to prevent repeated resolution during snapshot creation.
-
Tests
- Added a regression test verifying that a back-navigation menu with ten history entries displays nine entries.
- Confirmed each workspace is checked exactly once during menu snapshot generation.