Skip to content

test(bench): measure what one native-list recycle costs at the Element PAPI - #119

Merged
Huxpro merged 2 commits into
new-lynxfrom
test/lynx-list-reuse-work
Aug 21, 2026
Merged

Huxpro merged 2 commits into
new-lynxfrom
test/lynx-list-reuse-work

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner

Summary

Issue #103, step U3a — the ceiling measurement §5 puts before U3's implementation slices. It is independent of the #66 stack and based directly on new-lynx.

benchmarks/lynx-list/ already counts how many cells are reused (12 physical cells, 988 reuses, 1,000 logical rows). It never counted what a reuse costs, which is the number U3 is named for: recycling a row by writing only the slots whose value differs, instead of rebinding its physical subtree node by node.

This adds that counter. Every fake Element PAPI entry point routes through one recorder, and one sample is taken around each scroll step's enqueueComponent + componentAtIndex pair. The result is reported against a slot-write floor — write the values that differ between the outgoing row and the incoming one, touch only the nodes that own them — derived from the fixture's own row description rather than restated as a constant.

A second row shape runs beside the existing one, because one shape cannot separate two very different answers: the write path tracks the changed values versus it tracks the row's size.

The measurement

narrow row  list-item[item-key*, reuse-identifier] > text > #text(title*)
wide row    list-item[item-key*, reuse-identifier]
              view[class, style]
                text[class]   > #text(title*)
                text[class]   > #text(subtitle*)
                view[class]
                  text[class] > #text("NEW")
row nodes element nodes varying values writes / recycle floor ratio
narrow 3 2 2 6 2 3.00x
wide 9 6 3 15 3 5.00x

Zero variance: all 988 samples of each shape are identical (writes 6-6, writes 15-15).

Broken down by entry point, one steady-state recycle is:

narrow wide
setAttribute 2 3
setRefSelector 4 12
getUniqueId 2 2
flush 1 1
createElement / insert / remove / replace 0 0

What it says

setAttribute is 2 and 3 — the floor exactly, at both widths. planLynxHostPropPatch already diffs, so rebindPhysicalTree writes only the values that actually changed, and it does so independently of how many static props and extra nodes the row carries. There is no prop-write slack for a slot table to recover.

The entire gap is setRefSelector: two calls per element node per recycle — clearPhysicalTreeAttachment blanks the nodes-ref selector when a cell is enqueued, and ensureNodesRefSelector reinstalls it for the incoming logical host. The selector encodes the logical host id (r{root}-h{id}-g{generation}), which necessarily changes when one physical cell is re-pointed at a different logical row. So per-recycle PAPI traffic is bounded by the row's element count, not its prop count.

This is a NO-GO for U3's recycling-by-slot-write premise on this workload, and the U3 design's §3 claim that reuse costs "proportional to the row's node count and prop count" is wrong for the prop half. A template-described row would still have to rewrite the selector, because it still changes logical identity — so the residual is not something slot tables address. I am recording the verdict here rather than softening it; §5's gate says the step stops if the current path is already near the floor.

It does name a separate, better-scoped question for later: every host element gets a nodes-ref selector installed eagerly, whether or not any query can ever name it. On the wide row that is 12 of 15 writes per recycle, 80%, paid by pages that never call SelectorQuery. Making selector installation demand-driven is its own design and its own PR.

Guards

Both ratios are committed to benchmarks/baselines/ratios.json, so the finding is a gate rather than a note — a regression in the prop diff shows up as a breach, and an improvement to selector lifetime shows up as headroom.

Validation

  • node benchmarks/bench.mjs --quick --ratios lynx-list → PASS, 3/3 guards checked (physical_cells 0.012x eager unchanged, reuse_writes 3.00x, wide_reuse_writes 5.00x)
  • vitest run --project lynx → 441 passed (32 files)
  • pnpm sync — no generated changes
  • pnpm format:files on every changed file
  • pnpm typecheck — not run for this diff: no changed file is in any TypeScript project (benchmarks/lynx-list/workload.ts is bundled by esbuild and is not in a tsconfig). Checked standalone with the same tsgo flags before and after: the two pre-existing errors (getParent/isEqual missing from the fake PAPI, setEvent's listener union) are identical on new-lynx, and this diff adds none.
  • pnpm test — not run for this diff; it touches only benchmarks/, and the two rspeedy-plugin failures issue Execution plan (#58): complete L2, then drive L4→L6 — full context handoff #66 §2.3 records as pre-existing in this container are unrelated to it.

Risk / follow-ups

  • The counters are Element PAPI call counts, not CPU. They cannot see the work the host spends deciding what to write — rebindPhysicalTree still walks the subtree and builds a prop patch per node even when it emits nothing. A row-width sweep of that cost would need a timing harness, and this suite is deliberately not one.
  • The wide row is authored as raw universal commands, like the existing narrow one, so it exercises the record path rather than a compiled template. A template-lowered row is U3b's subject, not this measurement's.
  • slot-write-floor is a target with no implementation behind it. It exists to be a denominator; it is not a claim that 3 writes is reachable while nodes-ref selectors keep their current lifetime.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

…t PAPI

The lynx-list benchmark counted how many cells are reused. It did not count
what a reuse costs, which is the number issue #103's U3 step is named for:
recycling a row by writing only the slots whose value differs, instead of
rebinding its physical subtree.

Count every fake Element PAPI entry point and sample one window around each
scroll step's enqueueComponent + componentAtIndex pair, reported against a
slot-write floor derived from the fixture's own row shape. A second row shape
runs beside the existing one — 9 nodes, 3 varying values, 6 static props —
because one shape cannot separate "the write path tracks the changed values"
from "it tracks the row's size".

The answer is the same at both widths: setAttribute is 2 and 3, which is the
floor exactly. The whole gap is setRefSelector, two calls per element node per
recycle, clearing the nodes-ref selector when a cell is enqueued and
reinstalling it for the incoming logical host. Per-recycle PAPI traffic is
bounded by the row's element count, not its prop count, and the prop write path
has no slack in it.

Both ratios are committed as guards so the finding is a gate rather than a note.
They are deterministic call counts, not timings, and they do not measure the CPU
the host spends deciding what to write.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy

Huxpro commented Aug 21, 2026

Copy link
Copy Markdown
Owner Author

Answering this PR's follow-up: demand-driven selector installation — NO-GO as a local change

This PR's body nominated the residual as its own design and its own PR:

every host element gets a nodes-ref selector installed eagerly, whether or not any query can ever name it. On the wide row that is 12 of 15 writes per recycle, 80%, paid by pages that never call SelectorQuery. Making selector installation demand-driven is its own design and its own PR.

I took it, implemented it, and reverted it. The finding is worth more than the patch was.

Demand-driven installation already exists

It is not missing — it is already built, and already correct:

  • universal-core.ts emits { op: 'ensure-public-instance', id } for exactly the hosts that carry a ref, a lifecycle, or a local callback.
  • host-driver.ts applies that command through ensureNodesRefSelector, which is also what getPublicInstance calls.
  • A negotiated compact program mount therefore installs no selectors at mount, and exactly one when a host is asked for. installs only requested public-instance selectors for negotiated compact program hosts pins that end to end.

So the eager installs are not an oversight. They are the fallback for every regime that is not that one.

Why the gate is where it is

Laziness requires both compact: true and lazyPublicInstances: true. keeps program selectors eager unless a safe compact mount explicitly negotiates laziness asserts this directly: with { lazyPublicInstances: true } alone — on a batch that is a single mount-template-range, nothing mixed about it — the selector is still eager.

The reason is sound, and it is about the background, not about main. Only under a compact acknowledgement does the background derive its handles locally from the batch (compactHandle), which makes ensure-public-instance its sole route to a queryable host. In every other regime main publishes handleDelta, the background holds handles it never asked for, and handleBinding will happily build [octane-ref=r{root}-h{id}-g{generation}] for any of them. Skipping the install there is a silent node-not-found, not a saving.

Why a native list can never reach that regime

compactCandidate requires !state.hasNativeListTopology, and that flag is monotonic per container: one native <list> disqualifies the whole root for its lifetime.

Meanwhile createPhysicalTree, rebindPhysicalTree and clearPhysicalTreeAttachment — the three functions that spend the 12 writes — are reachable only from the native-list cell machinery (detachListCell, and the materialize/reuse pair behind componentAtIndex).

The two populations are disjoint by construction. The only code that recycles is the only code that can never be lazy. That is the whole finding, and no amount of local rework in host-driver.ts changes it.

What I actually built, and what it cost

A sticky selectorRequested bit on LynxHostRecord, ensureNodesRefSelector split from a new bindNodesRefSelector (install on a freshly bound node only when requested), a guarded blank in clearPhysicalTreeAttachment, and a monotonic state.demandPublicInstances latched from either proof that the peer announces.

--project lynx went 441 → 14 failed. Four survived after I fixed a real bug of my own — selectorInstalled memoizes the attempt, so raw-text records carry it as true even though installNodesRefSelector returns early for them, and my guarded blank lost the independent type check and called setRefSelector on text nodes.

Those last four are not stale assertions. They are the deliberate contract above, and my latch was reading lazyPublicInstances as "the peer asks before it queries" when it means no such thing on its own. Reverted; --project lynx is 32 files / 441 passed on this branch untouched. No ratio guard moved, because no code moved.

What it would actually take

Both options are protocol-level, and both are about when the background becomes able to query — not about when main installs:

  1. Withhold queryability until asked, everywhere. Make handleBinding on the background require the same ensure-public-instance round trip that the compact regime already forces, so the invariant stops being compact-only and becomes uniform. No new message type — the command exists. Cost is one commit round trip before the first query on a host, once per host per generation.
  2. Install as part of the NodesRef operation. The first invoke for a host round-trips to main, which installs and replies, then the query runs. LynxNodesRef already documents that its promise "resolves once Lynx accepts the selector-query submission", so the extra hop sits inside the published contract.

I recommend 1. It reuses a command that already exists, it deletes a special case rather than adding one, and it makes every regime behave the way the compact one already does. 2 keeps the change inside nodes-ref.ts but puts a round trip on a latency-sensitive path and leaves the two regimes divergent.

The prize, if it is taken

Per steady-state recycle, on this PR's own counters:

row writes now setRefSelector now writes after floor ratio now → after
narrow 6 4 2 2 3.00× → 1.00×
wide 15 12 3 3 5.00× → 1.00×

That is the slot-write floor this PR defined, reached exactly — and reached by fixing selector lifetime, which this PR correctly identified as a different lever from the slot tables it ruled out.

This needs a decision before anyone writes it, so I am leaving it here rather than opening a PR that changes the protocol on my own initiative.


Generated by Claude Code

The narrow and wide reuse workloads compare their numbers against each
other, so the begin/leave/sampleSoFar/enter/end window must be the same
on both arms; it was copy-pasted, and a future edit to one loop would
silently skew one arm. sampleRecycle() now owns the window for both.
Reported numbers are unchanged (ratios 3.00x/5.00x reproduce).

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YHfFsy9CLaRovTFH8pTGyY
@Huxpro
Huxpro marked this pull request as ready for review August 21, 2026 21:02
@Huxpro
Huxpro merged commit e25722d into new-lynx Aug 21, 2026
18 of 19 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