Skip to content

perf(showcase-dashboard): memoize matrix cells + coalesce SSE deltas - #4502

Merged
jpr5 merged 2 commits into
mainfrom
worktree-cheeky-knitting-graham
Apr 30, 2026
Merged

jpr5 merged 2 commits into
mainfrom
worktree-cheeky-knitting-graham

Conversation

@AlemTuzlak

Copy link
Copy Markdown
Contributor

Summary

The shell-dashboard matrix tab can spike CPU and freeze the browser on load and during update bursts. Two compounding causes:

  1. Every PB SSE delta forces a full grid re-render. The matrix renders ~720 cells (18 integrations × ~40 features), and nothing in the cell tree was memoized — a single status row update propagated a new ctx.liveStatus Map identity through every cell, and identity-based React.memo couldn't help because the Map ref changed.
  2. PocketBase fires the subscribe callback once per record. A probe finishing dozens of services, or the initial-state replay on reconnect, produced N consecutive setRows calls and N React commits across separate microtasks (auto-batching only catches synchronous bursts).

Changes

  • ComposedCell wrapped in React.memo with a custom arePropsEqual. Compares overlays/catalogCell refs, ctx scalars, and — when the liveStatus Map identity changes — only the 5 row keys this cell actually reads (health:slug, e2e:slug/feature, smoke:slug, d5:slug/feature, d6:slug/feature). Because upsertByKey preserves row identity for unchanged keys, deltas that don't touch a cell's slug/featureId short-circuit at the memo boundary.
  • useLiveStatus buffers SSE callbacks into a per-key Map<string, PendingOp> and flushes via a single 16ms setTimeout. A burst of N deltas now produces 1 React commit instead of N. Last-write-wins per key. Buffer is cleared on effect teardown and on reconnect kickoff so post-reconnect initial fetches never land on top of stale buffered rows.

Why these two together

Throttling alone would still re-render every cell on each flush. Memoization alone would still get invalidated on every delta because each delta produces a new Map. Together: a burst of unrelated row updates → 1 React commit, and within that commit only cells whose specific rows changed actually re-render.

What I have not changed

  • The three independent useNowTick 1s timers on the Ops tab. They only matter if the freeze reproduces on Ops, not Matrix; happy to consolidate as a follow-up if needed.
  • OverlayColumnHeader (rendered 18× in the header, calls LevelStrip which does 4 Map lookups each). Same memo treatment would apply if these still show up after this lands.
  • No virtualization. Should not be necessary if the memo + throttle combo works as expected.

Test plan

  • Existing unit tests in the touched files pass — useLiveStatus.test.tsx 15/15, composed-cell.test.tsx 9/10 (the 1 failure is a pre-existing test-vs-impl drift in the "renders 3 layers when all active — docs deduped by health" case, verified by stashing my changes and re-running).
  • No new TypeScript errors in the touched files.
  • Manual perf trace before/after on the live dashboard — Chrome DevTools → Performance tab → record while loading the dashboard against a real PB feed. Expected: scripting time during initial load and during probe-finish bursts drops sharply.
  • Functional sanity — confirm cells still update on real status changes (e.g. trigger a probe and watch the matching cell flip tone within ~1 frame).

The matrix tab can freeze the browser on load and during update bursts
because every PB SSE delta triggers a full re-render of all ~720 cells
(18 integrations × ~40 features), and PocketBase fires the subscribe
callback once per record — a probe finishing dozens of services or an
initial-state replay produces N consecutive React commits.

- ComposedCell: wrap in React.memo with a custom equality check that
  compares overlays/catalogCell refs, ctx scalars, and (when the
  liveStatus Map identity changes) only the 5 row keys this cell
  actually reads. Because upsertByKey preserves row identity for
  unchanged keys, deltas that don't touch a cell's slug/featureId
  short-circuit at the memo boundary.

- useLiveStatus: buffer SSE callbacks into a per-key Map and flush via
  a single 16ms setTimeout. A burst of N deltas now produces 1 React
  commit instead of N. Last-write-wins per key. Buffer is cleared on
  teardown and on reconnect kickoff so post-reconnect initial fetches
  never land on top of stale buffered rows.
@vercel

vercel Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chat-with-your-data Ready Ready Preview, Comment Apr 30, 2026 2:52pm
docs Ready Ready Preview, Comment Apr 30, 2026 2:52pm
form-filling Ready Ready Preview, Comment Apr 30, 2026 2:52pm
research-canvas Ready Ready Preview, Comment Apr 30, 2026 2:52pm
travel Ready Ready Preview, Comment Apr 30, 2026 2:52pm

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

