Skip to content

perf(web): typing, scrolling and switching threads re-render fewer components - #44833

Open
maria-rcks wants to merge 6 commits into
mainfrom
t3/optimize-ui-redraws-bench
Open

maria-rcks wants to merge 6 commits into
mainfrom
t3/optimize-ui-redraws-bench

Conversation

@maria-rcks

@maria-rcks maria-rcks commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Recording, 1m32s: main on the left and this PR on the right run the same scripted session (open three threads, type, scroll, twice, then watch a streaming thread), each with a counter of React component renders; components flash purple when they re-render; after a typed key main shows 22 renders and this PR 4, and at the end main is at 53,454 renders and 1,426 commits and this PR at 43,434 and 1,253

Typing, scrolling, opening Settings and switching threads re-rendered components that had nothing new to show. Each typed character re-rendered 21 components and now re-renders 4. Scrolling the timeline re-renders 22% fewer, Settings 28% to 33% fewer, closing the terminal 22% fewer, thread switches 2% to 12% fewer. Nothing on screen changes. The causes are three, each a few lines:

  • The composer footer read the prompt inline for the alternate send shortcut label, so React Compiler made the prompt a dependency of the whole footer and every character rebuilt it, voice controls included. The label is now resolved before the JSX.
  • ChatView subscribed to the whole timeline follow state, so every atEnd flip while scrolling re-rendered it. Only the mode is rendered, so that is now the snapshot. scrollToEnd leaves the hook stable like its other callbacks.
  • The pending branch setter was a new function on every ChatView render and shared a memo scope with the details panel and overlay hosts, rebuilding them each time. It now closes over a thread id read beside it, which the compiler caches on; a checkout that resolves after a thread switch still writes its own thread.

A fourth cause, the sidebar's row click handler, was part of this PR until main landed the same fix in #47105. The rebase onto 3067c498471 dropped it from this diff, which is why switches gain less here than the 7% to 32% first measured against b927b1fd936.

report:client --renders now also counts renders in its two 10 s streaming windows, where nothing is clicked or typed, and its probe walks past 800 hook slots (ChatView has over 1,200, so its later state changes read as context). compare leaves out the CPU of a window that counted renders, since that CPU includes the probe.

Measured on 2026-12-03 with vp run report:client ab <main checkout> <this checkout> --rounds 3 --renders --cases short,long,working,medium,approval,scroll-long,settings,terminal --floors floors.json: production builds, Chromium headless shell 158, 16-core AMD EPYC 7B13 Linux VM, pinned to 8 cores, runs alternated. Floors come from main against main the same day (noise). Values are component renders per step, each run's median one sample, 3 runs a side. A row is better only when the 95% interval of the difference clears both 5% and that metric's base-against-base floor. These are the tool's render rows, unedited:

Client metrics (lower is better)

before 3067c498471 (3 runs), after b665b453862 (3 runs)

metric before after change 95% interval
step.scroll-long.wheel-up.renders 684.5 537 -22% -160.5 to -123.5
step.settings.back.renders 434 290.5 -33% -144.5 to -143.5 better
step.settings.open.renders 696 502.5 -28% -194.5 to -86.5
step.terminal.close.renders 183.5 144 -22% -56.5 to -30.5 better
step.terminal.open.renders 242.5 194 -20% -108.5 to 32
switch.approval.first.renders 1274 1147 -10% -161 to -109
switch.approval.revisit.renders 1233 1137 -8% -96 to -78
switch.long.first.renders 2181 2037 -7% -146 to -110
switch.long.revisit.renders 1826 1730 -5% -103 to -89
switch.medium.first.renders 1786 1642 -8% -209 to -109
switch.medium.revisit.renders 1698 1591 -6% -113 to 300
switch.short.first.renders 1228 1084 -12% -155 to -115
switch.short.revisit.renders 1187 1096 -8% -115 to -77
switch.working.first.renders 2614 2410 -8% -576 to 6
switch.working.revisit.renders 2830 2769 -2% -154 to -16

The typing rows come from a run of every case the same day, 2 runs a side:

metric before after change 95% interval
step.type-long.key.renders 21.3 4 -81% -17.5 to -17 better
step.type-short.key.renders 21 4.5 -79% -17 to -16 better

No render row is worse. That run of every case ran at load average 40 to 50 and flagged three: palette open, the right panel switching to the pull request, and revisiting the pull request thread. Those steps have several modes on main itself (palette open reads 590, 899 or above 1,500; the panel switch about 1,000 or 1,400 to 2,600), depending on whether a late update lands inside the step, which it does more often on a busy machine. Run alone they do not differ: palette open has a median of 899 on main and 822 here over 4 runs a side; the panel switch 1,803 against 1,690 and the pull request revisit 3,057 against 3,386, both intervals across zero (3 runs against 2). The streaming windows are inconclusive: across the day's runs an idle thread read 142 to 866 renders per 10 s on main and 311 to 1,200 here, and watching the stream 3,400 to 6,700 on both.

The recording is one pass a side, recorded back to back on the same cores, so its totals show direction and the tables are the claim.

Timing, CPU and memory were measured on the head before the rebase (0b81adbc677c against b927b1fd936), without the render probe, at load average 4 to 7: no row worse, one better (switch.diff.first.loafMs 349 to 261 ms), a typed key painting in 32 ms with no long frame on both sides, renderer CPU and heap equal within their intervals, scroll long-frame time 109 ms against 110 ms over 24 samples a side. The machine was too busy on 2026-12-03 to repeat the timing run on the rebased head. Memory in that day's runs is unchanged: heap after every round 76.2 MB on main and 75.9 MB here, DOM nodes identical.

Proof, by level (4 is real code executed, 5 is the running production build):

  • Render counts: level 5, the tables above.
  • Behavior the changed handlers own, scripted in the production build on main and on this PR before the rebase, with the same results: scrolling up shows Scroll to end, and Scroll to end returns to the end (gap 0 px) and hides. Level 5.
  • The compiled output of the three files on the rebased head: the composer footer scope has 39 dependencies and prompt is not one, and the branch setter is cached on the thread id. Level 4.
  • vp run check:changed on Blacksmith for this head: format, lint, knip, typecheck, 9 test files with 146 tests, React Compiler coverage of the 3 files.

Not verified: frame rate on a display (headless Chromium only); timing on the rebased head; the alternate send shortcut label in the UI (same computation, moved; checked in the compiled output only); a branch checkout that resolves after a thread switch in the running app (traced in code by two reviewers, level 3); pending guest messages and resume scrolling to the end; the Electron shell; any gain while a thread streams (inconclusive). Mobile is untouched: it is separate React Native code with its own render counter, and this pass did not look at it.

Council review

Rounds: 4

Delegate Round 1 (4d5ee1b) Round 2 (0b81adb) Round 3 (d4bd5c2, rebased) Round 4 (b665b45)
GPT Sol (high; 6.1, then 6.2 from round 3) 7/10, one should: the branch setter could write the wrong thread after an awaited checkout. Fixed in f55c5ef. 9/10, no findings 8/10, one should: compare gated the CPU of a window that counted renders, which includes the probe. Fixed in b665b45. 9/10, one nit: the summary still prints that CPU under the same heading. Declined: the doc says so, and compare and noise no longer use it.
Claude Opus 5.5 (high) 8/10, one should: the probe now runs inside the idle CPU window. Kept and documented. 9/10, one nit: streaming-window counts follow the stream rate. Stated above. not run not run
Gemini 4 Neon (high) unavailable, quota exhausted unavailable 8/10, the same should as Sol. Fixed in b665b45. 9/10, the same nit. Declined.

Fable 5.2 via Claude Code in T3 Code.

🤖 Generated with Claude Code

@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Nov 28, 2026
@maria-rcks
maria-rcks marked this pull request as ready for review November 28, 2026 11:46
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Chat components update thread, timeline, and alternate-send shortcut handling. The web metrics harness captures render data during idle CPU sampling and includes idle render counts in metric summaries. The render probe scans more hook slots when identifying state changes and recording slot differences.

Changes: Chat interaction updates

Layer / File(s) Summary
Thread and timeline handling
apps/web/src/components/ChatView.tsx, apps/web/src/components/chat/useTimelineFollow.ts
Pending branch updates use the thread ID captured when the setter is created. Timeline-follow subscribes to a derived following state and returns the stable scrollToEnd handler.
Alternate-send shortcut calculation
apps/web/src/components/chat/ChatComposer.tsx
ChatComposer calculates the alternate-send shortcut label from the existing keybinding and composer context before rendering the footer.

Streaming render metrics

Layer / File(s) Summary
Render capture during idle CPU sampling
scripts/metrics/web/report.ts, scripts/metrics/web/probes.ts, scripts/metrics/web/client-metrics.ts
The Idle schema can store render data. The idle CPU sampler brackets its sampling window with the optional render probe. The probe scans up to 4,000 hook slots when identifying state changes and recording slot differences.
Idle render aggregation and summary
scripts/metrics/web/report.ts, scripts/metrics/web/client-metrics.test.ts, scripts/metrics/web/client-metrics.ts, docs/internals/16_performance-regressions.md
Metric flattening includes idle-window render and commit counts. Render summaries include idle records labeled as streaming. The test checks the streaming row, and the command description and documentation cover render counts during streaming CPU sampling windows.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant clientMetrics
  participant idleCpu
  participant harness
  participant renderProbe
  clientMetrics->>idleCpu: Pass harness for idle CPU sampling
  idleCpu->>harness: Start optional render probe
  idleCpu->>harness: Sample CPU during streaming window
  harness->>renderProbe: Capture render data
  idleCpu->>harness: Stop optional render probe
  idleCpu-->>clientMetrics: Return idle sample with render data
