Skip to content

perf(lynx): build native lists through direct first-screen emission - #113

Merged
Huxpro merged 6 commits into
new-lynxfrom
perf/lynx-direct-first-screen-lists
Aug 21, 2026
Merged

Huxpro merged 6 commits into
new-lynxfrom
perf/lynx-direct-first-screen-lists

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Stacked on #112 (feat/lynx-adopt-native-list). Issue #66 Phase C, C3 — the
last step of the phase.

The direct first-screen applier writes the rendered record tree straight to the
Element PAPI: no command batch, no prepared operation list, no cloned record
maps. It refused any tree containing a <list>. That was the right call while
such a page could not be adopted anyway — painting a page the background would
then re-render is worse than not painting it. #112 made it adoptable, so the
refusal became the remaining cost on exactly the pages a fast first screen
exists for.

A <list> breaks the applier's shape twice over:

  • Its element does not come from __CreateElement. It comes from __CreateList,
    with the three recycling callbacks passed at creation.
  • Its rows are not attached to it. The platform materializes a row through
    componentAtIndex when it needs one, so the walk records every row and every
    row-descendant without creating anything — exactly the create operations
    the staged path skips.

Creation order is a contract, and it decided the design

The list's element is created on the way down, so its unique ID lands in the
same order the staged path assigns it. A list whose element were created after
its subtree would hand a later sibling the lower ID, and a page's element
identity would depend on which applier painted it. Only the row metadata waits
for the way back up, because it is read off records the walk has not created yet.

That is why createNativeListNode splits into beginNativeListNode (create +
register the main-local list state) and publishNativeListItems (build the
descriptors, setAttribute('update-list-info', …)). The staged path calls the
composition and is unchanged. The differential fixture carries a <text> sibling
after the list precisely to hold this property — remove the sibling and the
ordering bug is invisible.

It asks before it paints, because it cannot stop halfway

Emitting as it walks means the applier cannot discover a malformed list partway
through and back out: that leaves a half-painted page, which is the one state the
staged path never produces. So it runs a pre-walk and hands anything it cannot
vouch for back to the staged path with nothing created, where the diagnostic is
raised from where it has always been raised.

The pre-walk runs the real validators — createLynxListItemDescriptor and
planLynxListUpdate — over the same nodes rather than restating their rules, and
tracks the two placement rules the prepare walk owns (nested <list>,
<list-item> outside a list) alongside. Ranges are transparent in it, for the
same reason they are in C1's predicate: @for produces no record, so an authored
list does not own its rows as children, and a reader stopping at the immediate
children would validate an empty list, wave it through, and fault at publish —
the half-painted page this exists to prevent.

Two trees still take the staged path, and both deliberately:

  1. A host that offers no __CreateList. That page cannot build a <list> at
    all and is owed the diagnostic, not a silent fallback.
  2. A list topology the pre-walk will not vouch for.

Measurement

Same harness and fixture as #112: a template-created shell wrapping a <list>
whose rows arrive through a keyed @for, each row a <list-item> with three
children. Arms by checking out host-driver.ts; 21 samples per pass after 3
warmups, each in a fresh environment. Every sample of both arms was asserted to
have adopted.

page arm first paint total main-thread PAPI
1,000 rows staged 42.5 ms 120.6 ms 21
1,000 rows direct 29.5 ms 102.3 ms 21
50 rows staged 3.1 / 2.9 ms 9.3 / 7.7 ms 21
50 rows direct 2.3 / 2.5 ms 6.7 / 6.9 ms 21

Each cell is a median of 21 samples. The 1,000-row rows take the middle of three
passes — paint: staged 42.5 / 40.9 / 45.7, direct 28.5 / 29.5 / 31.4 — so the two
distributions are disjoint (slowest direct sample 31.4 ms, fastest staged
40.9 ms). The 50-row page ran two passes each, so both are given rather than
averaged.

Element PAPI traffic is identical in both arms at both sizes: 21 calls. That
is the point. What this removes is Octane's own staging, not one host call; the
platform sees the same page built the same way.

