Skip to content

perf(lynx): plan-aware wire — fold provable plan instances into instantiate-by-plan-id commands ([lynx-perf 4]) - #12

Closed
Huxpro wants to merge 1 commit into
lynx/auto-memofrom
lynx/plan-wire
Closed

Huxpro wants to merge 1 commit into
lynx/auto-memofrom
lynx/plan-wire

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Closes #5. Stacked on #11 ([lynx-perf 3], base lynx/auto-memo), which stacks on #10 → #1.

Summary

  • The Lynx wire now folds provable plan instances into one instantiate(planId, parent, before, ids, values, events) command each, replacing that instance's per-node create/event/insert commands. Mounting a table row costs 1 wire command instead of ~16; create@10k drops from 160,000 commands to exactly 10,000 and the create commit serializes at 31% of its former size (−69%).
  • Identity is content-derived (canonical plan serialization → double FNV-1a hash), so both bundles compute the same ID for structurally identical plan constants with zero compiler changes: the main thread registers every eligible plan flowing through its universalPlan facade and announces the IDs in its ready reply (plus incremental plan-registry messages for lazily loaded chunks). The background folds only announced IDs, so registry skew, HMR drift, lazy chunks, or a hash collision all degrade to the per-node commands exactly as before — protocol version stays 1, and a main thread that announces nothing sees byte-identical traffic.
  • The main thread expands each instantiate back into the identical per-node commands before the untouched validation and Element PAPI apply paths run, so handle acknowledgement, first-screen adoption, and worklet bookkeeping see exactly what they saw before.

Why

  • Issue [lynx-perf 4] Plan-aware wire: instantiate-by-plan-id instead of per-node create commands #5: mounting ships per-node command objects whose clone+validate+staging volume is the excess; the compiler already hoists every host template as a universalPlan shared by both bundles, and the wire never used it.
  • The issue sketched instantiate(planId, instanceId, slotValues[]); the landed shape carries explicit ids (pre-order) and events (listener tokens) instead of an opaque instanceId because both threads must agree on per-host IDs for the acknowledgement handle deltas and the accepted-listener table — expansion regenerates the same IDs the background staged, so the ack path needed no changes at all.
  • Optional item 4 (flat-array command packing) was measured-first and not taken: bytes are already −69%, and the wall-clock run shows crossing volume is not the current bottleneck (below), so the define-gated packing would add complexity for no measured win.

Changes

  • packages/octane/src/universal-core.ts: staged batches carry planInstances provenance — each fully-new, visible, plan-rooted host subtree with its plan, slot values, pre-order host IDs, and staged listeners — only for async transports that declare the new planInstantiation capability. Non-opted transports see byte-identical batches. Subtrees containing lifecycles, local callbacks, portals, retained or invisible nodes are never attributed.
  • packages/lynx/src/core/plan-wire.ts (new): canonical plan serialization + content-derived wire IDs (collision → poisoned-ID eviction), the realm-local plan registry, static wire-shape eligibility (rejects propsSlot, key/ref/children bindings, main-thread:* bindings, if/switch/component nodes, event∩value slot aliases), the background fold (two-pass footprint proof: an instance folds only when its commands are exactly its creates + events + one root insert, with no foreign command touching its IDs), and the main-thread expansion that regenerates identical per-node commands.
  • packages/lynx/src/core/transport.ts: tracks announced plan IDs from main-ready/plan-registry; folds the wire message only — handle-delta validation and worklet lifetimes keep reading the per-node prepared batch, which mirrors what main regenerates on expansion.
  • packages/lynx/src/main-thread.ts: announces registered plans in every ready reply and re-announces late registrations; expands instantiate commands before the untouched prepare/apply/ack paths; an unresolvable plan ID rejects the commit like any other malformed batch.
  • packages/lynx/src/main-renderer.ts: registers every eligible compiled plan constant at the universalPlan facade — the whole main-side registry with no compiler involvement.
  • packages/lynx/src/core/protocol.ts: instantiate command validation (exact keys, positive-integer IDs, wire-leaf values, typed event entries), plans on the ready reply, and the plan-registry message.
  • packages/lynx/tests/plan-wire.test.ts (new, 11): cross-facade identity (background and main-renderer facades normalize props: {}/absent differently — canonicalization treats them identically), eligibility rejections, staged→folded→expanded round-trip equality, fallback and disqualification paths.
  • packages/octane/tests/universal-plan-instances.test.ts (new, 8 × two projects): provenance staging, retained instances not re-attributed, lifecycle-containing wrappers excluded, no opt-in → no planInstances key.
  • packages/lynx/tests/milestone-7-integration.test.ts: three new dual-realm scenes over the real compiled layers — fold happy path (announced plans → instantiates on the wire, correct DOM, live taps, update retains identity with zero instantiates, insertion folds exactly one, late registration announces), per-node fallback when nothing is announced, and registry corruption rejecting the commit.
  • packages/lynx/tests/protocol.test.ts: instantiate/plans/plan-registry validation cases.
  • benchmarks/lynx-table: main-layer.ts — the same app compiled with the main-thread renderer in a second vite build, mirroring the device's dual bundles; run.mjs bridges its registered plans into the workload realm and pins a real create model floor (one instantiate per row).
  • benchmarks/baselines/ratios.json: create_commands_{1k,10k} guards at the one-command-per-row floor.
  • Changesets: octane patch (plan-instance provenance), @octanejs/lynx patch (plan-aware wire).

Measured

Deterministic (in-process dual-thread rig, real tap tokens, drift-checked across iterations; every non-create counter byte-identical before/after):

op before after model floor
create@1k commands 16,000 1,000 1,000 (1.00×)
create@10k commands 160,000 10,000 10,000 (1.00×)
create@1k commit bytes 1,034,818 321,187 — (−69%)
create@10k commit bytes 10,573,119 3,309,119 — (−69%)
update10th / select / swap / storms unchanged unchanged (byte-identical counters)

node benchmarks/bench.mjs --only lynx-table --ratios → PASS, 25 guards checked. Red-check: the pre-change head measures 16.0× the new create floor, so reverting the fold breaches both new guards; the three new integration scenes and the round-trip test also fail against a reverted implementation, and the octane provenance tests fail with the staging gate flipped.

Acceptance vs issue #5:

  • create@10k ~160k → ~10k+toolbar instantiates: met exactly (10,000).
  • commit bytes −50% or better: met (−69%).
  • create@10k wall-clock 2400–3000ms band: not reached — see below.
  • FCP: the first-screen path (MT-rendered) is untouched; not separately instrumented.
  • Fallback tested: registry-skew scene (nothing announced → per-node wire, working DOM/taps) and corruption scene (unknown plan ID → commit rejected, diagnostic surfaced).

Lynx for Web wall clock (same 4-core host, all four cells re-run back-to-back, identical instrument, medians of n=3), before → after:

op @10k octane before octane after vue-vdom (same run)
create 4459 ±30 (2.17×) 4669 ±277 (2.08×) 2249 ±116
update10th 473 (2.60×) 534 ±25 (2.41×) 222 ±15
select 274 (1.95×) 298 ±22 (1.74×) 171 ±12
updateStorm 1460 (0.51×) 1429 ±179 (0.44×) 3251 ±32
selectStorm 487 (0.35×) 481 ±57 (0.31×) 1552 ±81
  • Folding is provably active in these bundles: a runtime probe of the profile web build counts 1,094 outbound wire commands for mount + create-1000 (per-node would be ~16,600) at 326KB — the rig's numbers, reproduced in the real Rspeedy dual-bundle pipeline under Chromium.
  • Wall-clock create is nonetheless unchanged within noise (every cell drifted a similar few percent between sessions; the vs-vdom ratio nudged 2.17× → 2.08×). The issue's own caveat proved out: vue-lynx's +b:c experiment showed create is PAPI-bound, and the 16× reduction in per-command clone+validate volume was not the wall-clock bottleneck on this host. What instantiate does not remove: background staging still materializes per-node commands before folding, and main-thread expansion regenerates them before the PAPI apply loop. Reaching the 2400–3000 band needs the react-tier work the issue explicitly scoped out (code-staged MT create — materializing the plan directly to PAPI without intermediate command objects), now the honest candidate follow-up.

