Skip to content

perf(lynx): take first-tree capture off the first-paint path - #751

Merged
trueadm merged 3 commits into
octanejs:mainfrom
Huxpro:claude/lynx-first-screen-capture-perf
Aug 13, 2026
Merged

trueadm merged 3 commits into
octanejs:mainfrom
Huxpro:claude/lynx-first-screen-capture-perf

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Capturing the Lynx first tree runs after the page is already published to the
host, so everything it does sits between the tree reaching the DOM and the
browser painting it.

Two commits take work out of that window without changing behaviour.

1. Skip prop-patch planning for hosts that own nothing on the main thread.
Capture re-plans every painted host's props only to recover what the main thread
should own: a main-thread: event handler or main-thread:ref. Both are the
only ways to declare it and both carry that prefix, so a host without one is
answerable by a prop-name test rather than a full patch plan — and the props
themselves were already planned and applied when the tree was built. The
surrounding checks are untouched, including the one that matters when planning
is skipped: a host that never asked for main-thread ownership must still have
none mounted.

2. Describe the first tree after the paint, not before it. Most of the
window is not validation but allocation — turning the captured tree into the
clone-safe description the background clones when it adopts. Nothing before
adoption reads it, so it now builds on first read. Capture splits along
validation-versus-allocation rather than eager-versus-lazy, which is what keeps
the contracts intact:

  • eager — structural validation and the native-ID read, because a host that
    cannot be captured must still fault the synchronous first screen while its
    caller holds the source to retry cleanup against; plus the event token map,
    because a tap on the painted tree resolves through it long before adoption;
  • deferred — the description, which reads only record fields that teardown
    retains, so firstScreenSnapshot() still answers while unmount cleanup
    retries.

Relationship to #748–#750

Measurements

Re-measured independently on current upstream main (ecc7700b) with the
fork-only #52 Element PAPI harness. The harness was cherry-picked only into a
scratch branch and is not part of this PR.

  • host: 32× Intel Xeon Platinum 8336C @ 2.30 GHz
  • Chromium 149.0.7827.55, Node v22.22.2
  • 10,000-row first screen; production bundles built side by side before
    sampling
  • one browser session, fresh page per sample, alternating AB/BA order, n=8
  • result reported as median of paired deltas
  • semantic control on every sample: 10,000 rows, checksum 996039633, two
    flushes
candidate vs base FCP paired median improved pairs post-publication paired median improved pairs
both commits −106.35 ms 8/8 −96.55 ms 8/8
commit 1 only −73.65 ms 7/8 −49.20 ms 8/8

Commit 1 resolves positive on this quieter host, so both commits remain in the
PR. The sensitive post-publication metric is flush #0 end → flush #1 start.
The measurement used the harness's counts probe, which retains exact flush
start/end epochs without adding a clock read to every Element PAPI call.

Validation

  • packages/lynx suite on the exact fork head: 25 files, 340/340 passed.
  • Current upstream main + this PR: packages/lynx 340/340 passed.
  • Lynx Web host Playwright smoke on both the exact head and current-main
    integration: 3/3 passed, including native tap, gallery scrolling, and a
    main-thread handler flush.
  • pnpm test -- --silent=passed-only: 2,179/2,181 files and
    20,916/20,921 tests passed. The five failures were fixed-5-second timeouts in
    unrelated CLI and website-MCP suites under the full 276-workspace run; the
    two affected files then passed 17/17 when rerun together (slowest 1.28 s).
    No browser project failed.
  • Deliberate break 1: forcing hasLynxMainThreadProp to always return
    false makes transfers a compatible first tree without allocating or restructuring native nodes fail.
  • Deliberate break 2: returning no described nodes produces exactly 14
    failures across first-screen, host-driver, and
    milestone-8-integration.
  • speed up CI #52 harness self-tests: 23/23 passed.
  • Independent paired A/B reproduced first-paint improvement in the same
    direction with identical semantic checksums.
  • pnpm typecheck:files on the three changed source files.
  • pnpm format:files:check on all five changed files.

Risk / follow-ups

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code


Note

Medium Risk
Touches first-screen capture and adoption contracts in core Lynx; behavior is intended to be identical but timing and memory retention paths change around snapshot build and release.

Overview
Moves work out of the post-publication, pre-paint window for Lynx first-tree capture without changing observable behavior.

During capture, hosts that have no main-thread: props no longer run a full planLynxHostPropPatch pass; hasLynxMainThreadProp gates planning while the same main-thread event/ref consistency checks still run (using a shared empty event list when planning is skipped).

The clone-safe first-tree snapshot is no longer built synchronously at the end of capture. Capture still validates structure, native IDs, and event tokens eagerly, but createLynxFirstTree now takes a describe callback and exposes snapshot via a getter that materializes once on first read. releaseLynxFirstTree clears describe so released trees do not retain the source container.

Two patch changesets document benchmark impact on large first screens (e.g. lynx-table @ 10k rows).

Reviewed by Cursor Bugbot for commit 79ec1b7. Bugbot is set up for automated code reviews on this repo. Configure here.

Huxpro and others added 2 commits August 13, 2026 10:18
…nership

First-tree capture re-plans every painted host's props, but only to recover
what the main thread should own: a `main-thread:` event handler or
`main-thread:ref`. Those are the only two ways to declare it and both carry
the `main-thread:` prefix, so a host without one can be answered by a
prop-name test rather than a full patch plan. The props themselves were
already planned and applied when the tree was built; this call rediscovers
nothing else.

The surrounding checks are unchanged, including the one that matters when the
plan is skipped: a host that never asked for main-thread ownership must still
have none mounted.

This sits on the first-paint path. Capture runs after the page is published to
the host, so every millisecond it spends is paint delay — the host-boundary
instrument in benchmarks/lynx-table measures that window at 16.7-20.1% of FCP
across 1k/10k/30k rows. In the benchmark's 10,000-row first screen no host
declares a `main-thread:` prop, so all 70,041 skip planning and the
capture-time planning cost falls from a 58.3 ms median to 28.8 ms on the same
instrument. End to end that is about 1.7% of first paint, below what the
harness resolves on a 4-CPU host, so no wall-clock claim is made for it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UMisRkW6cGfodXwpy4kxYS
Capturing the first tree runs after the page is already published to the
host, so everything it does sits between the tree reaching the DOM and the
browser painting it. The host-boundary instrument measures that window at
16.7-20.1% of FCP across 1k/10k/30k rows.

Most of it is not validation, it is allocation: turning the captured tree
into the clone-safe description the background clones when it adopts. Nothing
before adoption reads that description, so it now builds on first read.

What stays eager is what the contracts need:

- validation and the native-ID read, because a host that cannot be captured
  has to fault the synchronous first screen while its caller still holds the
  source to retry cleanup against; and
- the event token map, because a tap on the painted tree resolves through it
  long before the background adopts.

The description therefore reads only retained record fields, which is why
`firstScreenSnapshot()` still answers while unmount cleanup retries rather
than describing a container mid-teardown.

Measured on the lynx-table 10,000-row first screen, paired A/B with both
bundles built side by side and alternating order, n=8: the post-publication
window falls from a 217 ms median to 163 ms with every pair improving, and
FCP@10k from 1,576 ms to 1,483 ms (paired median -85 ms).

Deliberately breaking the description turns 14 tests red across
first-screen, host-driver, and milestone-8-integration, so both read orders —
first read at adoption, and first read from the public accessor before it —
are already covered.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UMisRkW6cGfodXwpy4kxYS
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Aug 13, 2026
@trueadm
trueadm marked this pull request as ready for review August 13, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants