Skip to content

perf(messages): stop per-row work on every incoming message in an open channel - #4605

Draft
joahg wants to merge 1 commit into
block:mainfrom
joahg:joah/perf-timeline-rows
Draft

joahg wants to merge 1 commit into
block:mainfrom
joahg:joah/perf-timeline-rows

Conversation

@joahg

@joahg joahg commented Dec 1, 2026

Copy link
Copy Markdown
Member

Problem

Every incoming message in an open channel did work proportional to the number of rows already open: the timeline mapped every row into an element on each render, scanned the list for the latest and catch-up ids, and the relay store re-derived sort keys, relayed-from claims, thread references and thread roots for every row. At about 2,300 open rows that was roughly 3 us per row per message at 1x, and it grows with channel length.

Fix

  • ChannelTimeline hands rows to the Virtualizer as data with a function child, so elements are only built for the visible range. The latest/catch-up ids come from a new newestRow helper, the id lookup is lazy, and the kept-rows and target-index guards avoid rebuilding when nothing changed.
  • Per-object WeakMap caches (keyed by the immutable event or row, or the tags array): eventMs in message-order, relayed-from claims, thread-reference, thread roots in TypingIndicator.
  • rowsProfileKey replaces repeated per-row profile key construction in useRowProfiles.
  • Fast paths: membershipRows with no membership rows, activity-presentation with no agent rows (lazy neighbor lookup and lazy keep).
  • Relay store: reconcile and setWindow make a single pass, and the prune and trim loops exit early when they cannot match.

No freezing changes; arrays are not frozen or sealed in any hot path.

Results

Per peer message, live window minus quiet window, 60 messages at 4 per second, mean of per-round medians over interleaved A/B rounds (order alternated each round), host load1 6-10 during the runs. Base is origin/main at the same commit.

Engine, CPU Rows Base Branch Change
Chromium 1x, task ms 300 11.36 11.16 -2%
Chromium 1x, task ms 1,400 14.33 14.00 -2%
Chromium 1x, task ms 2,300 18.56 14.95 -19%
Chromium 1x, script ms 300 / 1,400 / 2,300 5.02 / 6.40 / 8.80 5.04 / 5.68 / 5.90 0% / -11% / -33%
Chromium 4x, task ms 300 40.09 and 41.62 43.30 and 43.12 +4 to +8%
Chromium 4x, task ms 1,400 56.07 51.05 -9%
Chromium 4x, task ms 2,300 71.02 57.01 -20%
WebKit 1x, busy ms (spin probe) 300 15.84 16.29 +3%
WebKit 1x, busy ms (spin probe) 2,300 26.81 21.48 -20%

Script time per message no longer grows with row count on the branch (5.0 to 5.9 ms from 300 to 2,300 rows at 1x, against 5.0 to 8.8 ms before). Timeline inclusive CPU-profile slope went from 1.44 to about 0.33 us per row per message.

Known costs and limits:

  • At 300 rows the branch is about 0.3 to 0.4 ms per message slower at 1x (1.5 to 3 ms at 4x), consistently across rounds; the break-even is around 1,000 rows. I did not find the source.
  • The remaining task-time growth with rows is mostly layout/paint and a few other O(rows) owners that this PR does not touch (unread bookkeeping, the store's accepted-id set).
  • The store caps the open window at 2,400 rows, so 5,000 rows is not reachable; 2,300 is the largest case measured.
  • Load was not low (load1 6-10, sometimes higher between runs); the claim rests on interleaving and on the consistent sign across rounds.

Tests

  • Unit tests added or extended for sort order, relayed-from, thread references, membership profile key, message grouping (newestRow), message projection reconcile, and activity presentation including thread-root freshness. Each cache guard was mutated once and a test failed for each; one mutant (memory-only owners prune in reconcile) is equivalent and cannot be killed.
  • Virtualizer mocks in five test files now render function children.
  • The pre-push hook's full unit suite and tsc --noEmit pass; biome on the changed files is clean.
  • Chromium browser specs for timeline, scroll, history, unread, thread and typing areas, run on this branch and on origin/main with --no-deps: 98 failures on the branch and 101 on origin/main on this host, and the branch failures are a subset of the base failures. scroll.spec.mjs fails the same 4 cases on both.

🤖 Generated with Claude Code

… an open channel

Every peer message re-rendered the whole timeline, so the cost of one message
grew linearly with the rows already open (about 3 us per row per message).

- Virtualizer builds row elements lazily for the visible range instead of
  mapping every row on each render.
- Latest/catch-up ids come from the newest row instead of scanning the list.
- Cache sort keys, relayed-from claims, thread references and thread roots per
  immutable event/row object so they are computed once.
- Skip membership and activity presentation work when no membership or agent
  rows are present.
- Reconcile and window merging in the relay store make a single pass and skip
  prune loops that cannot match.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Joah Gerstenberg <joah@squareup.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant