Skip to content

perf(lynx): take the first screen off the command batch - #108

Merged
Huxpro merged 5 commits into
new-lynxfrom
perf/lynx-first-screen-batch
Aug 20, 2026
Merged

Huxpro merged 5 commits into
new-lynxfrom
perf/lynx-first-screen-batch

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 20, 2026 •

Copy link
Copy Markdown
Owner

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 event commands to recover those listeners. Taking a
UniversalHostBatch as a parameter nonetheless obliged the renderer to build
one first — at 10,000 fixture rows, 160,094 frozen command objects, 140,082 of
which the direct path walks past and drops
.

renderLynxFirstScreen now returns an envelope carrying the renderer, the
version, and exactly the 20,012 listener bindings, and
applyLynxFirstScreenDirect takes that. batch becomes a cached lazy property
built 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 event commands are re-projected from the
envelope'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:

  • a whole prop patch 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, and 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 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 copied and frozen;
  • a Set/Map pair 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.

applyLynxFirstScreenDirect is not reachable through the package's exports
(packages/lynx/package.json has no ./core/*), but in-repo callers reach it by
path, and there is one outside packages/lynx: the four-cell differential in
packages/octane/tests/lynx-target-first-screen.test.ts. My first pass missed it
and it threw envelope.events is not iterable on both direct cells; the fix
commit updates it. I had reasoned from the package exports, which say nothing
about a relative import, and validated only the lynx vitest project — which
does 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:

phase before after Δ min Δ p25 Δ med
first-screen render 205.9 ms 134.9 ms −34.5% −33.6% −37.3%
direct apply 468.2 ms 412.4 ms −11.9% −12.2% −11.7%
first-tree capture 291.8 ms 213.8 ms −26.7% −28.4% −30.2%
direct-path total 965.9 ms 761.1 ms −21.2% −21.8% −23.1%

Two honest caveats on that table:

  • Without the phase isolation, the same alternated experiment (n=36/side)
    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.
  • An earlier version of the harness read result.batch at 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)
  • targeted: vitest run --project lynx --project octane --project octane-prod
    — the lynx project alone is what let the missed caller through, so the
    two projects that compile and execute target: 'lynx' output now run too
  • node --test benchmarks/lynx-table/stages/instrument-source.test.mjs — the
    stage harness patches main-renderer.ts and main-thread.ts by exact
    string, and its own round-trip test confirms every anchor survives this diff
  • pnpm test — full run at 935d6e3d (this head plus perf(lynx): build first-screen host records without spread-and-delete #109): 2182/2196
    files, 20,954 tests passing
    . No packages/lynx or packages/octane
    failure; 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 changes

Red–green

Every claim below was broken deliberately on the committed state and restored:

break result
batch builder mints fresh listener ids instead of re-projecting the envelope's red — 4 tests, incl. the staged-vs-direct equivalence oracle
event collection drops the visibility gate red — 2 tests, incl. the hidden-Activity oracle
batch builder mutates the (deliberately unfrozen) listener bindings in place red — 2 tests, incl. the new materialization-order test
empty-children fast path inverted red — 7 tests
events never collected red — 5 tests
main-thread patch guard forced vacuously false red — 1 test, via the main-thread ref assertion

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 batch before painting must not change the painted tree, the adoption
snapshot, the event journal, or the PAPI shape, and a deferred read must still
yield the same commands.

Risk / follow-ups

  • Two pre-existing assertions in captureLynxFirstTree have no oracle — the
    missing main-thread event and retains main-thread event checks. Deleting
    that 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.
  • The clone-scratch reset cannot be red–greened, and shouldn't be: both
    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.md allocation claims belong in the benchmark system,
    not a correctness test.
  • The missed caller above is a coverage-shape problem worth naming: the only test
    that exercises target: 'lynx' compiler output through the host driver lives
    in packages/octane/tests, so no packages/lynx change is fully validated by
    the lynx project alone.
  • Stacked on 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.
  • perf(lynx): build first-screen host records without spread-and-delete #109 stacks on this branch and continues the same slice: it stops the
    first-screen records from being built by spread-and-delete, and carries the
    FCP record for both.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

Huxpro and others added 3 commits August 20, 2026 07:40
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

Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner Author

pnpm test — the run this PR's Validation section owed

One full run at 935d6e3d on the stack tip, which contains this branch's head
0d64af8:

Test Files  14 failed | 2182 passed (2196)
     Tests   8 failed | 20954 passed | 75 skipped (21037)
  Duration  3782.40s

No packages/lynx test failed and no packages/octane test failed, which
is the result this diff needed — packages/octane/tests/lynx-target-first-screen.test.ts,
the four-cell differential the envelope change broke and 0d64af8 fixed, passes
in the project that actually loads it.

All 14 failures are timeouts or browser/process flakes on a host that was
running several Playwright suites concurrently on 4 CPUs (load average 4–6
throughout): Hook timed out in 10000ms in SSR/bundling beforeAlls (aria ×2,
alien-signals, octane react-hosted/web3-island), Test timed out in
adapter-vercel / dropzone / syntax-highlighter / docusaurus, five
real-browser suites (website ×3, doom, octane browser suspense-hydration,
drei browser e2e), and rspeedy-plugin native-demo, which #66 §2.3 records
as pre-existing in this container.

The attribution is checkable rather than asserted: of those failing packages,
exactly one depends on @octanejs/lynx — rspeedy-plugin-octane — and its
failing assertion is about killing a detached process tree. The other twelve
have no lynx dependency at all.

The FCP@10k A/B this PR listed as owed is now done and reported on #109, which
measures the whole stack against 781e4fca3: −158.5 ms paired mean, t(29) =
−4.94
, median 2751.6 → 2599.1 ms.


Generated by Claude Code

@Huxpro
Huxpro changed the base branch from test/lynx-u0-block-counts to new-lynx August 20, 2026 19:56
@Huxpro
Huxpro marked this pull request as ready for review August 20, 2026 19:56
@Huxpro
Huxpro merged commit d58f340 into new-lynx Aug 20, 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