Validation

  • pnpm format:check (scoped format:files:check on all changed files)
  • pnpm typecheck
  • pnpm test — full root run on this head: 16910 passed / 106 skipped. 21 failed files = the 19 pre-existing environmental files (identical set to the PR perf(octane): transported universal roots adopt retained subtrees — render breadth proportional to change size ([lynx-perf 3]) #11 baseline: Playwright-browser suites without a Chromium download, macOS-only rspeedy native-demo, website ssr-smoke /benchmarks card count) + 2 lynx contract assertions this diff legitimately moved — the runtime source-graph inventory gaining plan-wire.ts and the first-screen ready reply now announcing plans — both updated in this PR and green.
  • targeted on the final diff: packages/lynx/tests (26 files / 295 tests, two-realm rig with validation on, incl. plan-wire 11 and the three new milestone-7 scenes), universal-plan-instances + universal-transport (both octane and octane-prod projects)
  • node benchmarks/bench.mjs --only lynx-table --ratios → PASS (25 guards)
  • new tests red-checked against reverted implementation; new guards red-checked against the pre-change head (16.0×)

Risk / follow-ups

  • Fold eligibility is conservative by construction (footprint proof per instance); anything unprovable ships per-node commands, so the failure mode of a missed fold is wire volume, never correctness. The corruption path (announced but unresolvable plan) rejects the commit through the existing malformed-batch path.
  • The paced-storm rig counter (framesPumped) is wall-clock sensitive through the transport's 64ms pace safety valve: one contended run showed a one-frame drift (5→4) while unrelated heavy tasks shared the host; quiet-machine runs are stable across 4 iterations on both heads. Pre-existing sensitivity, unrelated to this diff.
  • Wall-clock create remains ~2.1× vue-vdom: the remaining cost is per-node staging on the background plus expansion + per-node PAPI apply on main. Code-staged MT create (plan → PAPI directly, skipping command materialization on both sides) is the follow-up that this PR's registry/announcement/expansion machinery was designed to enable.
  • Flat-array command packing (issue item 4) deliberately not taken; measure-first said no.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code

https://claude.ai/code/session_013br9KPN1riTSP683xEvUuU


Generated by Claude Code

…wire commands

The compiler already hoists every host template as a universalPlan shared
by both bundles; the wire never used it. The background now re-encodes each
provable plan instance — its creates, listeners, and root insert — as one
instantiate(planId, parent, before, ids, values, events) command, and the
main thread expands it back into identical per-node commands before the
untouched validation and Element PAPI apply paths run.

Identity is content-derived (canonical serialization, double FNV-1a), so
structurally identical plan constants hash alike across bundles with zero
compiler changes. The main thread registers eligible plans at its
universalPlan facade and announces the IDs in the ready reply plus
incremental plan-registry messages; the background folds only announced
IDs, so registry skew degrades to per-node commands and protocol stays
version 1. An announced-but-unresolvable plan rejects the commit.

create@10k drops 160,000 -> 10,000 wire commands (1.00x the new
one-instantiate-per-row model floor) and the create commit serializes at
31% of its former size; every other deterministic counter is
byte-identical. Wall-clock create is unchanged within noise on the
4-core web host — crossing volume was not the bottleneck there — with a
runtime probe confirming folding is active in the real Rspeedy bundles.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013br9KPN1riTSP683xEvUuU
This was referenced Aug 9, 2026
@Huxpro

Huxpro commented Aug 11, 2026

Copy link
Copy Markdown
Owner Author

Conclusion: close as superseded. The structural compact-template and incremental-run work in octanejs/octane PRs 693 and 700 replaces this older plan-wiring approach.

@Huxpro Huxpro closed this Aug 11, 2026
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