Skip to content

perf(lynx): adopt the first screen's props instead of copying them - #271

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

Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-first-screen-adopts-props

Conversation

@Huxpro

@Huxpro Huxpro commented Sep 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

The second cut of #247's publish work, and the larger half of the record-building
bucket #270 opened on. 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.

cloneProps is 27.7 ms of #247's 83.2 ms host-record bucket at 10,000 rows, and
this PR stops calling it on the direct first-screen path.

The copy is defending against a boundary this path does not have

cloneProps exists because a host prop bag normally arrives from the other
thread — a separate JS realm in production — so the container must build data it
owns out of values it has proved it can hold. The direct first-screen path has no
such crossing. Its bag was built by universalProps
(packages/octane/src/universal-core.ts) in this realm, on this call stack, and
frozen at its only construction site: both of that function's return paths end
in Object.freeze(props), so frozen-on-arrival is a construction invariant rather
than a property of one fixture. __proto__ is installed there as an own data
property by defineUniversalProtoProp, so the prototype is always this realm's
Object.prototype.

So the container can hold the bag it was given, and adoptFirstScreenProps does —
after testing each premise instead of assuming it. A bag that fails any one takes
the copy exactly as before: still mutable, reached through a class, or holding an
object.

The counters chose the shape before the code was written

The copy's work at 10,000 rows, from a local instrument (60,002 bags, 70,002 keys):

calls 60002  keys 70002  empty 0  nested 0  scalarOnly 60002
mtScan 0  mtHit 0  frozenIn 60002  nullProtoIn 0

Every bag is already frozen, every bag is scalar-only, and not one carries a
main-thread: key. For a scalar-only frozen bag the copy's only observable
product is the null prototype — which is what made adoption, rather than a faster
copy, the shape of the cut. Per #264 this is structural and 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.

The main-thread: unwrap now asks a value, not a name

cloneProps ran a whole pre-pass — a {...source} spread plus a startsWith per
key — to find tagged callables. A tagged callable is a function, so it can only
appear where the copy already stops for a non-scalar. Folding the unwrap into that
branch costs one startsWith per non-scalar prop instead of one per prop, and a
first screen of scalar bags never asks it at all. The pass this replaces walked
70,002 keys to find zero.

Changes

  • @octanejs/lynx: adoptFirstScreenProps, wired into the single direct
    first-screen call site; cloneProps folds its main-thread: pre-pass into the
    copy loop's non-scalar branch. The wire paths — create, update, recreate,
    template-run and program-plan — all keep cloneProps unchanged.

Correctness

The gate the directive keeps in full force is byte-identity between the old and
new publish path, plus a decline path for anything the direct path cannot carry.
Four new tests in first-screen-direct.test.ts, on a scene whose prop names are
Object.prototype members (constructor, toString, valueOf,
hasOwnProperty, isPrototypeOf, propertyIsEnumerable, toLocaleString) with
an unadoptable nested-style child beside them:

  • paints that scene identically through both arms — applyLynxFirstScreenDirect
    and the staged prepareLynxHostBatch;
  • copies a bag that is still mutable: the test mutates the caller's object
    after painting and asserts the snapshot is unaffected;
  • refuses a bag that reaches the applier through a class
    (/host props require plain objects/);
  • gives a host only its own props, never one its prototype carries.

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

break red lynx tests
adopt without the scalar-only scan 1
adopt without the Object.isFrozen test 1
adopt without the prototype test 1
drop the folded main-thread: unwrap 4
loosen the prototype test to cloneProps's cross-realm shape 0

The fifth is reported as a miss rather than dressed up. I traced it: both for…in
readers of a stored bag operate on compiled-program prop tables rather than on
adopted first-screen bags, so no existing scene can observe an inherited
enumerable today. The tightening is defensive — it was found by reading rather
than by measuring, and it closes the hazard before a future reader opens it, but
this suite would stay green without it.

A deliberate strictness change

