Skip to content

perf(web): stop re-rendering the page on every MCP message - #2931

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2928-batch-message-renders
Nov 5, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2928-batch-message-renders

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2928

Root cause

A CPU profile of a sidebar-open load run, plus a commit counter on React's devtools hook, turned up four per-message costs. The first is shared by both cases:

  1. App re-rendered synchronously on every log event. App subscribes to the message, fetch and stderr logs. Each log dispatches once per entry, and useSyncExternalStore renders synchronously on every notification. The client's connection diagnostics (connectionDiagnosticsChange) are re-dispatched on every request and every response, so each tools/call rendered the whole page about four times. That was the closed-sidebar ceiling of ~68/s.
  2. Every Protocol and Network row re-rendered on every message. Nothing in the list was memoized, so the cost of one message grew with the size of the log.
  3. Every new row mounted its collapsed body. Mantine's Collapse keeps its children in the tree. A compact Protocol row therefore mounted two read-only Ace editors and a closed edit-and-replay Modal as soon as it appeared.
  4. The Protocol/Network correlation re-parsed every fetch body on every render. It runs JSON.parse over the whole fetch log once per App render.

Fix

  • Coalesced notifications. useStoreSnapshot takes an optional NotifyScheduler (core/react/notifyScheduler.ts). The web client passes animationFrameScheduler for the three logs and for connection diagnostics, so any burst costs one render per frame. Only the signal is deferred: the hooks still re-read the live store, so no entry is dropped or reordered, and a render that happens sooner for another reason already sees the latest list. Other useInspectorClient values (status, capabilities, …) stay immediate. The TUI is unchanged because the parameter is optional.
  • Memoized rows. ProtocolEntryRow and NetworkEntryRow are wrapped in memo. The parent's handlers are new functions on every render, so they now reach the rows through useLatestCallback stand-ins. To make entry identity a sound memo key, MessageLogState now replaces an entry with an updated copy when it folds in a response, a rejection or a send failure, instead of mutating it. FetchRequestLogState already worked this way. The AGENTS.md and TypedEventTarget notes that used the old mutation as their example are updated, and the revision-counter rule they state is unchanged.
  • Bodies mount on first open. useOpenedOnce gates the Protocol and Network detail bodies and the edit-and-replay modal. Once mounted they stay mounted, so collapsing still animates and an editor keeps its state.
  • Parses cached per entry. In correlateTransportErrors the parses are memoized in a WeakMap keyed by entry. Fetch entries are immutable, so a replaced entry is parsed fresh.

Measurements

Method: the same throwaway Playwright script for both builds. It runs the prod web server (minified vite build) and the composable modern-http config (echo over Streamable HTTP), then drives Tools → echo → Load test at concurrency 5 with no rate limit, two repetitions each. Headless Chromium, 1280×900, same machine.

Setup Before (origin/v2/main) After
Sidebar closed, 4 s 68.1 / 68.9 calls/s, p50 ~68 ms 1,300 / 1,382 calls/s, p50 ~3 ms
Sidebar open (Protocol tab), 4 s 10.8 / 11.0 calls/s, p50 ~430 ms 150 / 152 calls/s, p50 ~32 ms
Sidebar open, 12 s (log reaches its 1,000-entry cap) — ~105 calls/s

React commits during a 4 s closed run fell from ~1,590, about four per call, to ~340, about one per frame.

Correctness. A fixed 200-request run with the sidebar open shows exactly 200 tools/call echo rows, all answered and none Pending.

What is left, and the load panel's hint

With the sidebar open the page is still about 10× slower than with it closed. A trivial-row experiment, with each Protocol row rendered as a single Text, reached ~1,300/s open. That puts the remaining cost entirely in mounting and folding one real row: about 3 ms of Mantine per row, and the per-row ScrollArea on the compact target is the largest piece (removing it alone took the 12 s figure from ~105 to ~178/s). Fixing that means virtualizing the lists or replacing the per-row scroller, which is new scope and is filed as a follow-up. The load panel's "close the sidebar to measure the server" hint is therefore still accurate and left as is.

No visible UI change, so no screenshots.

🤖 Generated with Claude Code

cliffhall and others added 2 commits November 5, 2026 15:29
With the monitoring sidebar open the web load panel was capped at ~11
calls/s (~68 closed) because the browser main thread spent ~95% of a run
re-rendering. Profiling found four per-message costs:

- App subscribed to the message, fetch and stderr logs, and to the
  client's connection diagnostics (re-dispatched on every request and
  response), each rendering the whole page synchronously per event.
  useStoreSnapshot now takes an optional NotifyScheduler; the web client
  coalesces those four subscriptions to the animation frame. Only the
  notification is deferred: the hooks re-read the live store, so no entry
  is dropped or reordered.
- Every Protocol/Network row re-rendered on every message. Rows are now
  memoized, with the parent's handlers passed through stable
  useLatestCallback stand-ins, and MessageLogState replaces an entry when
  it folds in a response instead of mutating it, so identity is a sound
  memo key.
- Each row mounted its collapsed detail body (two read-only Ace editors
  for a Protocol entry) and a closed edit-and-replay Modal up front. Both
  now mount on first open (useOpenedOnce) and stay mounted.
- The Protocol/Network correlation re-parsed every fetch body on every
  render; the parses are memoized per (immutable) entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the web monitoring rendering optimizations.

No open issues 🎉

What changed in this PR

Addresses #2928 by reducing per-message rendering work in the web monitoring UI, using optional scheduling in shared React hooks while leaving TUI behavior unchanged.

Changes:

  • Coalesces log and connection-diagnostic notifications to animation frames while continuing to read live store data.
  • Memoizes Protocol and Network rows, replaces updated message entries immutably, and delays detail mounting until first open.
  • Caches transport-body parsing per entry and adds regression tests for scheduling, entry updates, callbacks, and lazy mounting.
File Description
core/​react/​useStoreSnapshot.ts Supports optional coalesced notifications and cleanup.
core/​react/​useStderrLog.ts Accepts an optional notification scheduler.
core/​react/​useMessageLog.ts Adds scheduling and documents immutable entry updates.
core/​react/​useInspectorClient.ts Allows connection diagnostics to use scheduled notifications.
core/​react/​useFetchRequestLog.ts Accepts an optional notification scheduler.
core/​react/​notifyScheduler.ts Adds notification coalescing and animation-frame scheduling.
core/​mcp/​typedEventTarget.ts Updates commentary while preserving the revision-counter contract.
core/​mcp/​state/​messageLogState.ts Replaces updated entries instead of mutating them.
clients/​web/​src/​utils/​correlateTransportErrors.ts Caches request-ID and response-error parsing per entry.
clients/​web/​src/​utils/​correlateTransportErrors.test.ts Tests repeated scans and replacement-entry handling.
clients/​web/​src/​test/​integration/​mcp/​inspectorClient-response-rejected.test.ts Re-reads entries after rejection annotations.
clients/​web/​src/​test/​core/​react/​useStoreSnapshot.test.tsx Tests coalescing, cleanup, and live snapshot reads.
clients/​web/​src/​test/​core/​react/​notifyScheduler.test.ts Tests scheduling, cancellation, and timer fallback.
clients/​web/​src/​test/​core/​mcp/​state/​messageLogState.test.ts Tests immutable updates and evicted-request handling.
clients/​web/​src/​hooks/​useOpenedOnce.ts Adds a persistent first-open latch for lazy mounting.
clients/​web/​src/​hooks/​useOpenedOnce.test.tsx Tests initial and subsequent open states.
clients/​web/​src/​hooks/​useLatestCallback.ts Provides stable callbacks that invoke the latest handler.
clients/​web/​src/​hooks/​useLatestCallback.test.tsx Tests callback identity and current-handler invocation.
clients/​web/​src/​hooks/​useInspectorStores.ts Enables animation-frame scheduling for all three logs.
clients/​web/​src/​components/​groups/​ProtocolListPanel/​ProtocolListPanel.tsx Memoizes Protocol rows with stable callbacks.
clients/​web/​src/​components/​groups/​ProtocolListPanel/​ProtocolListPanel.test.tsx Tests fresh replay handlers and updated row content.
clients/​web/​src/​components/​groups/​ProtocolEntry/​ProtocolEntry.tsx Defers detail and replay-modal mounting until first use.
clients/​web/​src/​components/​groups/​ProtocolEntry/​ProtocolEntry.test.tsx Tests lazy mounting and adjusts detail assertions.
clients/​web/​src/​components/​groups/​NetworkStreamPanel/​NetworkStreamPanel.tsx Memoizes Network rows and stabilizes reveal callbacks.
clients/​web/​src/​components/​groups/​NetworkStreamPanel/​NetworkStreamPanel.test.tsx Tests invocation of the latest reveal callback.
clients/​web/​src/​components/​groups/​NetworkEntry/​NetworkEntry.tsx Defers detail mounting until first expansion.
clients/​web/​src/​components/​groups/​NetworkEntry/​NetworkEntry.test.tsx Tests first-open mounting and retention after collapse.
clients/​web/​src/​App.tsx Enables scheduled connection-diagnostic notifications.
AGENTS.md Updates the historical mutation example without changing the rule.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed: round 1 came back clean (no inline comments, no suppressed findings, "no open issues"), which is the stop condition.

@cliffhall
cliffhall merged commit e10b1f3 into v2/main Nov 5, 2026
12 checks passed
@cliffhall
cliffhall deleted the v2/fix/2928-batch-message-renders branch November 5, 2026 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web: rendering each MCP message in the monitoring sidebar caps the page at ~10 calls/s

2 participants