Skip to content

perf(lynx): install a nodes-ref selector only where one was requested - #129

Merged
Huxpro merged 2 commits into
new-lynxfrom
perf/lynx-nodes-ref-on-demand
Aug 21, 2026
Merged

Huxpro merged 2 commits into
new-lynxfrom
perf/lynx-nodes-ref-on-demand

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

A nodes-ref selector is now installed only where a public instance was
requested, and it follows its logical host across native <list> recycles.
One steady-state recycle costs the slot-write floor exactly.

row writes per recycle before after floor ratio
list-item > text > #text 6 2 2 3.00x → 1.00x
card row with a nested badge 15 3 3 5.00x → 1.00x

setRefSelector no longer appears in a steady-state recycle at all. Both
benchmarks/baselines/ratios.json ceilings drop from 3 and 5 to 1.

Why

#128 measured the residual over the floor and named its three sources. Two calls
per element node per recycle — cleared at enqueueComponent, reinstalled at
componentAtIndex — plus three places in main-thread.ts that installed a
selector as a side effect of asking an unrelated question, because
driver.getPublicInstance installs one:

  • publishing a handle for every update command in a commit;
  • resolving a first-screen native event target;
  • filtering the attachment stream, which asks about every node of every cell
    on every recycle.

#128's ablation showed why installing lazily at the create/rebind paths alone
buys nothing: the attachment predicate reinstalls what the enqueue cleared, so
the same calls move into a different column. The floor is reachable only if the
predicate stops installing too — and that is what makes this a protocol change
rather than a local one.

The protocol already carries the request. ensure-public-instance is emitted by
universal-core for exactly the hosts that can obtain a handle (a ref, a
lifecycle, or a local callback), so this adds zero extra commands to the
wire. What was missing was that the main thread treated the announcement as
advisory and installed eagerly anyway.

Changes

  • host-driver.ts: a record carries a sticky selectorWanted bit beside its
    per-node selectorInstalled bit. ensure-public-instance and
    getPublicInstance set it; the list-cell create and rebind paths install only
    for hosts that asked; the enqueue-time clear only clears what was installed.
    The want outlives the node, because a detached cell owns none and nothing
    re-renders to re-announce the request.
  • New getLynxHostHandle(container, id): one host's identity without
    requesting a public instance. The three predicates above use it, which leaves
    acknowledgementHandles with no use for the driver at all, so that parameter
    goes.
  • protocol.ts: lynxLazyPublicInstancesNegotiated(capabilities) — the single
    derivation of the negotiated flag, now used by both peers. The main thread
    goes demand-only only when it holds, and client-driver.ts sets its own
    lazyPublicInstances from the same call. Each half is unsound without the
    other: a main thread that skips the selector while the background skips the
    announcement strips refs with no error anywhere.
  • A peer below LYNX_LAZY_PUBLIC_INSTANCE_READY_REQUEST_BASE, or one that
    announced no capabilities, keeps the eager install. For it an uninstalled
    selector is a ref that addresses nothing.
  • Tests: two unit scenarios for the demand and fallback regimes, plus the
    existing end-to-end recycled-list scenario in background-list.test.ts
    strengthened to assert that a row's own handle still addresses the cell
    showing it, across a real recycle over the real transport.

Only the native-list paths (createPhysicalTree / rebindPhysicalTree) became
demand-driven. Ordinary create, recreate, and first-screen adoption still
install eagerly; making those lazy too is the broader change and is not in
this diff.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test — 21,052 pass, 3 fail, none in a package this diff
    touches. git diff base..head --name-only lists only packages/lynx/,
    benchmarks/, and a changeset, so all three suites read byte-identical
    inputs on both sides. All three are already attributed as this container's
    persistent failures in
    #125's per-file attribution,
    which reproduced them on two branches sharing no code:
    rspeedy-plugin native-demo (stopProcessTree reaping in a Linux
    container, pre-existing per Execution plan (#58): complete L2, then drive L4→L6 — full context handoff #66 §2.3), drei-adapted-browser
    upstream-e2e (an external .hdr asset this container cannot fetch), and
    octane-events-browser suspense-hydration ([0, 0] instead of
    [2, 9]). I re-ran the last one alone on a quiet host and it still fails,
    which matches docs(bench): measure what the L5 main-thread repayment is worth #125 placing it in the persistent rather than the
    load-sensitive class. docs(bench): measure what the L5 main-thread repayment is worth #125's fourth, dropzone probes/hydration, did not
    fail in this run.
  • targeted: vitest run --project lynx — 32 files, 443 tests, all pass
  • targeted: node benchmarks/bench.mjs --quick --ratios lynx-list — 3/3
    applicable guards pass at the new 1.00x ceilings

Both oracles were checked against a broken implementation, not just asserted:

  • On pre-change source the new unit test reports four addressable hosts nobody
    asked about; on this change it reports none.
  • Ablating the sticky bit (demand-install that does not remember) turns the
    end-to-end scenario red at expected null to be 'Row 0' — the row was
    announced at mount, a thousand rows before any cell existed.

Risk / follow-ups

  • Rows that carry a ref, a lifecycle, or a main-thread callback are announced,
    and each announced node still pays the clear-and-reinstall pair.
    The saving
    is per node nobody queries, not per row; the benchmark's rows carry none, so
    the headline numbers are the best case, not the average one.
  • The announcesPublicInstances fallback is covered by a regression test but is
    not measured by the benchmark suite.
  • Stale-selector audit: every path that gives a record a physical node either
    creates a fresh node or reuses one that clearPhysicalTreeAttachment cleared,
    and both re-entry points reset selectorInstalled first, so a demand-skipped
    install cannot leave a node answering an old selector. detachListCell clears
    on all four pooling modes; recreate skips hosts with no active node, so a
    detached cell is never materialized outside the list path.
  • Eager install sites remain by design: the ordinary create and recreate
    ops, first-screen direct emission, and first-tree adoption. Every node they
    produce gets a selector whether or not anything ever queries it. The
    template-run path is already conditional, so how much that is worth depends on
    how much of a first screen mounts through templates — which is unmeasured, and
    is the next thing to measure rather than to assume. (The third incidental site
    this PR removed, the per-update handle publication, is not part of that
    remainder; it is fixed here.)

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

A native <list> cell used to clear and reinstall a nodes-ref selector on
every element node of every recycle, and three predicates in main-thread.ts
installed one as a side effect of asking an unrelated question, because
driver.getPublicInstance installs as it reads.

A record now carries a sticky selectorWanted bit beside its per-node
selectorInstalled bit, set by ensure-public-instance and by getPublicInstance.
The list-cell create and rebind paths install only for hosts that asked, the
enqueue-time clear only clears what was installed, and the three predicates use
a new non-installing getLynxHostHandle. The want outlives the node because a
detached cell owns none and nothing re-renders to re-announce the request.

Both peers derive the regime from one negotiated flag, now single-sourced as
lynxLazyPublicInstancesNegotiated in protocol.ts: a peer that announces no
public instances keeps the eager install, because for it an uninstalled
selector is a ref that addresses nothing. The wire is unchanged --
universal-core already announced exactly the hosts that can obtain a handle.

One steady-state recycle now costs the slot-write floor exactly: 2 Element PAPI
writes on the 3-node row and 3 on the 9-node row, down from 6 and 15. The
lynx-list ratio ceilings drop from 3 and 5 to 1.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
Huxpro pushed a commit that referenced this pull request Aug 21, 2026
The demand rule from #129 covered only native list cells. Extending it to
ordinary create, recreate, and template-run mounts drops the benchmark table
from 4,000 nodes-ref selector writes to 0 at 1,000 rows, and from 40,000 to 0
at 10,000.

Reading the session's negotiated lazyPublicInstances capability to decide it
is unsound. A background composes its first batch before the main-ready reply
granting the capability reaches it, and dispatches it after, so that batch
names none of the hosts it will query however the session was negotiated. The
main thread, reading the reply it had already sent, skipped their selectors:
the gallery example's <list ref> mounted unaddressable and its auto-scroll
invoke failed with native code 2.

Commits now carry the promise themselves in a new optional `announces` field,
stamped from the capability captured when the batch was composed. The main
thread latches its container into demand installs on the first commit that
makes it, which is sound because delivery is FIFO. `instances` is decided from
the same compose-time capability rather than re-read at dispatch, closing the
same silent ref loss for a compact template-only first batch.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
@Huxpro
Huxpro changed the base branch from perf/lynx-demand-nodes-ref-selector to new-lynx August 21, 2026 22:18
@Huxpro
Huxpro marked this pull request as ready for review August 21, 2026 22:18
@Huxpro
Huxpro merged commit b908ab4 into new-lynx Aug 21, 2026
1 check 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