The deleted pre-pass built {...source} before the prototype test ran, which
laundered a non-plain prototype through the spread. A class instance carrying a
main-thread: handler was therefore accepted by the old cloneProps and is
rejected by the new one. This is the intended behaviour — cloneProps's contract
has always been "plain objects only" — but it is a behaviour change on the wire
path, not only on the first screen, and it is called out here rather than buried.

Measurement

Unlike #270, the web lane can resolve this one. An interleaved 4-round A/B
(10,000 rows, 60,002 described hosts, 9 reps/round, arms alternating within each
round):

round base publish candidate publish
0 484.9 351.7
1 466.1 306.3
2 422.5 377.8
3 507.9 324.1

Median-of-medians 475.5 → 337.9 ms (−137.6, −28.9%); best-min 334.7 → 263.4;
4 of 4 rounds favour the candidate, and the two arms' median ranges do not
overlap
([422.5, 507.9] against [306.3, 377.8]).

Piecewise ablation, and a caveat about it

Sizing, from single-run probes over the same tree (median publish, [min] in
brackets; these are correctness-breaking probes, not candidates):

probe publish vs base
base 490.7 [395.0] —
skip the prototype check 453.6 [365.0] −37.1 [−30.0]
skip the main-thread: pre-pass 378.4 [329.8] −112.3 [−65.2]
skip the freeze 362.0 [339.9] −128.7 [−55.1]
faster prototype test 359.7 [294.7] −131.0 [−100.3]
adopt the frozen bag 285.8 [262.1] −204.9 [−132.9]

Read the ordering, not the numbers. A single run of each arm is not a
measurement, and this campaign has a case that proves it: the main-thread:
pre-pass shows −112.3 ms here and −11.5 ms — inside the noise — when the
same change is run under the interleaved A/B above. The pre-pass fold ships in
this diff on the structural argument alone (70,002 startsWith calls and 60,002
spreads removed to find nothing), not on that −112.3. Only the interleaved figure
in the previous section is claimed as a result. (The same caveat applies to
#270's ablation table, and has been added there.)

Spec-silent choices, noted rather than asked about

  1. The adoption guard is stricter than the copy's guard, asking for exactly
    this realm's Object.prototype or none. The two are answering different
    questions: hasCrossRealmPlainPrototype decides whether a bag from another
    realm is plain enough to copy out of, and accepts "one hop from null" — which
    permits a prototype carrying its own enumerable properties. Adoption decides
    whether a bag can be held as it is, and for…in over a held bag would see
    those. This realm's Object.prototype cannot carry them; its members are all
    non-enumerable.
  2. The scalar scan uses Object.keys, not for…in, for the same reason —
    the scan must not itself depend on the prototype being clean.
  3. An empty bag still answers with EMPTY_HOST_PROPS. Adoption would
    otherwise hand back a distinct frozen empty object per propless host, and the
    shared singleton is the invariant the rest of the driver was written against.
  4. Only the direct first-screen call site adopts. Every wire path keeps
    cloneProps, because for those the realm boundary is real.

Validation

  • pnpm typecheck — clean, repo-wide
  • --project lynx — 49 files, 771 tests, all pass
  • pnpm test — 4 failed, 26,811 passed, 4 skipped across 2,452 files, with
    6 files red. None is in lynx, none can reach the lynx host driver, 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; fails identically on c124594c7 with this stack absent (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
syntax-highlighter › fails closed for missing, extra, renamed, collided, stale, and removed-lane evidence failed test — Test timed out in 5000ms six execFileSync child processes inside one 5 s deadline. Re-run alone on this head: passes, 4.65 s of test time — it is at the edge unloaded and tips over under the full suite
packages/aria/tests/hydration.test.ts beforeAll timeout, its test skipped the fixture build exceeds the default 10 s hookTimeout under load. Re-run alone on this head: passes, 19.17 s wall
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 stack absent

The last two rows are beforeAll fixture builds hitting the default 10 s
hookTimeout, which is why 6 files are red but only 4 tests failed: their tests
are skipped rather than run.

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

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.

Risk / follow-ups

  • The strictness change above is the one behaviour difference outside the
    first screen. A class instance carrying a main-thread: prop now throws where
    it used to be laundered through the spread.
  • This PR is stacked on perf(lynx): mint a first-screen host handle on demand #270 (perf/lynx-lazy-first-screen-handle). The two
    are independent in host-driver.ts — that file auto-merges onto new-lynx —
    but both add tests to first-screen-direct.test.ts at adjacent points, so a
    solo cherry-pick conflicts there textually. Stacking gives the cleaner review
    diff, and GitHub retargets this to new-lynx when perf(lynx): mint a first-screen host handle on demand #270 merges.
  • Branch naming. The harness assigned claude/octane-issue-66-phase-a-7wz97z;
    this repo's create-a-pr requires a type-prefixed topic branch so the label bot
    can read the type from it. The topic branch is used, and the deviation is
    flagged here rather than resolved unilaterally.

The next cut was built, and then declined

Recorded here rather than left as a to-do, because the interesting part is why it
did not land. After this PR, a re-profile of publish on this head named
firstScreenTreeHasList — a full read-only traversal of every node whose entire
product is one boolean — at 72.1 ms over 7 screens, 10.3 ms per screen. Fusing
it into the ref/ceiling pre-walk that already visits the same nodes in the same
order was implemented in full: the scan deleted, the list question asked at the
first list host the pre-walk meets, and every pre-walk refusal held rather than
thrown so an unbuildable list still outranks it. It passed 773 lynx tests, and
all five deliberate breaks went red — including the two ordering ones that had no
coverage before it (walk truncated where a refusal is held, held refusal overwritten by a later one).

It was declined on its own evidence:

round base publish fused publish
0 296.7 330.8
1 351.5 290.7
2 311.8 338.7
3 308.3 336.6

Median-of-medians 310.1 → 333.7, best-min 242.5 → 255.6, three of four rounds
favouring base, arms fully overlapping: a non-result that, if anything, leans
the wrong way. Reading the code back explains it — the deleted scan's comparisons
do not disappear, they move into the fused loop, so what is actually removed is
one loop's iteration and stack overhead, not the per-node work. A cut that
relocates work rather than removing it does not earn the ordering change it
needed (the list decline would have moved to after envelope validation), so it
is not in this diff.

The cut after it landed instead

planLynxHostPropPatch — the next-largest contained frame in publish, at
120.6 ms over 7 screens — turned out to be diffing every first-screen host
against an empty bag it derived seven constants from, 40,002 times per screen.
That one removes work rather than relocating it, and is open as #272, stacked
on this branch. The remaining candidate, the per-host state.generations.set(id, 1),
is sized but carries an incrementalCompact behaviour trade and is inside the
noise (321.9 against a 315.7 base); it belongs to the device round, which is where
the directive puts the performance verdict.

Does this change the sequence?

No. This is B under the #247 directive, taken after A (#269) as instructed,
and it is the second cut inside publish after #270. 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 why the cut had to remove work
rather than move it to capture. #68's capture deferral is unaffected and still
follows.

Provenance

  • An agent produced this diff (agent-authored)

`cloneProps` exists because a host prop bag normally arrives from the other
thread, a separate JS realm in production, so the container has to build data it
owns out of values it has proved it can hold. The direct first-screen path has
no such crossing: its bag was built by `universalProps` in this realm, on this
call stack, and frozen at its only construction site. When every value is a
scalar there is nothing to clone and no tagged callable to unwrap, so the
container holds the bag it was given.

Adoption tests each of those rather than assuming them, and a bag that fails any
one takes the copy: still mutable, reached through a class, or holding an object.
The prototype test asks for this realm's `Object.prototype` or none, not the
copy's looser cross-realm shape, because a prototype one hop from null can carry
enumerable properties of its own that `for...in` over a stored bag would see.

The `main-thread:` unwrap is now asked of a value rather than of a name. A
tagged callable is a function, so it can only appear where the copy already
stops for a non-scalar; the pass it replaces walked every key of every bag, and
on a 60,002-host page found nothing.

Publish at 10,000 rows falls from a 475.5 ms median-of-medians to 337.9 ms over
an interleaved four-round A/B, with the arms' medians not overlapping.

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