Skip to content

perf(lynx): describe the first tree after the first paint, not before it - #153

Merged
Huxpro merged 2 commits into
new-lynxfrom
feat/lynx-first-tree-described-after-paint
Aug 25, 2026
Merged

Huxpro merged 2 commits into
new-lynxfrom
feat/lynx-first-tree-described-after-paint

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Capture the first tree eagerly; describe it lazily.

captureLynxFirstTree runs after renderFirstScreenNow has already published
the page to the host (main-thread.ts), and before announceReady(). Every
allocation 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. 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.

This is #56's second commit (a132577) re-derived against new-lynx rather
than transplanted: createLynxFirstTree has grown from three parameters to
five, 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

  • Validation and the per-record getUniqueId read: 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: a tap on the painted tree resolves through it long
    before the background adopts.
  • The logical-row map: the ownership equality capture ends with reads its size,
    and a first screen holds one native list host rather than one per host, so
    there is no per-row allocation here to move.
  • The native-list journal: adoption re-reads each list's recycling epoch to
    detect componentAtIndex/enqueueComponent traffic in the window between
    capture and adoption, so it has to be the epoch capture saw.
  • The capture's own roots and version: terminal cleanup empties
    rootChildren, so a builder that read them late would describe the container
    rather 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 !== null the source refuses to prepare a batch and both
apply() paths refuse to apply one. A first tree therefore still answers with
the tree it captured while unmount cleanup retries, and after its owner has
been disposed.

One deliberate, narrow change: releaseLynxFirstTree now drops the builder
along 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 —
releaseFirstTree in main-thread.ts — drops its reference in the same step,
so firstScreenSnapshot() answers null there as it always did.

Validation

  • pnpm typecheck — exit 0.

  • pnpm format:files on all five changed files — every file unchanged.

  • pnpm sync — exit 0, no generated changes.

  • targeted: --project lynx 545/545; host-driver.test.ts +
    first-screen.test.ts 116/116.

  • pnpm test — 3 failed | 21,229 passed (21,232) across 2,208 test
    files, 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 test
    files, benchmarks/, and a changeset, and nothing under
    packages/octane, packages/drei, or packages/rspeedy-plugin-octane:

    • octane/suspense-hydration › "restores focused input and its selected text
      after 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 both
      branches 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.md says the LIS reconciler guarantees final DOM
      and survivor identity but not which nodes physically move, which may make
      this assertion over-specified.
    • drei/upstream-e2e › "should match previous one" — the run fetches
      potsdamer_platz_1k.hdr from a CDN this container blocks. Environmental.
    • rspeedy-plugin › native-demo — the pre-existing failure recorded in
      Execution 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 passed
    5/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:

  1. Builder reads live state.acceptedVersion/state.rootChildren instead of
    the values read at capture → the new teardown test goes red, and only it.
  2. releaseLynxFirstTree keeps the builder → the new release test goes red,
    and only it.
  3. Builder describes described.slice(0, -1) → 19 red across
    first-screen.test.ts, host-driver.test.ts, list-recycling.test.ts, and
    milestone-8-integration.test.ts — both read orders, plus perf(lynx): build native lists through direct first-screen emission #113's native-list
    path, 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:

scale this branch base median-of-medians paired median pairs improving
1,000 (n=11) 207.6 ms 208.1 ms −0.5 ms (−0.24%) −2.5 ms (−1.2%) 7/11
10,000 (n=11) 1331.6 ms 1389.7 ms −58.1 ms (−4.18%) −51.1 ms (−3.7%) 7/11
30,000 (n=9) 3981.7 ms 4175.9 ms −194.2 ms (−4.65%) −221.2 ms (−5.3%) 8/9

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 in
the same window. Every octane-base ÷ octane ratio 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 create at exactly 1.00× is the semantic control here:
captureLynxFirstTree never runs on that path, so the change should be
invisible 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

  • The behavior change is release, not read. A first tree released before
    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 —
    releaseFirstTree in main-thread.ts — drops its reference in the same step,
    so firstScreenSnapshot() answers null there exactly as before. The new
    release test pins both halves.
  • Deferred reads are safe because the container refuses to move. While
    state.firstTree !== null the source refuses to prepare a batch
    (host-driver.ts:4528) and both apply() paths refuse to apply one (:4439,
    :6122), so nothing can replace a captured record's props, children, or
    visibility in between. transferFirstTree and disposeLynxHostContainer
    clear journals but never touch those record fields, and
    LynxDenseHostRecordStore.get() caches materialized records, so the retained
    references are stable. cloneProps already rejects cycles and foreign
    prototypes when props enter the container, so the deferral moves no
    reachable fault later.
  • perf(lynx): take first-tree capture off the first-paint path #56's other commit is not here, on purpose. 498d549 (skip prop-patch
    planning for hosts with no main-thread ownership) is already subsumed by
    new-lynx's sticky state.hasMainThreadProps predicate, which is O(1) per
    record 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.keys
    walks 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.
  • Next candidate on this path, not in this slice. Capture still reads
    getUniqueId per record — 17.3 ms of papi_read at 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.
  • Observed while reading, not acted on. A captured record's props are
    already frozen null-prototype deep clones, so snapshotFirstTreeProps's
    second 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

  • An agent produced this diff (agent-authored)

Generated by Claude Code

`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
@Huxpro
Huxpro marked this pull request as ready for review August 25, 2026 09:36
@Huxpro
Huxpro merged commit ecabf0f into new-lynx Aug 25, 2026
11 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants