Repository navigation
perf(lynx): take the first screen off the command batch - #108
Conversation
The synchronous first screen materializes through direct Element PAPI emission. That applier needs the rendered node tree and the background listeners the renderer assigned; it never needed the command batch, and only ever read the batch's `event` commands to recover those listeners. Taking the batch as a parameter nonetheless obliged the renderer to build one first: at 10,000 fixture rows, 160,094 command objects, each frozen, of which 140,082 are walked past and dropped. `renderLynxFirstScreen` now returns an `envelope` carrying the renderer, the version, and exactly those listener bindings, and `applyLynxFirstScreenDirect` takes that instead. `batch` becomes a cached lazy property built from the same bindings, so the native-list fallback, the staged apply path, and any caller that reads the batch as a value are unchanged — they just build it when they ask for it. Counting hosts and assigning listener ids also collapse into one walk; they were two. The `event` commands are re-projected from the envelope's bindings rather than assigned a second time, so the ids the two paths see cannot drift no matter when, or whether, the batch is built. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
Four allocations happened once per record, on pages that can hold tens of thousands of them: - A whole prop patch was planned only to read back which main-thread events and refs the record expects. `state.hasMainThreadProps` is sticky and set by every path that accepts props from exactly the predicate that decides this, so a false value proves no record in the root can expect either. An unscoped raw `#text` record's props are a string value and nothing else, which is why `create` already takes the same shortcut. - An empty event table was spread and sorted into an array that stayed empty. Two of every seven fixture hosts bind an event; the rest paid three allocations for the same empty list. - An empty child list was copied and frozen. - A `Set`/`Map` pair was allocated to track cycles while cloning props. The capture now owns one pair and clears it per record, which leaves each record's clone memo behaving exactly as a fresh pair would. Every assertion the capture made still runs. The ref check in particular is unguarded, because it asserts the *absence* of an unexpected main-thread ref rather than the presence of an expected one, so skipping it would stop checking anything. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
`applyLynxFirstScreenDirect` takes an envelope now, and this caller was missed: it lives in `packages/octane/tests` and reaches the module by relative path, so the `lynx` vitest project never loaded it and `packages/lynx/package.json` not exporting `./core/*` said nothing about it. The four-cell differential threw `envelope.events is not iterable` on both direct cells. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
|
Summary
Issue #66 Phase B: take the first screen off the command batch.
The synchronous first screen materializes through direct Element PAPI emission.
That applier needs the rendered node tree and the background listeners the
renderer assigned; it never needed the command batch, and only ever read the
batch's
eventcommands to recover those listeners. Taking aUniversalHostBatchas a parameter nonetheless obliged the renderer to buildone first — at 10,000 fixture rows, 160,094 frozen command objects, 140,082 of
which the direct path walks past and drops.
renderLynxFirstScreennow returns anenvelopecarrying the renderer, theversion, and exactly the 20,012 listener bindings, and
applyLynxFirstScreenDirecttakes that.batchbecomes a cached lazy propertybuilt from the same bindings, so the staged apply path, the native-list
fallback, and any caller that reads the batch as a value are unchanged — they
just build it when they ask. The
eventcommands are re-projected from theenvelope's bindings rather than assigned a second time, so the listener ids the
two paths see cannot drift no matter when, or whether, the batch is built.
Counting hosts and assigning listener ids also collapse into one walk; they were
two.
First-tree capture lost four per-record allocations in the same slice, on pages
that hold tens of thousands of records:
the record expects —
state.hasMainThreadPropsis sticky and set by every paththat accepts props from exactly the predicate that decides this, so a false
value proves no record in the root can expect either, and an unscoped raw
#textrecord's props are a string value and nothing else (which is whycreatealready takes the same shortcut);every seven fixture hosts bind an event, the rest paid three allocations for
the same empty list;
Set/Mappair allocated per record to track cycles while cloning props;the capture now owns one pair and clears it per record, which leaves each
record's clone memo behaving exactly as a fresh pair would.
applyLynxFirstScreenDirectis not reachable through the package's exports(
packages/lynx/package.jsonhas no./core/*), but in-repo callers reach it bypath, and there is one outside
packages/lynx: the four-cell differential inpackages/octane/tests/lynx-target-first-screen.test.ts. My first pass missed itand it threw
envelope.events is not iterableon both direct cells; the fixcommit updates it. I had reasoned from the package exports, which say nothing
about a relative import, and validated only the
lynxvitest project — whichdoes not load that file. That file exists precisely to cover the seam between
the two packages, so it was the one caller most likely to be missed and least
affordable to miss.
Measurement
Same host, same session, alternated before/after/before/after over three rounds,
10,000 rows, n=30 per side, each phase timed on a collected heap so garbage
one phase produces is not billed to the next:
Two honest caveats on that table:
reads −8% overall with apply appearing 8% slower. That is not a
regression: the applier strictly does less work now (20,012 iterations instead
of 160,094), and the old arrangement's eager batch was being collected during
the apply phase. Forcing the collection out of the timed spans is what
separates the two, and it moves every phase into the win column. Both numbers
are reported because the un-isolated one is what an un-instrumented run sees.
result.batchat the end of every rep,which on the post-change build materializes the batch and leaves its garbage
for the next rep. That was a harness bug, fixed before these numbers; the
timed loop now never touches
batch.The deterministic half of the claim needs no statistics: the batch is unchanged
at 160,094 commands and byte-identical in shape (asserted by the new test),
and the direct path builds zero of them. Building it on demand costs 50.1 ms
when something does read it, which on the direct path is never.
The sanctioned FCP@10k A/B through
benchmarks/lynx-table/prototype/run-fcp.mjs(issue #66 §4) is now done, on #109, where it measures this branch plus the one
above it against
781e4fca3: median FCP 2751.6 → 2599.1 ms, paired mean−158.5 ms, t(29) = −4.94, 95% CI [−224.2, −92.9]. It is reported there
rather than here because the two branches were built and driven as one app.
Validation
pnpm typecheck:files(changed files)pnpm format:files(changed files)vitest run --project lynx --project octane --project octane-prod— the
lynxproject alone is what let the missed caller through, so thetwo projects that compile and execute
target: 'lynx'output now run toonode --test benchmarks/lynx-table/stages/instrument-source.test.mjs— thestage harness patches
main-renderer.tsandmain-thread.tsby exactstring, and its own round-trip test confirms every anchor survives this diff
pnpm test— full run at935d6e3d(this head plus perf(lynx): build first-screen host records without spread-and-delete #109): 2182/2196files, 20,954 tests passing. No
packages/lynxorpackages/octanefailure; all 14 failures are timeouts or browser/process flakes on a
loaded host, with per-package attribution in a comment below.
pnpm sync— no generated changesRed–green
Every claim below was broken deliberately on the committed state and restored:
One new behavioral test,
paints the same first screen whether or not the command batch was materialized, pins the contract that is genuinely new here:reading
batchbefore painting must not change the painted tree, the adoptionsnapshot, the event journal, or the PAPI shape, and a deferred read must still
yield the same commands.
Risk / follow-ups
captureLynxFirstTreehave no oracle — themissing main-thread eventandretains main-thread eventchecks. Deletingthat loop leaves the suite green. I verified this is pre-existing, not
introduced here: the same deletion applied to the unmodified capture code
is also green. My guard sits in front of them, so a scene that exercises them
would be worth adding, but writing negative controls for them is out of scope
for a perf slice and belongs in its own change. The guard itself is covered:
forcing it vacuously false goes red through the ref assertion.
clones are deep-frozen, so cross-record aliasing is unobservable through any
API. The
clear()bounds memory rather than correcting semantics, and per.claude/rules/testing.mdallocation claims belong in the benchmark system,not a correctness test.
that exercises
target: 'lynx'compiler output through the host driver livesin
packages/octane/tests, so nopackages/lynxchange is fully validated bythe
lynxproject alone.test/lynx-u0-block-counts(test(bench): re-run the #103 U0 ceiling against the real block core #107). Not for merge ahead of its base.first-screen records from being built by spread-and-delete, and carries the
FCP record for both.
Provenance
agent-authored)Generated by Claude Code