Skip to content

perf(lynx): fold plan instances on the wire - #38

Closed
Huxpro wants to merge 1 commit into
perf/lynx-upstream-adoptionfrom
perf/lynx-upstream-plan-wire
Closed

Huxpro wants to merge 1 commit into
perf/lynx-upstream-adoptionfrom
perf/lynx-upstream-plan-wire

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 10, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Fold each provably complete, announced universal plan instance into one Lynx wire command, then expand it back to the existing validated per-node command stream on the main thread.
  • Reduce deterministic create traffic from 16 commands per table row to exactly one instantiate per row: create@10k is 160,000 → 10,000 commands and 10,572,877 → 3,309,117 serialized bytes (-68.7%).
  • Preserve the current protocol and correctness surface: an unannounced, ineligible, ambiguous, colliding, or externally touched instance falls back to the unchanged per-node commands.

Why

The compiler already hoists stable host structure into universal plans shared by the background and main bundles, but the transport discarded that structure and cloned every host create/event/insert command independently. This layer adds a conservative plan registry and wire representation without changing the compiler, handle acknowledgement, listener ownership, PAPI validation, or apply paths.

This is the independently mergeable P3 replacement for #12, frozen at b4b0ab92df2f4b4ec0cf3b1a203158f8aa6943ec over source parent adfad409867733a524c5ab200c15b0510535586d. The historical PR remains untouched.

Changes

  • packages/octane/src/universal-core.ts: expose plan-instance provenance only to async transports that opt into plan instantiation; release the provenance and prepared payload after transport settlement.
  • packages/lynx/src/core/plan-wire.ts: canonicalize plans, derive and collision-check wire identities, prove fold footprints, fold eligible instances, and expand instantiate commands without per-commit recursive helper allocation.
  • packages/lynx/src/core/{protocol,transport}.ts: validate instantiate/registry messages, consume only identities announced by main, poison local collisions, and keep the original prepared batch for acknowledgement/worklet bookkeeping.
  • packages/lynx/src/{main-renderer,main-thread}.ts: register eligible plans, announce initial and late identities, and expand before the existing validation/apply pipeline.
  • Add unit, protocol, dual-realm integration, fallback/corruption, lifecycle-release, and universal provenance coverage.
  • Extend benchmarks/lynx-table with the real main-renderer bundle/registry bridge, strict one-command-per-row create guards, stage instrumentation, and a race-free click-to-presentation arm handshake.
  • Add patch changesets for octane and @octanejs/lynx.

Stack

The fork review stack is GitHub Stack octanejs#36 and is managed with the official gh stack extension. Upstream delivery remains bottom-to-top.

Simplification

  • Kept protocol version 1. Main peers that announce no plans receive byte-identical per-node traffic; no compatibility branch or compiler coordination is required.
  • Reused the existing validator/apply/ack paths by expanding immediately before them instead of teaching every downstream consumer a second command model.
  • Removed an intermediate blueprint wrapper, per-event/per-instance deep freezes, retained wire payloads, per-instantiate recursive closures, and post-settlement provenance retention found during the audit.
  • Restricted eligibility instead of adding recovery complexity: accessors, cycles, non-plain objects, symbols, main-thread/reserved props, event/value aliasing, lifecycle-bearing subtrees, and foreign command footprints stay per-node.
  • Deliberately did not add flat-array command packing. The structural fold already removes 68.7% of create bytes; another encoding needs independent evidence.

Performance audit

Exact comparison:

  • parent: 71e1b8a88a8c3e37cc005d37a8e6f75cf31088b4
  • candidate: 4a444b1b9665d8b4020d774a220bd0e21933cdb2
  • host: 32x Intel Xeon Platinum 8336C, Linux 5.15.120.bsk.3-amd64, Node 22.22.2, Chromium 149.0.7827.55
  • control bundle SHA-256: P2 65e026f73dd62bb6484c2806979eec1a2d4f0a1c9938ef6108893bd6f765865a, P3 6d989b92a0f107f5cd7d71ba443f23a912397997f691cb9fa4825b0e51fb585c
  • profile bundle SHA-256: P2 07c72dbaeae4bfcade074fbebeb33d2570a858eb91bed0f009fe0e2f0f5d579a, P3 d7dae63ae1522b79898f6de98787ea75467a40a6df292c3c5d3a80e3ac0f6c74
  • ignored local raw inputs: p3-final-candidate-wire.json, p3-final-control-{cpu,heap,cold}.json, p3-final-control-cold-reverse.json, and p3-final-profile-{cpu,heap,cold,storm}.json under benchmarks/lynx-table/attribution/results/

Deterministic wire model

Three drift-checked candidate iterations at 1k/10k; the parent capture uses the same final harness. Every semantic oracle passed.

operation P2 P3 delta / invariant
create commands @1k 16,000 1,000 -93.75%, exact 1/row floor
create commands @10k 160,000 10,000 -93.75%, exact 1/row floor
create bytes @1k 1,034,724 321,043 -69.0%
create bytes @10k 10,572,877 3,309,117 -68.7%
update10th @1k / @10k 100 / 1,000 100 / 1,000 unchanged
select commands / bytes / Row renders 2 / 237 / 2 2 / 237 / 2 unchanged at both scales
swap commands / Row renders 997 / 0 997 / 0 unchanged at both scales
update storm @10k 50 commits / 50,000 commands same unchanged
select storm @10k 30 commits / 60 commands same unchanged

