Skip to content

perf(react): coalesce root notifications into one commit - #1

Open
colinux wants to merge 2 commits into
tchak:mainfrom
colinux:perf/batch-root-notifications
Open

colinux wants to merge 2 commits into
tchak:mainfrom
colinux:perf/batch-root-notifications

Conversation

@colinux

@colinux colinux commented Nov 2, 2026 •

Copy link
Copy Markdown

Why

In demarche.numerique's form-field editor, each field editor holds a <react-fragment> (a react-aria select). On a 275-field form, autosaving a field near the top returns one turbo-stream replace per following field: ~265 fragments removed and re-created at once.

Each of them calls notify(), which commits the single root, and every commit re-renders the portal of every mounted fragment: the work is quadratic. Measured in Chromium on that page:

0.18.4 0.19.5 same flush change on 0.18.4 this PR (on 0.19.5)
React commits per autosave 518 542 2 2
Rendering after the response 1.4 s 1.9 s 0.1 s 0.6 s
React commits on page load 553 553 3 3

What

  • fix(actions): ready() awaits the scheduled actions before asking plugins for their pending work. It used to read the plugins first, so it never awaited the renders its own actions start; it only worked because those renders committed in a microtask.
  • perf(react): notify() schedules a single flush (still flushSync) in a later task, and render() awaits it. A microtask doesn't help: actions run in separate animation-frame callbacks, each followed by its own microtask checkpoint. The new test reproduces this: 1 commit, 30 with a microtask.

Trade-offs

  • A newly created fragment can be painted empty for one frame, as with the concurrent root before 0.19.5.
  • ready() doesn't await the unmount of removed fragments: flushing removals synchronously would bring back half of the commits.

Verification

✅ Final review done on 6f88957, findings addressed

Tests: vp test and vp check pass, on Chromium only (no Firefox or WebKit locally).

Final review: 9 findings. 3 fixed (the test, a test title, a comment), 1 kept as a trade-off above, 1 dismissed (React 19's flushSync doesn't rethrow uncaught render errors, it reports them through onUncaughtError), 4 without a reachable scenario.

Not proven: Firefox and WebKit; the one-frame empty paint is reasoned, not observed.

colinux and others added 2 commits November 2, 2026 16:15
Actions#ready() read every plugin's pending set before awaiting the scheduled
actions. Those actions are applied on the next animation frame, and the React
plugin only starts tracking a fragment render when the morph reaches it, so
ready() never awaited the renders its own actions triggered. It only appeared
to: each render committed in a microtask continuation (after preload) that ran
before ready() resumed. The existing "actions plugin" test fails as soon as a
render commits any later, which the next commit does.

Await the scheduled actions first, then ask the plugins: the renders they
started are in the pending set by then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every notify() commits the root, and a commit re-renders the portal of every
mounted fragment. Replacing many fragments at once is therefore quadratic: in
demarche.numerique's form-field editor, an autosave on a 275-field procedure
replaces ~265 fragments (one turbo-stream each) and took 518 React commits
(542 with the flushSync of 0.19.5), blocking the main thread for 1.4–1.9 s.
Loading that page took 553 commits.

notify() now schedules a single flush in a later task and returns a promise
resolved once it has run. render() awaits it before dropping the loading
class, so ready() resolves after the fragments its actions rendered are
committed. Removals (a fragment removed by a morph, remove(), finalizeMorph)
are committed by the same deferred flush without being awaited, as they were
on the concurrent root before 0.19.5. Coalescing in a microtask does not help:
actions are applied in separate animation-frame callbacks, each followed by
its own microtask checkpoint. The flush itself keeps flushSync.

With this change the same autosave takes 2 commits and ~0.14 s of rendering,
and the page load 3 commits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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