Skip to content

perf(web): stop sidebar rows re-rendering their context menu tree - #24107

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
Pearcekieser:perf/perf-sidebar-lazy-row-menus
Oct 28, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
Pearcekieser:perf/perf-sidebar-lazy-row-menus

Conversation

@Pearcekieser

@Pearcekieser Pearcekieser commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A sidebar session row re-renders without re-rendering the Radix context menu tree around it.

Every render of a session row also rendered the row's ContextMenu root, Menu, MenuProvider, Popper, PopperProvider, PopperAnchor and ContextMenuTrigger. The row hands its <div> to ContextMenuTrigger asChild, that element is new on every row render, and each Radix layer re-renders when its children prop changes. PopperAnchor also re-attaches its composed ref on each of those renders. The menu content was already memoized, so the menu showed nothing new.

Why it matters

Rows re-render while background sessions stream and whenever a row moves. In the render benchmark's s1x5 scenario, 186 of 192 row renders also rendered the menu tree, and MenuProvider, ContextMenu, Popper and Menu were the four highest Radix render counts in the run.

Not a goal

  • Folder header context menus. Their menu items are rebuilt inline on every header render, so a stable trigger alone saves nothing there.
  • The transcript's MessageContextMenu and per-message dropdowns, which are a separate render path.
  • Mounting the Radix menu only on hover or open, which was the first plan. Inserting the Radix layers on hover would move the row element under new parents, which remounts it and drops focus and any in-flight right-click or long-press. The stable trigger keeps the mounted menu and stops its re-renders instead.

What changed (motivation → approach → change)

Nothing changes on screen. A session row re-render no longer re-renders the row's right-click menu, which cuts the sidebar's render work while sessions stream (7920 component renders instead of 9780 in the benchmark below).

ContextMenuStableTrigger in components/ui/context-menu.tsx builds <ContextMenu><ContextMenuTrigger asChild>…</ContextMenuTrigger>{content}</ContextMenu> once per change of content. The trigger's child is one constant outlet element. The outlet reads the caller's current element from context and renders it through Radix Slot, which merges the trigger's props and ref onto it the same way asChild does. A new row element now re-renders only the outlet. SessionRow uses it in place of the plain ContextMenu and ContextMenuTrigger pair. The DOM and the handler order on the row are unchanged.

website/package.json adds @radix-ui/react-slot as a direct dependency at 1.3.0, the version the menu packages already install. website/package-lock.json gains only that root entry.

Backwards compatibility

Compatible: ContextMenu and ContextMenuTrigger are unchanged, and ContextMenuStableTrigger is a new export.

Removes nothing: the row renders the same element with the same handlers, ref and menu content.

Tests

  • ContextMenuStableTrigger.test.tsx pins right-click opening the menu while the element's own onContextMenu and onPointerDown still run. It checks that a re-render updates the same DOM node without detaching its ref and that the menu still opens afterwards. It also checks that Escape on a keyboard-opened menu returns focus to the element.
  • ChatSidebar.rowMemo.test.tsx now counts Radix ContextMenu.Root renders when a top insertion displaces 20 rows. It expects 1, for the new row only. On main it counts 21.
  • Determinism: both files ran in 5 shuffled orders with seeds 1007 to 5007, 14 of 14 tests green each time.
  • vitest related on the two changed source files ran 987 files and 15,081 tests, all green.

Proof: not provable -- frontend test (prove.py runs Python only). The rowMemo assertion was run against main's SessionRow.tsx and failed with 21 against 1.

Manual verification

Render benchmark from #23431 (scripts/bench-render.mjs), applied locally on main 5631aab without and with this change. Scenario s1x5, desktop, 30 s, 3 runs per side. All 3 runs on each side gave the same counts.

s1x5 renders main this PR
MenuProvider 662 290
ContextMenu 504 132
Popper 332 146
PopperAnchor 332 146
Menu 331 145
ContextMenuTrigger 252 66
SessionRow 192 192
all components 9780 7920
React commits 189 189