The arc across the whole phase, on the same 1,000-row page:

state when the page appears
before #112 — declined, background renders it 70.4 ms
#112 — painted and adopted through the staged path 42.8 ms
this PR — painted and adopted through the direct path 29.5 ms

Every figure is a lower bound on the real improvement: the harness hands the
background's commit over immediately, where a real page pays a bundle load and a
cold render on another thread first.

Red/green

Every guard below was produced by breaking the implementation on this branch and
running --project lynx (440 tests).

break result
rows painted as ordinary hosts 8 failed | 431 passed
never publish the rows 4 failed | 435 passed
publish on the way down instead of the way back up 4 failed | 435 passed
hasNativeListTopology never set 440 passed — unguarded, see below
pre-walk always returns true 3 failed | 437 passed
pre-walk always returns false 2 failed | 437 passed
ranges opaque in the pre-walk 1 failed | 439 passed
restored 440 passed

The one unguarded line, stated rather than papered over

state.hasNativeListTopology = true has no test that fails without it. It is
unobservable on a direct-painted container today, by construction: the only
caller of applyLynxFirstScreenDirect is main-thread.ts:1907, on the
first-screen source container, and the staged prepareLynxHostBatch(source, …)
only runs when the direct applier returned false. So a container is never both
direct-painted and batch-prepared, and the flag's consumers — the compact-run and
lazy-public-instance fast paths in the staged prepare — never read it on a
container this path built.

It is set anyway, because the flag is a property of the container's topology,
not of the applier that produced it, and the next caller of this function should
not have to rediscover that. I did not write a test for it: the only test that
could fail would have to reach past the public entry point into
LynxHostState, which .rulesync/rules/testing.md forbids, and a contrived
scenario asserting an internal boolean would protect the current routing rather
than the behavior.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test — ran on this head: 2,185 files / 20,971 tests passed, 11 files
    / 5 tests failed. Every failure reproduces on the integration base
    origin/new-lynx in this container and none is in packages/lynx
    — six
    re-verified there one by one, the rest are build/spawn timeouts on a 4-CPU
    box. Full breakdown in this comment.
  • targeted: vitest run --project lynx → 440 passed (32 files), including 12
    in packages/lynx/tests/first-screen-direct.test.ts
  • node benchmarks/bench.mjs --quick --ratios lynx-list → PASS, allocation
    ratio 0.012x eager, unchanged
  • pnpm sync — no generated changes

native-demo — one of the two rspeedy-plugin failures issue #66 §2.3 records as
pre-existing here — is among them.

Risk / follow-ups

  • The pre-walk is a second pass over the tree, paid only by pages that contain a
    <list> — a list-free page still short-circuits on firstScreenTreeHasList.
    It is the price of never half-painting; at 1,000 rows the paint still drops
    13 ms net.
  • The pre-walk duplicates the call of the list validators, not their rules.
    If a new list rule lands somewhere other than createLynxListItemDescriptor,
    planLynxListUpdate, or the two placement checks, the pre-walk would not see
    it and the applier could fault mid-walk. A single "would the staged path accept
    this list?" entry point both paths call would close that; it is not this PR.
  • compareFirstTree still walks all 4,002 records on the 1,000-row page to
    compare 2 physical nodes — measured at 11.1 ms in feat(lynx): adopt a painted native list into the background root #112's instrumentation.
    Narrowing that walk is the natural next increment.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

The direct applier writes the rendered record tree straight to the Element
PAPI — no command batch, no prepared operation list, no cloned record maps —
and refused any tree containing a `<list>`. That was the right call while such
a page could not be adopted anyway; now that it can be, the refusal was the
remaining cost on exactly the pages a fast first screen is for.

A `<list>` breaks the applier's shape twice. Its element comes from
`__CreateList` with the recycling callbacks passed at creation, not from
`__CreateElement`; and its rows are not attached to it, because the platform
materializes a row when it needs one, so the walk records every row and
descendant without creating anything — the same operations the staged path
skips.

The element is still created on the way down, where its unique ID lands in the
order the staged path assigns it: a list whose element were created after its
subtree would hand a later sibling the lower ID. Only the row metadata waits
for the way back up, because it is read off records the walk has not created
yet. `createNativeListNode` splits into `beginNativeListNode` and
`publishNativeListItems` for that, and the staged path calls the composition.

Emitting as it walks means the applier cannot discover a malformed list halfway
and stop — a half-painted page is the one state the staged path never produces
— so it runs the real validators over the tree first and hands anything it
cannot vouch for back, with nothing created and the diagnostic raised from
where it has always been raised. A host offering no `__CreateList` falls back
for the same reason: that page is owed the report.

At 1,000 rows the first screen goes from a median 42.5 ms to 29.5 ms, the two
distributions disjoint across three passes each. Element PAPI traffic is
identical in both arms — 21 calls — which is the point: what this removes is
Octane's own staging, not one host call.

Refs #66.

Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner Author

Full pnpm test on this exact head (566700e):

Test Files  11 failed | 2185 passed (2196)
     Tests   5 failed | 20971 passed | 76 skipped (21052)

Every failure reproduces on the integration base origin/new-lynx (5d61724) in this container, and none is in packages/lynx. I re-ran the six non-obvious ones there rather than assuming:

failing file on base new-lynx mode
packages/octane/tests/browser/suspense-hydration/… also fails (1 failed | 10 passed) assertion — expected [0,0] to equal [2,9]
packages/drei/tests/browser/upstream-e2e.browser.test.ts also fails page.waitForFunction 30 s timeout
packages/dropzone/tests/probes/hydration.test.ts also fails 5 s test timeout
packages/octane/tests/react-hosted/web3-island.test.ts also fails 10 s hook timeout
packages/aria/tests/hydration.test.ts also fails 10 s hook timeout
packages/aria/tests/package-boundary.test.ts also fails 10 s hook timeout

The remainder are build/spawn timeouts on this 4-CPU container, not assertions:
website/tests/ssr-hydration.e2e.test.ts and ssr-smoke.test.ts both die on
website vite build did not finish within 300000ms;
packages/octane-evals/tests/user-app-corpus.test.ts on spawnSync pnpm ETIMEDOUT;
packages/input-otp/tests/hydration/input.test.ts on a 10 s hook timeout.
packages/rspeedy-plugin-octane/tests/native-demo.test.ts is the known
pre-existing failure recorded in issue #66 §2.3.

The diff in this PR is confined to packages/lynx/src, packages/lynx/tests,
docs/, and a changeset — there is no mechanism by which it reaches a DOM
hydration, drei, aria, or website suite. vitest run --project lynx is
440 passed (32 files) on this head.


Generated by Claude Code

claude added 4 commits August 20, 2026 19:08
… paints

Before this branch a page holding a native <list> was declined by the direct
first-screen applier and rebuilt through the staged path, so prepare's final
validation raised `main-thread ref X is assigned to hosts N and M` with zero
PAPI calls spent. C3 makes such a page directly buildable, and the direct path
carried a pre-walk for only one of prepare's two non-list diagnostics: the
main-thread/background event collision. A duplicated ref therefore survived to
`installMainThreadRef`, which reports it as `is already mounted` — a weaker
message, raised mid-mount, after elements exist. That is the half-painted page
the pre-walk exists to prevent.

Extend the pre-walk to carry both diagnostics, with prepare's exact ref message
and its visibility rule. The walk is now unconditional: a background listener is
announced by the envelope, but a ref is announced by nothing but the host's own
props, and this applier is exported, so no envelope field may make the check
skippable. It is a second read-only pass over a tree the function already scans
for a <list>.

This is where the pre-check fix on new-lynx (e6b2898) lands once #112 retires
`firstScreenTreeIsUnadoptable`: the same two diagnostics, replayed from the one
place that still decides whether to paint.

Regressions: the two gate scenarios now also assert nothing was painted, and a
new one puts the duplicate on two rows of one list — rows own no element during
the first screen, so no mid-mount check can ever see it. Both ref scenarios were
verified red without the check (2 failed | 25 passed).

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy

Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner Author

Your duplicated-main-thread-ref regression caught a real defect in this PR. Fixed in d7e1e6b, and the stack is rebuilt on new-lynx at 2393505 (#108–#110 now merged; #111 retargeted to new-lynx).

What was broken. Before this branch, a page holding a native <list> was declined by the direct applier and rebuilt through the staged path, so prepareLynxHostBatch's final validation raised main-thread ref "gate:ref" is assigned to hosts N and M with zero PAPI calls spent. C3 makes that page directly buildable, and the direct path carried a pre-walk for only one of prepare's two non-list diagnostics — the event collision. The duplicate therefore survived to installMainThreadRef, which reports is already mounted: a weaker message, raised mid-mount, after elements exist. That is exactly the half-painted page the pre-walk exists to prevent, so the test was failing for the right reason.

Where your e6b2898 landed. Your fix replays both diagnostics inside firstScreenTreeIsUnadoptable, which #112 retires. Rather than drop it in the merge, I moved it to the one place that still decides whether to paint: applyLynxFirstScreenDirect's pre-walk now carries both checks, with prepare's exact ref message and its record.visible rule. Same two diagnostics, same wording, one site instead of two.

The walk is now unconditional. A background listener is announced by the envelope, so a page with none could skip a collision-only walk; a main-thread:ref is announced by nothing but the host's own props, and this applier is exported — an envelope field claiming "no refs here" would put a correctness check at the caller's discretion. It costs a second read-only pass over a tree this function already scans for a <list>.

Regressions. Both of your scenarios now also assert nothing was painted (querySelectorAll('view') is empty), which is the half of the contract the message alone does not prove. Added one more: the duplicate on two rows of a single list. Rows own no element during the first screen, so no mid-mount check can ever see that one — it fails with nothing thrown at all without the pre-walk. Verified red first: 2 failed | 25 passed with the ref branch disabled, 27 passed with it.

Re-validated at the stack tip after the cascade: vitest run --project lynx 448 passed (32 files), pnpm typecheck clean, pnpm sync no changes.

One correction to this PR's body I owe you: the Risk bullet says the pre-walk is "paid only by pages that contain a <list>". That is still true of the list-eligibility pre-walk it was written about, but the diagnostics pre-walk is now unconditional. I will fold that into the body rather than leave it reading as though nothing changed.


Generated by Claude Code

Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner Author

Correction to my previous comment: I said I would fold that Risk-bullet fix into the PR body, and I am deliberately not doing it. The only PR-editing tool available in this session round-trips the body lossily — it strips <list>, <view> and <list-item> out of the text it returns, so writing back what it reads would replace every one of those with an empty pair of backticks and corrupt the description throughout. Not worth the damage for one bullet.

So the corrected bullet stands here instead of in the body:

The list-eligibility pre-walk is a second pass paid only by pages that contain a <list> — a list-free page still short-circuits on firstScreenTreeHasList. The diagnostics pre-walk (event collision + duplicated main-thread ref) is unconditional as of d7e1e6b, because a ref is announced by nothing but the host's own props. Both are the price of never half-painting; at 1,000 rows the paint still drops 13 ms net.

Happy to apply it to the body directly if you would rather it live there.


Generated by Claude Code

Capture journaled the epoch with one copy of the counter sum and adoption
compared it with another; a counter added to only one side would silently
break staleness detection (every list adoption repairs, or a stale tree
adopts). One listRecyclingEpoch() now owns the formula for both sides.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YHfFsy9CLaRovTFH8pTGyY
@Huxpro
Huxpro changed the base branch from feat/lynx-adopt-native-list to new-lynx August 21, 2026 20:24
@Huxpro
Huxpro marked this pull request as ready for review August 21, 2026 20:24
@Huxpro
Huxpro merged commit ce0eb03 into new-lynx Aug 21, 2026
1 of 2 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