Repository navigation
perf(lynx): describe the first tree after the first paint, not before it - #153
Merged
Merged
Conversation
`captureLynxFirstTree` runs after `renderFirstScreenNow` has published the page to the host and before `announceReady()`, so every allocation it makes sits between the tree reaching the DOM and the browser painting it. Most of what it allocates is not validation: it is the clone-safe description the background clones when it adopts — one frozen node object per record, a frozen copy of that record's child order, and a deep clone of its props. Nothing before adoption reads that description. Build it on first read. `createLynxFirstTree` takes a builder instead of a snapshot and `LynxFirstTree.snapshot` is a memoizing getter, so the first reader — `compareFirstTree` at adoption, or `firstScreenSnapshot()` before it — pays for it, off the paint path. What stays eager is what the contracts need: validation and the native-ID read, because a host that cannot be captured has to fault the synchronous first screen while its caller still holds the source to retry cleanup against; the event token map, because a tap on the painted tree resolves through it long before the background adopts; the logical-row map, because the ownership equality capture ends with counts it; and the native-list journal, because adoption re-reads each list's recycling epoch to detect traffic between capture and adoption. The capture's own root children and accepted version are read at capture rather than from the builder, since terminal cleanup empties `rootChildren`. Releasing a first tree now drops the builder as well as the journal maps it already cleared. The builder closes over the source container's records, so keeping it would leave a released tree retaining the page it described; a tree released before anything read it therefore refuses to describe itself. Same-window AB/BA against a preserved base build, fresh page per sample, quiet host: paired median −2.5 ms (−1.2%, inside noise) at 1,000 rows, −51.1 ms (−3.7%) at 10,000, −221.2 ms (−5.3%, 8/9 pairs) at 30,000. Monotone in record count, as removing per-record allocation from the paint path predicts. Every update-band ratio holds at or above parity, with post-mount `create` at exactly 1.00× as the semantic control — capture never runs on that path. Refs #148, #103. Co-Authored-By: Claude <[email protected]> Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
This was referenced Aug 24, 2026
…ree-described-after-paint
Huxpro
marked this pull request as ready for review
August 25, 2026 09:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Capture the first tree eagerly; describe it lazily.
captureLynxFirstTreeruns afterrenderFirstScreenNowhas already publishedthe page to the host (
main-thread.ts), and beforeannounceReady(). Everyallocation it makes therefore sits between the tree reaching the DOM and the
browser painting it. Most of what it allocates is not validation — it is the
clone-safe description the background clones when it adopts: one frozen node
object per record, a frozen copy of that record's child order, and a deep clone
of its props. On the 10,000-row bench fixture that is roughly 70,000 records'
worth of allocation on the paint path, for a description nothing reads until
the background's first batch arrives.
That description now builds on first read.
createLynxFirstTreetakes abuilder instead of a snapshot and
LynxFirstTree.snapshotis a memoizinggetter, so the first reader —
compareFirstTreeat adoption, orfirstScreenSnapshot()before it — pays for it, off the paint path.This is #56's second commit (
a132577) re-derived againstnew-lynxratherthan transplanted:
createLynxFirstTreehas grown from three parameters tofive, and the walk is now fused with the logical-row map that #113's direct
first-screen lists added. The audit of #56 against this branch is on #103.
What stays eager, and why
getUniqueIdread: a host that cannot becaptured has to fault the synchronous first screen, while its caller still
holds the source to retry cleanup against.
before the background adopts.
and a first screen holds one native list host rather than one per host, so
there is no per-row allocation here to move.
detect
componentAtIndex/enqueueComponenttraffic in the window betweencapture and adoption, so it has to be the epoch capture saw.
rootsandversion: terminal cleanup emptiesrootChildren, so a builder that read them late would describe the containerrather than the capture.
Behavior
The description reads only fields a captured record retains, and no path can
replace a captured record's props or children in between: while
state.firstTree !== nullthe source refuses to prepare a batch and bothapply()paths refuse to apply one. A first tree therefore still answers withthe tree it captured while unmount cleanup retries, and after its owner has
been disposed.
One deliberate, narrow change:
releaseLynxFirstTreenow drops the builderalong with the journal maps it already cleared, because the builder closes over
the source container's records and keeping it would leave a released tree
retaining the page it described. A tree released before anything read it
therefore refuses to describe itself. The only consumer path that releases —
releaseFirstTreeinmain-thread.ts— drops its reference in the same step,so
firstScreenSnapshot()answersnullthere as it always did.Validation
pnpm typecheck— exit 0.pnpm format:fileson all five changed files — every file unchanged.pnpm sync— exit 0, no generated changes.targeted:
--project lynx545/545;host-driver.test.ts+first-screen.test.ts116/116.pnpm test— 3 failed | 21,229 passed (21,232) across 2,208 testfiles, on this branch's pushed head. All three are pre-existing or
environmental, and none is reachable from this diff — it touches
packages/lynx/src/core/{first-screen,host-driver}.ts, their two testfiles,
benchmarks/, and a changeset, and nothing underpackages/octane,packages/drei, orpackages/rspeedy-plugin-octane:octane/suspense-hydration› "restores focused input and its selected textafter a keyed DOM reorder" — reproduces on
main:git diff origin/main origin/new-lynx -- packages/octane/src/runtime.ts packages/octane/tests/browser/suspense-hydration/is empty, so bothbranches run byte-identical code here. It also fails in isolation on a quiet
host, so it is not a load flake. Reported separately; note that
docs/differences-from-react.mdsays the LIS reconciler guarantees final DOMand survivor identity but not which nodes physically move, which may make
this assertion over-specified.
drei/upstream-e2e› "should match previous one" — the run fetchespotsdamer_platz_1k.hdrfrom a CDN this container blocks. Environmental.rspeedy-plugin›native-demo— the pre-existing failure recorded inExecution plan (#58): complete L2, then drive L4→L6 — full context handoff #66 §2.3 for this container.
For the record, the W0 head's run (PR test(bench): port the Element PAPI boundary instrument onto new-lynx #151) had a fourth,
syntax-highlighter/light-async, which does not reproduce here and passed5/5 in isolation there — a full-run-only flake, reported as such rather than
as fixed.
Break-checks. Every new assertion was watched fail before it was kept:
state.acceptedVersion/state.rootChildreninstead ofthe values read at capture → the new teardown test goes red, and only it.
releaseLynxFirstTreekeeps the builder → the new release test goes red,and only it.
described.slice(0, -1)→ 19 red acrossfirst-screen.test.ts,host-driver.test.ts,list-recycling.test.ts, andmilestone-8-integration.test.ts— both read orders, plus perf(lynx): build native lists through direct first-screen emission #113's native-listpath, which perf(lynx): take first-tree capture off the first-paint path #56 never covered.
Restored after each; 545/545 again.
FCP. #148 W1 requires the −5.4% be re-earned on
new-lynx, not quoted.Same-window AB/BA, fresh page per sample, quiet host, this branch against a
preserved base build of
new-lynx@6bf6703a4:A separate n=5 pilot at 10k gave −57.0 ms median-of-medians (−4.08%), 4/5
improving; pooling both 10k sessions gives 16 pairs, median −43.85 ms, 11/16
improving. 1,000 rows is inside this host's noise and is reported as such.
The win is monotone in record count — 4,028 / 40,028 / 120,028 selector attrs —
which is what removing per-record allocation from the paint path predicts and
what a timing artifact would not produce. 30,000 rows independently re-earns
#56's −5.4%. Well above #148's 1% NO-GO threshold: GO.
Records are committed under
benchmarks/lynx-table/prototype/results/.Update band.
run-web.mjs --reps 5 --scales 10000, base as a second cell inthe same window. Every
octane-base ÷ octaneratio is ≥ 1.00×: create 1.00×,update10th 1.05×, select 1.06×, updateStorm 1.04×, selectStorm 1.12×, clear
1.00×. No regression in the update band.
Post-mount
createat exactly 1.00× is the semantic control here:captureLynxFirstTreenever runs on that path, so the change should beinvisible there, and is.
Bundle. +413 B raw / +153 B gzip on the main-thread bundle — the memoizing
getter and the builder closure.
Risk / follow-ups
anything described it now throws instead of answering. That is deliberate:
the builder closes over the source container's records, so keeping it would
leave a released tree retaining the page it described, which is the opposite
of what release is for. The only consumer path that releases —
releaseFirstTreeinmain-thread.ts— drops its reference in the same step,so
firstScreenSnapshot()answersnullthere exactly as before. The newrelease test pins both halves.
state.firstTree !== nullthe source refuses to prepare a batch(
host-driver.ts:4528) and bothapply()paths refuse to apply one (:4439,:6122), so nothing can replace a captured record's props, children, orvisibility in between.
transferFirstTreeanddisposeLynxHostContainerclear journals but never touch those record fields, and
LynxDenseHostRecordStore.get()caches materialized records, so the retainedreferences are stable.
clonePropsalready rejects cycles and foreignprototypes when props enter the container, so the deferral moves no
reachable fault later.
498d549(skip prop-patchplanning for hosts with no main-thread ownership) is already subsumed by
new-lynx's stickystate.hasMainThreadPropspredicate, which is O(1) perrecord where perf(lynx): take first-tree capture off the first-paint path #56's per-host key scan is O(prop keys) — 70,041
Object.keyswalks on this fixture's first screen to answer what one boolean answers.
Landing it would be a regression. The full audit is on Lynx specialized background core: the Block model over the wire (#58 L2 body, post-attribution direction) #103.
getUniqueIdper record — 17.3 ms ofpapi_readat 10k on the W0 instrument.Deferring that is a smaller and riskier slice, because a native ID that cannot
be read has to fault the synchronous first screen; it stays a separate
proposal.
already frozen null-prototype deep clones, so
snapshotFirstTreeProps'ssecond clone may be removable outright. That changes the description's
aliasing contract rather than only when it is paid, so it is W4 material.
Provenance
agent-authored)Generated by Claude Code