📣 Social Copy Generator

Generate social media copies (Twitter/X, LinkedIn, Blog Post) for this PR using Claude.

  • Generate social media copies

@AlemTuzlak
AlemTuzlak enabled auto-merge April 30, 2026 14:58
@jpr5
jpr5 merged commit a3bbe3e into main Apr 30, 2026
6 checks passed
@jpr5
jpr5 deleted the worktree-cheeky-knitting-graham branch April 30, 2026 15:00
jpr5 added a commit that referenced this pull request Apr 30, 2026
…ges (#4504)

## Summary

Follow-up to #4502. With matrix cells memoized and SSE deltas coalesced,
the dashboard is much smoother — but the **initial dashboard load**
still freezes for a few seconds because:

1. **Initial PB fetch is 10 sequential round-trips.** `fetchInitial`
walks `getList(page=1, 200) → getList(page=2, 200) → …` up to 10 pages,
each awaiting the previous. Network wall time stacks.
2. **First commit with real data is unavoidably a full-matrix
re-render.** The empty-map → populated-map transition invalidates
per-key memo checks on every cell — that's 720 cell renders in one
synchronous React commit, blocking the main thread.

## Changes

- **Parallelize the initial fetch.** Pull page 1 sequentially to learn
`totalItems`, then fire pages 2..N concurrently via `Promise.all`. Wall
time drops from `sum(rtt_per_page)` to roughly `max(rtt_per_page)`
modulo network parallelism. PocketBase reads are independent so this is
safe.
- **`startTransition` around the initial `setRows(initial)`.** The first
big commit is unavoidable, but marking it as a transition lets React 19
yield to user input mid-walk instead of blocking for the entire render.
`setStatus` stays urgent so the "connecting → live" indicator still
flips immediately.
- **`startTransition` around the SSE flush.** Bursts that the 16ms
coalescer can't fully absorb (large reconnect replays, simultaneous
probe completions) still need to yield rather than block.

## Test plan

- [x] `useLiveStatus.test.tsx` 15/15 pass with parallelized fetch (the
mock handles arbitrary page numbers).
- [x] `composed-cell.test.tsx` 9/10 pass (1 pre-existing failure, same
as #4502).
- [x] No new TypeScript errors in `useLiveStatus.ts`.
- [ ] **Manual perf trace before/after on the live dashboard.**
Expected: initial-load wall time drops sharply (sequential → parallel
pages), and any remaining heavy commit no longer blocks the main thread
(transition lets React yield).
- [ ] **Functional sanity** — confirm rows still arrive correctly when
pages return out of order, and the "connecting → live" status transition
still fires before the heavy render.
zeekay pushed a commit to Dream-AI-4444/CopilotKit that referenced this pull request Sep 11, 2026
…opilotKit#4502)

## Summary

The shell-dashboard matrix tab can spike CPU and freeze the browser on
load and during update bursts. Two compounding causes:

1. **Every PB SSE delta forces a full grid re-render.** The matrix
renders ~720 cells (18 integrations × ~40 features), and nothing in the
cell tree was memoized — a single status row update propagated a new
`ctx.liveStatus` Map identity through every cell, and identity-based
React.memo couldn't help because the Map ref changed.
2. **PocketBase fires the subscribe callback once per record.** A probe
finishing dozens of services, or the initial-state replay on reconnect,
produced N consecutive `setRows` calls and N React commits across
separate microtasks (auto-batching only catches synchronous bursts).

## Changes

- **`ComposedCell`** wrapped in `React.memo` with a custom
`arePropsEqual`. Compares `overlays`/`catalogCell` refs, ctx scalars,
and — when the `liveStatus` Map identity changes — only the 5 row keys
this cell actually reads (`health:slug`, `e2e:slug/feature`,
`smoke:slug`, `d5:slug/feature`, `d6:slug/feature`). Because
`upsertByKey` preserves row identity for unchanged keys, deltas that
don't touch a cell's `slug/featureId` short-circuit at the memo
boundary.
- **`useLiveStatus`** buffers SSE callbacks into a per-key `Map<string,
PendingOp>` and flushes via a single 16ms `setTimeout`. A burst of N
deltas now produces 1 React commit instead of N. Last-write-wins per
key. Buffer is cleared on effect teardown and on reconnect kickoff so
post-reconnect initial fetches never land on top of stale buffered rows.

## Why these two together

Throttling alone would still re-render every cell on each flush.
Memoization alone would still get invalidated on every delta because
each delta produces a new Map. Together: a burst of unrelated row
updates → 1 React commit, and within that commit only cells whose
specific rows changed actually re-render.

## What I have not changed

- The three independent `useNowTick` 1s timers on the Ops tab. They only
matter if the freeze reproduces on Ops, not Matrix; happy to consolidate
as a follow-up if needed.
- `OverlayColumnHeader` (rendered 18× in the header, calls `LevelStrip`
which does 4 Map lookups each). Same memo treatment would apply if these
still show up after this lands.
- No virtualization. Should not be necessary if the memo + throttle
combo works as expected.

## Test plan

- [x] Existing unit tests in the touched files pass —
`useLiveStatus.test.tsx` 15/15, `composed-cell.test.tsx` 9/10 (the 1
failure is a pre-existing test-vs-impl drift in the "renders 3 layers
when all active — docs deduped by health" case, verified by stashing my
changes and re-running).
- [x] No new TypeScript errors in the touched files.
- [ ] **Manual perf trace before/after on the live dashboard** — Chrome
DevTools → Performance tab → record while loading the dashboard against
a real PB feed. Expected: scripting time during initial load and during
probe-finish bursts drops sharply.
- [ ] **Functional sanity** — confirm cells still update on real status
changes (e.g. trigger a probe and watch the matching cell flip tone
within ~1 frame).
zeekay pushed a commit to Dream-AI-4444/CopilotKit that referenced this pull request Sep 11, 2026
…ges (CopilotKit#4504)

## Summary

Follow-up to CopilotKit#4502. With matrix cells memoized and SSE deltas coalesced,
the dashboard is much smoother — but the **initial dashboard load**
still freezes for a few seconds because:

1. **Initial PB fetch is 10 sequential round-trips.** `fetchInitial`
walks `getList(page=1, 200) → getList(page=2, 200) → …` up to 10 pages,
each awaiting the previous. Network wall time stacks.
2. **First commit with real data is unavoidably a full-matrix
re-render.** The empty-map → populated-map transition invalidates
per-key memo checks on every cell — that's 720 cell renders in one
synchronous React commit, blocking the main thread.

## Changes

- **Parallelize the initial fetch.** Pull page 1 sequentially to learn
`totalItems`, then fire pages 2..N concurrently via `Promise.all`. Wall
time drops from `sum(rtt_per_page)` to roughly `max(rtt_per_page)`
modulo network parallelism. PocketBase reads are independent so this is
safe.
- **`startTransition` around the initial `setRows(initial)`.** The first
big commit is unavoidable, but marking it as a transition lets React 19
yield to user input mid-walk instead of blocking for the entire render.
`setStatus` stays urgent so the "connecting → live" indicator still
flips immediately.
- **`startTransition` around the SSE flush.** Bursts that the 16ms
coalescer can't fully absorb (large reconnect replays, simultaneous
probe completions) still need to yield rather than block.

## Test plan

- [x] `useLiveStatus.test.tsx` 15/15 pass with parallelized fetch (the
mock handles arbitrary page numbers).
- [x] `composed-cell.test.tsx` 9/10 pass (1 pre-existing failure, same
as CopilotKit#4502).
- [x] No new TypeScript errors in `useLiveStatus.ts`.
- [ ] **Manual perf trace before/after on the live dashboard.**
Expected: initial-load wall time drops sharply (sequential → parallel
pages), and any remaining heavy commit no longer blocks the main thread
(transition lets React yield).
- [ ] **Functional sanity** — confirm rows still arrive correctly when
pages return out of order, and the "connecting → live" status transition
still fires before the heavy render.

This branch was successfully deployed

5 active deployments
Preview – research-canvas — 8b5c6d2d Deployed Apr 30, 2026 by vercel[bot]
Preview – travel — 8b5c6d2d Deployed Apr 30, 2026 by vercel[bot]
Preview – docs — 8b5c6d2d Deployed Apr 30, 2026 by vercel[bot]
Preview – form-filling — 8b5c6d2d Deployed Apr 30, 2026 by vercel[bot]
Preview – chat-with-your-data — 8b5c6d2d Deployed Apr 30, 2026 by vercel[bot]
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.

2 participants