Loading

Suggested reviewers: juliusmarminge

Merge Risk: Minimal · up to b665b

This change reduces unnecessary re-rendering in the chat UI and extends the performance metrics tooling. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
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.
Title check Passed The title names the main change: web re-renders drop for typing, scrolling, and thread switching. The description and objectives both state this as the goal. The title is specific and follows conventi…
Description check Passed The description follows the template. It has Problem, Change, Scope, and Verification sections, and each contains specific content. The Problem section states the expected behavior and the cause. The …
Full details: Title check

Explanation

The title names the main change: web re-renders drop for typing, scrolling, and thread switching. The description and objectives both state this as the goal. The title is specific and follows conventional commit format.

Full details: Description check

Explanation

The description follows the template. It has Problem, Change, Scope, and Verification sections, and each contains specific content. The Problem section states the expected behavior and the cause. The Change section explains each of the three fixes and why they are needed. The Scope section explains the rebase and the removed sidebar fix, but it does not link an issue or state that a maintainer requested the PR. Verification gives measured tables, test counts, and a list of unverified areas. The Problem section does not give explicit reproduction steps, and the author omits the agent model line only if it is not required. These gaps are minor, so the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d4bd5c2 and b665b45.

Files selected for processing: 3

  • docs/internals/16_performance-regressions.md
  • scripts/metrics/web/client-metrics.test.ts
  • scripts/metrics/web/report.ts
Files skipped from review as they are similar to previous changes: 1

  • docs/internals/16_performance-regressions.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Copy link
Copy Markdown

When switching from A to B and back, did you measure whether A’s timeline and scroll container were rebuilt? That would help distinguish the cost of remounting a timeline from the cost of re-rendering its rows.

We tried retaining the two most recent timelines in a separate macOS prototype. When we ported it to stock source, one regression run found only one mounted timeline where we expected two. We haven’t benchmarked this PR or established a speedup on stock T3.

Would a separate issue with a synthetic reproduction of the remount cost be useful?

…r components

The composer footer read the prompt inline, so every typed character rebuilt the footer and its voice controls. The sidebar's row click handler changed on every route, so a thread switch re-rendered every row. ChatView subscribed to the whole follow state, so each atEnd flip while scrolling re-rendered it, and a handler that was new on every render rebuilt the details panel and overlay hosts with it.
…or typed

With --renders, the two 10 s windows that sample renderer CPU while a thread streams now also count React commits and component renders, so background redraws are measured and gated like interactions. The probe also walks past 800 hook slots, which had reported ChatView's later state changes as context.
…g windows

Covers the new row in the existing summary test, the --renders help text and the performance doc.
…s its own thread

The stable wrapper ran the latest render's setter, so a checkout started on one empty thread and resolved on another put its branch on the second. The setter now closes over a thread id read beside it, which the compiler caches on, so it is still the same function between renders.
The row key held the measured seconds, which could round a slow window into a second row. The doc says the windows' CPU includes the probe under --renders.
@maria-rcks
maria-rcks force-pushed the t3/optimize-ui-redraws-bench branch from 0b81adb to d4bd5c2 Compare December 3, 2026 04:32
macroscopeapp[bot]
macroscopeapp Bot previously approved these changes Dec 3, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at b665b45

Macroscope's review found this PR approvable — This reduces unnecessary chat UI re-renders and improves streaming performance measurements. The changes preserve existing product behavior and introduce no apparent material data or operational exposure.

You can add or adjust custom eligibility rules. Learn more.

@macroscopeapp
macroscopeapp Bot dismissed their stale review December 3, 2026 04:40

Dismissing prior approval to re-evaluate b665b45

@maria-rcks maria-rcks changed the title perf(web): typing, scrolling and switching threads re-render far fewer components perf(web): typing, scrolling and switching threads re-render fewer components Dec 3, 2026
@maria-rcks

Copy link
Copy Markdown
Collaborator Author

Note

Written by claude-fable-5-2 on behalf of Maria

Yes. report:client --renders measures a thread's first visit and its revisits separately and gives every render a cause, so mounts can be told apart from re-renders. From a run of this PR's head on 2026-12-03 (unminified production build, headless Chromium):

switch renders of which mount
long thread, first visit 2,037 534
long thread, revisit 1,725 499
short thread, revisit 1,062 61

So the web app does rebuild the timeline on a revisit (one timeline is mounted at a time), and on the long thread that is about 29% of the renders. The rest are components that were already on screen rendering again, which is the part this PR and #48244 reduce. Neither changes what is mounted or kept.

These are render counts, not time. I have not measured how many milliseconds of a revisit the remount accounts for, and nothing here says whether keeping a second timeline mounted would pay for its memory.

Proposals from outside the team go to Ideas discussions (see CONTRIBUTING.md). A reproduction that isolates the remount cost in time, on stock source, would be the useful thing to bring there.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants