Skip to content

perf(lynx): stop describing unbinds a teardown's own destroy subsumes - #245

Merged
Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-clear-skips-redundant-unbinds
Aug 30, 2026
Merged

Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-clear-skips-redundant-unbinds

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

E2 proper for #230, against #241's cost oracle. Stacked on #243 (the
teardown-mirror differential), which is the control this change is measured
against — review #243 first.

#241 says attribute first: "count them per row and price them." So I did.

Attribution

A deterministic probe (FakeNode host, papi.calls tally) over the lynx-table row
shape — view.row + three text cells + their text carriers = 7 hosts, two of
them tappable — priced a clear at exactly 3 host calls per row, invariant
across 500/1000/2000 rows and across teardown routes:

per row call observable?
2 setEvent (listener detach) no
1 remove (root removal) yes
0 destroy — Lynx has no destroy PAPI op

At 10k rows that is 30,001 calls, of which 20,000 describe listeners on hosts
that no longer exist by the end of the commit
— 66.7% of the clear's entire
host-boundary traffic.

Why the detaches are not observable

Not an assumption — resolveLynxHostNativeEvent already refuses a token on any
of: the record is gone (deleteRecord at destroy), the generation moved on, or
the host is no longer root-connected (remove, which precedes destroy). A
destroy trips all three. A binding left installed on a detached element cannot
reach a listener
.

And the codebase already relies on this: prepareDenseTeardown wraps its entire
event-detach loop in if (!plan.direct), so the certified direct teardown plan
has skipped these unbinds since it was written. This change carries that same
property to every other teardown route.

That matters because plan.direct is unreachable exactly when it would help
most. Instrumenting the dispatch site showed a bench-shaped paint→clear resolves
dense: false, mirror: true — records stay a map, so the direct plan is never
even attempted and the clear pays full freight. Keeping the compactHosts
refusal (as the ruling requires) means that is the normal route, not a corner.

What changed

removeNativeEvent gains a teardown mode that skips the PAPI unbind and the
superseded-binding scan. It is passed at the three teardown sites: the certified
plan's detach loop, the destroy lowering, and — as an outright skip of the
journalled operation — the event command lowering when the same batch destroys
that host.

Deliberately preserved:

  • Main-thread worklet release is never skipped. The engine holds those; only
    an explicit release() ends their lifetime, whatever happens to the element.
  • Native-event bookkeeping is still cleared, so no state.nativeEvents entry
    outlives its record.
  • Any native list topology keeps the explicit unbind. A list recycles
    elements the driver does not own and can hand one back still bound. Read live
    at each use rather than snapshotted, since a list can appear inside the very
    batch being lowered, and the two readers run in different phases.

Result

A clear costs one host call per row, whatever the row carries. At 10k rows:
30,001 → 10,001.

Validation

  • pnpm format:check

  • pnpm typecheck

  • pnpm test

  • targeted tests:

  • npx vitest run --project lynx — 730 passed (729 before this change).

  • Both tests red-verified against the unpatched driver, which took two
    attempts worth recording:

    • The partial destroy-run test goes red with expected [[…], […]] to deeply equal [].
    • The new oracle goes red with {setEvent: 1, remove: 1, flush: 1} vs
      {remove: 1, flush: 1} — but only after I fixed it. My first version
      passed unpatched, because its clean-paint scenario hit plan.direct, which
      already skipped the detaches. It was measuring the already-fast path and
      protecting nothing. It now parks an explicit host above the run's id space
      so the incremental compact declines, and asserts the bound on both
      routes.
  • pnpm format:files on both changed files — clean; changeset added.

  • pnpm test (repo-wide) has now finished: 26,770 passed, 1 skipped, 3
    failed
    across 2,451 test files. I said I would confirm whether the failures
    are pre-existing rather than let them pass silently, so I checked instead of
    asserting: with packages/lynx/src/core/host-driver.ts and its test file
    checked back out to the base commit (8938c1260), all three reproduce
    identically
    .

    failing test full run, this head isolated, src reverted to base
    rspeedy-plugin › native-demo.test.ts › escalates cleanup for a detached descendant after its leader exits fail fail
    drei-adapted-browser › upstream-e2e.browser.test.ts › should match previous one fail fail
    octane-events-browser › suspense-hydration.test.ts › restores focused input and its selected text after a keyed DOM reorder fail fail

    None of the three imports the host driver, and the diff touches no package
    they reach. Two are headless-Chromium behaviours in this container (a WebGL
    canvas whose ready event never fires; text-selection restore across a keyed
    DOM reorder). The third asserts a 100 ms SIGTERM→SIGKILL process-group
    escalation and fails deterministically here — in isolation, at base, under no
    load — so it is this container's process semantics, not a scheduling flake.

Risk / follow-ups

  • The wall-clock half of Clear/teardown pays 2.6–4× the peers' JS volume, regime-invariant, family-wide — attribute and shrink it (E2's cost half) #241's oracle is not measured here. clear@10k ≤
    1.15× react and heapMtsAfterClear ≤ upstream both need the web harness, and
    the app cannot be built in this environment (npx rspeedy build → 404 from
    the proxy). What is verified is the deterministic half — clear-time event
    calls = 0, one host call per row — and the call-count reduction that follows
    from it. Whether 20,000 fewer PAPI calls closes the 2.6× ratio is
    hypothesis, not verified; it needs a run on the 3-lane campaign.
  • Behaviour change worth a reviewer's eye: the batch-wide destroy set is now
    built on first use rather than at the first destroy command, so a malformed
    destroy id in a batch that also detaches listeners is reported at the detach
    rather than at the destroy. Same error, earlier point. No test pinned the
    ordering.
  • The event-operation skip is a lowering-time decision; the expansion still
    emits the detach commands, so the command volume through the generic applier
    is unchanged (~100k at 10k rows). Cutting those too would need
    plan.eventCommands renegotiated with the certified path — a larger change,
    deliberately not folded in here.
  • Branch-name deviation: the harness assigned
    claude/octane-issue-66-phase-a-7wz97z, but create-a-pr mandates a perf/
    topic branch so label-pr.yml can derive the type label. Topic branch used;
    flagging rather than silently picking a side.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

Clearing a template run cost one Element PAPI call per row plus one per
listener on that row. A `destroy-run` expands to a listener detach per event
site, a root remove per row, and a destroy per host, and the detaches were
journalled and lowered like any other event change, so the lynx-table row --
`view.row` over three text cells, two of them tappable -- spent three host
calls per row to clear. At 10k rows that is 30,001 calls, of which 20,000
describe listeners on hosts that no longer exist by the end of the commit.

Only the root removal is observable. Dispatch already refuses a token whose
record was deleted, whose generation moved on, or whose host is no longer
root-connected, and a destroy does all three, so a binding left installed on a
detached element cannot reach a listener. The certified direct teardown plan
has skipped these unbinds since it was written; this carries the same property
to every other teardown route, which is the route a table clear takes as soon
as anything else on the page owns a higher host id.

A clear now costs one host call per row whatever the row carries. Preserved:
main-thread worklet registrations are still released explicitly, because the
engine holds those and only release ends their lifetime; native-event
bookkeeping is still cleared, so no entry outlives its record; and any native
list topology keeps the explicit unbind, because a list recycles elements the
driver does not own and can hand one back still bound.

Tests: the partial destroy-run test now asserts zero teardown event calls plus
the semantic property that makes that safe -- neither destroyed token resolves
while a survivor's still does. A new oracle pins one host call per row across
1/4/16 rows and 0/1/2 listeners, on both the certified and the general
expansion route. Both were confirmed red against the unpatched driver.

Behaviour note: the batch-wide destroy set is now built on first use rather
than at the first destroy command, so a malformed destroy id in a batch that
also detaches listeners is reported at the detach instead of at the destroy.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
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.

1 participant