Skip to content

perf(lynx): mint a first-screen host handle on demand - #270

Merged
Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-lazy-first-screen-handle
Sep 2, 2026
Merged

Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-lazy-first-screen-handle

Conversation

@Huxpro

@Huxpro Huxpro commented Sep 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

The first cut of #247's publish work. Per the owner directive
(#247 issuecomment-5485061147): drive the compiled/direct path through publish
until the interpreted remainder is gone, using #254's phase segmentation as a
local build tool rather than a merge gate.

#247's own probe table named the target. Inside publish at 10,000 rows,
host record building is 83.2 ms — cloneProps 27.7, createHandle 16.9,
textValue 16.7, planLynxHostPropPatch 11.7, selector install 5.8,
emitHostNode 5.2 — and none of it paints anything. This PR takes
createHandle, and the selector install with it.

The handle was never on the paint path

A host record has always known its root, id and generation. It just spelled
all three through the frozen public handle it minted to hold them, so every
reader that wanted one number allocated the whole seven-field object. Nothing
before paint wants the object:

  • The one selector write every painted host takes builds
    r{root}-h{id}-g{generation} itself, and never read handle.selector — that
    field is the CSS query form, [octane-ref=…]. The two strings were never
    the same string, which is why nothing noticed.
  • Every other pre-paint reader — the applier walk, the capture that follows it,
    the event and attachment bookkeeping — wanted one number.

So the record names root, id and generation directly, and the direct
first-screen applier builds the same LynxCompactHostRecord the compact-ACK path
already uses, which mints its handle on first read. This is what "extend the
compiled/direct path" means literally here: the compact record is the compiled
path's record shape, and the interpreted first-screen walk was still hand-rolling
an eleven-field literal beside it.

Adoption, a nodes-ref, an update, or a wire delta each still get a handle,
identical to the eager one and the same object on every read.

Changes

  • @octanejs/lynx: LynxHostRecord names root/id/generation;
    LynxCompactHostRecord's constructor opens up to carry them;
    applyLynxFirstScreenDirect builds a compact record instead of a literal;
    installNodesRefSelector takes the record; 41 record.handle.{id,root,generation,type}
    reads become direct field reads; assignHostHandle becomes the single writer
    of the handle/generation pair.

Measurement, stated honestly

The web lane cannot resolve this cut, and per the directive it does not have
to — "a cut that shows nothing on V8 but is structurally sound still lands; the
target engine has no JIT to hide interpretation behind."

An interleaved 4-round A/B (10,000 rows, 60,002 described hosts, 9 reps/round,
arms alternating) gives base median-of-medians 435.8 ms vs candidate
415.0 ms for publish — but two of four rounds favour base, within-arm
spread is ±40 ms, and best-min favours base (332.6 vs 342.2). That is a
non-result, and it is reported as one.

What is exact, and is the reason the cut lands:

  • createHandle and createLynxNodesRefSelector leave the CPU profile
    entirely
    . 60,002 frozen seven-field objects and 60,002 selector strings are
    no longer allocated between render and describe.
  • visit self time falls 132.7 → 97.4 ms per screen.

All numbers in this section come from a local node:inspector instrument driven
from inside a Vitest run, over a 10,000-row lynx-table-shaped tree on a lean
no-op PAPI, and were taken while this commit sat on the neighbouring stack head.
The commit is byte-identical to the one here; the wall-clock figures are
indicative either way, and the two structural facts above are base-independent.

The ablation that chose this target

Sizing first, from a local instrument over the same 10k tree (median publish,
[min] in brackets; the ablations are correctness-breaking probes, not
candidates):

probe publish vs base
base 447.1 [358.4] —
skip cloneProps 316.0 [296.4] −131.1 [−62.0]
skip createHandle 359.4 [320.7] −87.7 [−37.7]
skip planLynxHostPropPatch 373.3 [312.8] −73.8 [−45.6]

Methodology caveat, added after the fact. Each row above is a single run of
each arm, and the follow-up PR produced a case that shows single-run ablation is
not a measurement: a cloneProps sub-change measured −112.3 ms as a single-run
ablation and −11.5 ms — inside the noise — when the same change was run under
an interleaved A/B. So read this table as an ordering of where the time sits,
which is all it was used for (it is what picked createHandle and named
cloneProps as the larger next target), and not as effect sizes. Nothing in this
PR's claims rests on these numbers: the reason the cut lands is the two structural
facts above, and the wall-clock A/B in the previous section is reported as a
non-result.

cloneProps is the larger target and is the next PR (#271); it is a different
kind of change (it turns on whether the direct path has a realm boundary to defend
against) and does not belong in this diff.

Two cuts the cheap measurement declined

Recorded because a later reader will otherwise try them:

  • A textValue guard: no effect (417.0 [357.1] vs base 447.1 [358.4] — inside
    the noise on median, 1.3 ms on min). Dropped.
  • A lazy selector getter on the frozen handle, keeping the handle eager:
    1.36× worse (606.6 [505.7]). An accessor on a frozen object deoptimizes the
    path that reads it. Dropped — which is part of why the whole handle, rather than
    one of its fields, is what goes lazy.

Per #264 the cut is structural, not a cache: that census settled the memo
question (identity-keyed hit rate 0.0000 over 40,028 nodes; value-keyed declined)
and is not revisited here.

Correctness

The gate the directive keeps in full force is byte-identity between the old and
new publish path. The new regression test paints the same scene through both
arms — applyLynxFirstScreenDirect and the staged prepareLynxHostBatch — and
asserts, for every host in the captured tree, that the on-demand handle equals
the one the staged path built eagerly, field by field, and that a second read
returns the same object.

Each assertion was falsified before being trusted, per octane-core-extend:

break red lynx tests
handle getter not memoized 3 — the new test, installs only requested public-instance selectors for negotiated compact program hosts, keeps a run certifiable for direct teardown after a bound slot is updated
compact record built with container.root + 1 3 — the new test, produces the identical adoption snapshot, journal, and physical tree as the staged path, paints a native list into the container the staged path would have produced
compact record built with node.id + 1 10, across first-screen adoption and both native-list arms
assignHostHandle writes the handle but not the generation 3 — same-root portal ordering, native event binding, and the PAPI prop-routing test

No cost assertion is added to a correctness test: exact allocation counts are an
optimization claim and belong in the benchmark ratio system, per
octane-core-extend.

Spec-silent choices, noted rather than asked about

  1. cloneRecord reads the source handle eagerly, forcing a lazy one. That is
    the correctness half of the laziness, not a leak: isPropsOnlyWrite decides by
    handle identity, so two copies that each minted their own would report a
    write that changed nothing. No paint path reaches cloneRecord — a clone is
    already an update-path allocation.
  2. assignHostHandle is the sole writer of the pair. The record's
    generation mirrors handle.generation exactly, so a recreate — which bumps
    the generation and builds the handle for the ACK — must set both or the two
    fields would describe different generations of one host. Audited: one write
    site for each field, and type is never mutated on a record, so the lazily
    captured type cannot go stale.
  3. installNodesRefSelector takes the record rather than the handle, and
    spells the attribute out itself rather than deriving it from handle.selector
    — which it already did, for the reason above.

Validation

  • pnpm typecheck — clean, repo-wide
  • --project lynx — 49 files, 767 tests, all pass
  • pnpm test — 3 failed, 26,806 passed, 6 skipped across 2,452 files, with
    6 files red. None is in lynx, and each is accounted for:
red shape why it is not this diff's
suspense-hydration › restores focused input and its selected text after a keyed DOM reorder failed test browser focus/selection; re-run on c124594c7 and fails identically (recorded on #269)
drei › should match previous one failed test headless screenshot comparison, 30 s waitForFunction timeout; same
native-demo › escalates cleanup for a detached descendant after its leader exits failed test process-tree signal escalation on a 100 ms budget, on a test named for macOS; same
packages/aria/tests/hydration.test.ts beforeAll timeout, its test skipped the fixture build exceeds the default 10 s hookTimeout under a loaded container — passes when run alone
packages/octane/tests/react-hosted/web3-island.test.ts (octane, octane-prod) beforeAll timeout, its tests skipped the RainbowKit fixture exceeds the same 10 s budget in this container — fails identically on c124594c7 with this commit absent

The last three are beforeAll fixture builds hitting the default 10 s
hookTimeout, which is why 6 files are red but only 3 tests failed: their tests
are skipped rather than run. Neither file can reach the lynx host driver.

  • pnpm format:files on every changed path
  • pnpm sync — no generated changes

Risk / follow-ups

Does this change the sequence?

No. This is B under the #247 directive, taken after A (#269) as instructed.
It is independent of A — the commit cherry-picks onto new-lynx cleanly and is
opened against it directly, so #269 and this can gate in either order. It is one
cut inside publish, and it changes no ABI, no wire, and no phase boundary:
render → publish → capture → announce still run in the one synchronous
task before paint, which is exactly why the cut had to remove work rather than
move it to capture. #68's capture deferral and the cloneProps adoption are
unaffected and still follow.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

A host record has always known its root, id and generation. It spelled all
three through the frozen public handle it minted to hold them, so every reader
that wanted one number allocated the whole seven-field object — and nothing on
the paint path wants the object. The one selector write every painted host
takes builds `r{root}-h{id}-g{generation}` itself, and never read
`handle.selector`: that field is the CSS query form, so the two strings were
never the same string.

The record now names root, id and generation directly, and the direct
first-screen applier builds the same LynxCompactHostRecord the compact-ACK path
already uses, which mints its handle on first read. Adoption, a nodes-ref, an
update or a wire delta each still get one, identical to the eager one and the
same object on every read, because `isPropsOnlyWrite` decides by handle
identity. `assignHostHandle` becomes the sole writer of the handle/generation
pair, so the two can never describe different generations of one host.

A 10,000-row page stops allocating 60,002 frozen handles and 60,002 selector
strings between render and describe; `createHandle` leaves the first-screen
profile entirely.

Refs #247.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
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.

1 participant