Session rows now cause 0 menu renders. The perf plan's target was an 80% drop in Menu, Popper and ContextMenu renders in this scenario, measured from the original baseline before the slot-list change (#23877): Menu 617, Popper 652, ContextMenu 732. This PR reaches ContextMenu -82%, Menu -76% and Popper -78%, so two of the three stop short of 80%. What remains comes from other owners, not session rows. The ContextMenu wrapper's 66 renders that remain come from MessageContextMenu (42) and folder headers (24). Menu's 145 are those 66 plus 79 dropdown menus, mostly on transcript messages.

Screenshots / video

Why no screenshot: the row renders the same DOM and the same menu, only fewer times.

Related Issues

Fixes #24106

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@Pearcekieser
Pearcekieser requested review from a team and KiroCrewCaptain as code owners October 25, 2026 20:17
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Oct 25, 2026
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed b259294c86efeee81f7ffbe16d92f3d4757a19f2 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] b259294

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of b259294c86efeee81f7ffbe16d92f3d4757a19f2 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the trigger fix removes the re-render at its cause, renders the same DOM, and adds an already-hoisted dependency.

Notes

  • ContextMenuStableTrigger only saves work if content is memoized, and nothing enforces that. Any later caller that passes inline content quietly loses the benefit, so the rowMemo contextRoot pin is the only guard.

[DESIGN-REVIEWED] b259294

Description read

[DESCRIPTION-READ] a87384d92add64426a40470b261497cef144bb923d15ebf337257d580cf0d7b8

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ✅ PASS

UX-level review of b259294c86efeee81f7ffbe16d92f3d4757a19f2 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

UX-Verdict: PASS

Nothing to check. This is a render-performance refactor: the session row keeps the same DOM, handlers and right-click menu, and the diff adds or changes no user-visible control, label or state.

[UX-REVIEWED] b259294

Description read

[DESCRIPTION-READ] 4cd68dae1b7ad108e4e278cca002b5408e89f4d17f1545ce12403d47f2a03b15

That sha256 names everything this verdict read: the PR description, plus the 3 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of b259294c86efeee81f7ffbe16d92f3d4757a19f2 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing to check.

What this change ships

Inventory (5 items) — 5 justified

Intent: keep the sidebar responsive while sessions stream by stopping each row re-render from also re-rendering its right-click menu tree. This is a FIX (perf).

  1. A session row re-render no longer re-renders the Radix context-menu tree around it — justified
  2. New shared ContextMenuStableTrigger export, used by SessionRow in place of the plain ContextMenu/ContextMenuTrigger pair — justified
  3. @radix-ui/react-slot@1.3.0 becomes a direct dependency; the lockfile already installs it at 1.3.0 for the menu packages — justified
  4. The row-memo test now counts Radix ContextMenu.Root renders and expects 1, not 21, when 20 rows are displaced — justified
  5. New ContextMenuStableTrigger tests: right-click, own handlers, ref kept across a re-render, focus returned after Escape — justified

[FIRST-PRINCIPLES-REVIEWED] b259294

Description read

[DESCRIPTION-READ] bfb30476130967db7c6c734a34c35ddc573e4e29ba0b21861c1da08b189fdbc0

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed b259294c86efeee81f7ffbe16d92f3d4757a19f2 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] b259294

@Pearcekieser
Pearcekieser force-pushed the perf/perf-sidebar-lazy-row-menus branch from 7952c43 to f7a8f85 Compare October 25, 2026 21:46
@Pearcekieser

Pearcekieser commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor Author

span=first-principles:website/src/components/ui/context-menu.tsx
suggestion (non-blocking): drop the disabled prop from ContextMenuStableTrigger

Disposition: fixed in f7a8f85.

The prop, its useMemo dependency and the test that set it are gone. SessionRow, the only caller, never passed it.

@Pearcekieser

Copy link
Copy Markdown
Contributor Author

note (non-blocking): the two reds on 7952c43 were not code findings. Backend Tests (3.12, 3) was cancelled by the runner mid-step ('The operation was canceled') on a frontend-only diff, and Opus 5.5 Review ended as 'review incomplete' with no findings posted. The push of f7a8f85 re-runs both.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 25, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Request changes at f7a8f85.

The fix looks sound: SessionRow already memoizes its menu content, so the memoized Radix tree with a constant Slot outlet removes the menu re-renders without changing the DOM or handlers, and CI is green.

Two items need an answer before approval:

  1. The PR body says folder header context menus are left as a follow-up, and your disposition refers to that follow-up, but no issue tracks it (#24106 covers session rows only). Please open an issue for it and link it in the description, or drop the follow-up wording if no follow-up is planned.
  2. The Design Review note on this head (comment) asks that the JSDoc state content must be referentially stable, since an inline content silently loses the saving (context-menu.tsx L66-L77). It has no reply yet. A one-line JSDoc addition or a reply would clear it.

— crew-pr-reviewer (r126)

@Pearcekieser

Copy link
Copy Markdown
Contributor Author

note (non-blocking): folder-header follow-up wording dropped

The description no longer calls the folder header menus a follow-up. That bullet in Not a goal now only says they are out of scope and why: their menu items are rebuilt inline on every header render, so this wrapper alone saves nothing there. No issue is planned from this PR. The earlier First Principles disposition mentioned the folder-header work only as an example of a future caller for disabled, and that sentence is superseded by this one.

@Pearcekieser

Pearcekieser commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor Author

span=design:website/src/components/ui/context-menu.tsx
suggestion (non-blocking): say in the JSDoc that content must be referentially stable

Disposition: fixed in 99b456f, which superseded the rebuttal first posted here.

The ContextMenuStableTrigger JSDoc now says to pass a referentially stable content memoized by the caller, because a new element on every render rebuilds the tree and saves nothing.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 26, 2026
@Pearcekieser
Pearcekieser force-pushed the perf/perf-sidebar-lazy-row-menus branch from f7a8f85 to 99b456f Compare October 26, 2026 22:57
@Pearcekieser

Copy link
Copy Markdown
Contributor Author

note (non-blocking): both review items are addressed on 99b456f

The first item, the folder-header follow-up wording, came out of the description at 03:14Z. The folder headers are now listed only as out of scope, with the reason.

The second item is fixed in 99b456f. The ContextMenuStableTrigger JSDoc now says to pass a referentially stable content memoized by the caller, because a new element on every render rebuilds the tree each time and saves nothing. The change is those two comment lines and nothing else. This supersedes my earlier rebuttal of the Design note.

@Pearcekieser

Copy link
Copy Markdown
Contributor Author

question (non-blocking): could a maintainer re-run the fork GPT 6.1 and Opus 5.5 review lanes on 99b456f?

Both check runs ended as "review incomplete" about two hours ago, and neither lane's comment has been updated past f7a8f85. The only change since that head is the two JSDoc lines in website/src/components/ui/context-menu.tsx that the review asked for. Both lanes reported no findings on f7a8f85.

@Pearcekieser

Copy link
Copy Markdown
Contributor Author

note (non-blocking): rebased on main, no behaviour change. b259294 is 99b456f on current main. No review lane had run on 99b456f, and main had moved 322 commits, including a class-name and rename-ref change in SessionRow.tsx that this diff does not touch. tsc is clean and the four menu test files pass, 30 of 30.

@Pearcekieser
Pearcekieser force-pushed the perf/perf-sidebar-lazy-row-menus branch from 99b456f to b259294 Compare October 27, 2026 13:48
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 27, 2026
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention labels Oct 28, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving b259294.

Both items from my r126 review are resolved. The description no longer promises a folder-header follow-up; it lists those menus as out of scope with the reason. The ContextMenuStableTrigger JSDoc now says content must be referentially stable (context-menu.tsx L41-L79).

The approach is sound. The Radix tree is memoized on the row's already-memoized menu content, and the changing row element reaches the trigger through a constant Slot outlet, so the DOM, handlers and ref stay the same (SessionRow.tsx L1125). The new direct dependency @radix-ui/react-slot 1.3.0 is the version already installed. All fork AI lanes pass at this head with no open concerns, and CI is green (PR Readiness success).

— crew-pr-reviewer (r133)

@iamwhatever
iamwhatever enabled auto-merge (squash) October 28, 2026 16:53
@iamwhatever
iamwhatever merged commit ed694b8 into kirodotdev:main Oct 28, 2026
106 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(web): sidebar rows re-render their context menu tree on every row render

2 participants