Skip to content

perf(renderer): drop redundant worktree card cache-TTL subscription - #13903

Merged
nwparker merged 1 commit into
mainfrom
nwparker/perf-agent-status-card-foundation
Aug 12, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/perf-agent-status-card-foundation

Conversation

@nwparker

@nwparker nwparker commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

The finding matters more than the code

This PR was originally scoped to bundle use-worktree-card-foundation.ts's 26 store subscriptions into one useShallow projection — 26 listeners per card down to 1, across ~100 mounted cards.

I measured it before writing it. It is a 4× pessimization. So this PR does not do it.

150 cards x 400 unrelated store writes, median of 5 runs, real store + React:

Approach Time
26 separate useAppStore hooks (today) 100.7 ms
1 hook + useShallow bundle 402.5 ms (4.00x worse)
1 hook + non-allocating keyed compare 141.5 ms (1.41x worse)

A vanilla-zustand sweep at 2/5/8/12/20/26 keys showed useShallow losing at every key count. There is no crossover to aim for.

Mechanism — zustand v5's shallow() on plain objects falls through to compareEntries, which allocates two Object.entries() arrays and two Maps per comparison, per subscriber, per store write:

// node_modules/zustand/vanilla/shallow.js:9
const mapA = valueA instanceof Map ? valueA : new Map(valueA.entries())
const mapB = valueB instanceof Map ? valueB : new Map(valueB.entries())

Measured in isolation against zustand 5.0.14: 721 ns for a 2-key bundle, 2,152 ns for 9-key — versus ~1-5 ns for the equivalent Object.is sweep. Even a perfect zero-allocation compare still loses, because the bundle must rebuild the whole projection on every notification while separate selectors short-circuit on the first Object.is hit.

Listener count is the wrong metric. It would have improved 26 -> 1 here while the hot path got 4x slower. Two sibling branches that did land this transform measured 1.3-1.4x regressions on their own target metric; both are being dropped on the strength of this result.

What actually ships

use-worktree-card-secondary-details.ts no longer opens its own subscription for promptCacheTtlMs — it derives it from foundation.settings, which the same component already subscribes to. 11 -> 10 listeners per card.

Staleness is impossible via two independent paths: use-worktree-card-foundation.ts:36 subscribes s.settings unconditionally, and usePromptCacheCountdownStartedAt's useShallow tuple already includes ttlMs. settings identity always changes on write — no in-place mutation at settings.ts:133/160 or ui.ts:2155.

Honestly: this is ~1 listener out of ~30 per card, well under 1% of sidebar notification cost. The measurement is the deliverable.

Validation

typecheck:tsc:web exit 0 (confirmed via --listFilesOnly that the new test is inside the web tsconfig program, so its casts are genuinely typechecked). Sidebar suite: 225 files / 2,049 tests pass. check:code-quality:changed 0 new findings. check:zustand-selector-fanout: 0.0146 ms/write, 0 render invalidations.

Guard proven to bite by double revert: reverting the change fails 1 test in hybrid form and 2 of 3 in pure form.

One caveat: the third test ("reports no TTL while suppressed") passes identically on main — it's a harmless behavior lock, not a regression guard.

Caveat on the numbers

Taken on macOS at load average ~24. Ratios and the deterministic sweep survive that; absolute milliseconds are inflated and shouldn't be quoted as app-level figures.

Made with Orca 🐋

promptCacheTtlMs was a third store subscription for a field the card
already reads via foundation.settings; derive it instead.

Does NOT bundle the 26 foundation selectors as originally planned.
Measured on this store with React (150 cards, 400 unrelated writes,
26 keys): 26 separate useAppStore hooks 100.7ms; one useShallow bundle
402.5ms (4.0x worse); one bundle with a non-allocating keyed compare
141.5ms (1.4x worse). zustand v5 shallow() rebuilds two Maps from
Object.entries per compare, and a bundle must still build the whole
projection every notification, so collapsing cheap selectors only
trades away useSyncExternalStore dispatches that are cheaper than the
object it allocates. Listener count would have improved 26 -> 1 while
the hot path got slower.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e942b0ce-8221-4956-ab5a-8342230cf1d5

📥 Commits

Reviewing files that changed from the base of the PR and between 96c17b4 and 57e6be7.

📒 Files selected for processing (3)
  • src/renderer/src/components/sidebar/use-worktree-card-controller.ts
  • src/renderer/src/components/sidebar/use-worktree-card-secondary-details.store-subscriptions.test.tsx
  • src/renderer/src/components/sidebar/use-worktree-card-secondary-details.ts

📝 Walkthrough

Walkthrough

The secondary-details hook now receives settings from the worktree card controller. It derives the prompt-cache TTL from these settings instead of subscribing to the app store. The TTL remains zero when the aggregate cache timer is disabled. New tests cover store listeners, rerenders, TTL updates, and compact-card behavior.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the implementation and validation, but it omits the required template sections for screenshots, testing checkboxes, AI review, security audit, and notes. Add the missing template sections and document testing results, cross-platform review, security risks, and any platform-specific behavior or follow-up work.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: removing the redundant worktree card cache-TTL subscription.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker
nwparker merged commit 3d4c968 into main Aug 12, 2026
45 checks passed
@nwparker
nwparker deleted the nwparker/perf-agent-status-card-foundation branch August 12, 2026 05:13
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#13903)

promptCacheTtlMs was a third store subscription for a field the card
already reads via foundation.settings; derive it instead.

Does NOT bundle the 26 foundation selectors as originally planned.
Measured on this store with React (150 cards, 400 unrelated writes,
26 keys): 26 separate useAppStore hooks 100.7ms; one useShallow bundle
402.5ms (4.0x worse); one bundle with a non-allocating keyed compare
141.5ms (1.4x worse). zustand v5 shallow() rebuilds two Maps from
Object.entries per compare, and a bundle must still build the whole
projection every notification, so collapsing cheap selectors only
trades away useSyncExternalStore dispatches that are cheaper than the
object it allocates. Listener count would have improved 26 -> 1 while
the hot path got slower.
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.

1 participant