The final node benchmarks/bench.mjs --ratios lynx-table run passes all 25 applicable guards. Reverting the fold restores 16 commands/row and breaches both strict create floors.

Create@10k CPU attribution

Five fresh pages/workers per target. Control captures observer-free totals; profile captures named allocation stages.

bundle target wall median ms BTS active ms MTS active ms named BTS share
control P2 3,047.5 752.0 2,389.7 -
control P3 2,863.9 (-6.0%) 771.1 (+2.5%) 2,202.4 (-7.8%) -
profile P2 2,980.6 788.0 2,329.7 67.7%
profile P3 2,935.0 (-1.5%) 784.9 (-0.4%) 2,234.7 (-4.1%) 86.7%

The product claim is the deterministic 16x command and 68.7% byte reduction, not a wall-clock speedup: control/profile directions agree that create is not slower, but per-node background staging and main-thread expansion/PAPI apply still exist. In the candidate profile, owner materialization (231.7ms), transaction staging (397.4ms), and folding (51.0ms) explain 86.7% of BTS active time.

Heap and lifecycle

Five repetitions per scale with explicit CDP GC and fresh pages/workers:

target retained slope bytes/row 1k median MiB 10k median MiB 30k median MiB worker released
P2 13,501 13.3 132.8 387.5 yes
P3 13,842 (+2.5%) 13.7 136.1 397.3 yes

The increase is linear and below the 5% decision threshold at every scale. Clear residuals, synchronous view destruction, and worker release all complete in every repetition. Heap snapshots used during simplification exposed retained provenance/payload and per-instance closure costs; after releasing/refactoring those, the remaining registry/instance metadata is classified as bounded resident overhead rather than a lifecycle leak. V8 context names move with bundled source offsets, so snapshot class-name deltas alone were not treated as proof.

Cold/steady and storm controls

In the first control run, create is -1.0%; select +0.7%; update10th +2.6%; swap +2.3%. First replace (+7.5%) and clear (+8.6%) crossed the 5% repeat threshold, so the entire cold matrix was repeated in reverse target order. Replace then measured +0.9%; create moved from -1.0% to +9.3%; clear remained +7.5%. Their steady deltas were -4.0%, -2.3%, and -0.6% respectively in the reverse run. This order-sensitive first-sample spread is classified as realm/JIT/RAF startup noise; steady samples and exact wire/presentation counts do not reproduce a product regression. Clear's repeatable ~6ms first-sample cost is disclosed as a cold-start sensitivity, not hidden.

The click boundary was corrected during the audit: DOM target lookup now finishes before arming, and a synchronous arm-token/result handshake prevents click-before-listener races. Select still quantizes into roughly 20ms and 33ms RAF buckets while every exact profile sample remains one commit, two changed rows, and 237 bytes. It is therefore reported as phase-sensitive rather than a stable gain/regression.

Profile storm medians at 10k are update 154.5 → 158.7ms (+2.7%) and select 43.1 → 41.2ms (-4.4%), with identical presentation and wire counts and all semantic checks passing.

Validation

  • scoped format check: all 20 changed files use repository Prettier style
  • package/core typecheck: @octanejs/lynx source/testing/typetests, Octane core, and benchmark app tsrx-tsc --noEmit
  • pnpm test - root run completed with 1,662 files / 18,033 tests passing; 9 unrelated projects / 26 tests failed under full-workspace contention, plus missing Playwright WebKit and a website server startup. No P3-owned project failed.
  • targeted tests: Lynx + plan-instance matrix 28 files / 306 tests; complete universal runtime/compiler neighborhood in dev+prod 33 files / 584 tests
  • deterministic benchmark ratio gate: 25/25 applicable guards
  • explicit-GC heap, CPU, cold, reverse-order cold, and storm attribution on exact SHAs
  • pnpm sync; git diff --check; final worktree clean

Risk / follow-ups

  • Folding is conservative and all misses fall back to the old commands, but a peer that announces an identity and later cannot resolve it rejects the malformed commit. Collision and corruption cases are covered.
  • The plan registry and instance metadata add about 2.5% retained bytes/row in this synthetic large table. It is bounded and released with the realm, but should remain visible in later cumulative checkpoints.
  • This layer reduces transport representation, not total materialization. A later direct plan-to-PAPI path would be required to remove background staging and main expansion; that belongs in its own measured PR.
  • The first-sample clear cell is consistently ~6ms slower while steady clear is neutral. Later cumulative benchmarks should keep the cold/steady split rather than collapsing it into one headline number.
  • FCP was not separately measured; the existing main-thread first-screen path remains covered by its integration tests.

Provenance

@github-actions github-actions Bot added the feat label Aug 10, 2026
@Huxpro Huxpro changed the title feat(lynx): fold plan instances on the wire perf(lynx): fold plan instances on the wire Aug 10, 2026
@Huxpro

Huxpro commented Aug 11, 2026

Copy link
Copy Markdown
Owner Author

Conclusion: close as superseded. Upstream PRs 693 and 700 replace the plan-wire mechanism with compact templates and incremental template runs.

@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