Skip to content

perf(lynx): compile deep validation out of production builds - #39

Closed
Huxpro wants to merge 1 commit into
perf/lynx-upstream-plan-wirefrom
perf/lynx-upstream-validation
Closed

Huxpro wants to merge 1 commit into
perf/lynx-upstream-plan-wirefrom
perf/lynx-upstream-validation

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 11, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Compile deep per-command, per-handle, per-snapshot, and final whole-tree validation walks out of production Lynx builds while retaining them unchanged in development and test builds.
  • Keep protocol envelopes, ownership cloning, record/attachment transitions, duplicate-id checks, and apply-time fault handling unconditional, so malformed production commits still reject instead of applying silently.
  • Remove the repeated final-tree scan from production prepare. On the exact-parent profile, a five-commit update storm over a retained 1k table spends 27.5 → 9.4ms in prepare (-65.8%, 2.93x), and the two-commit select storm spends 7.7 → 1.8ms (-76.6%, 4.28x), with identical commits, changed rows, commands, and final state.

Why

The accepted command batch already enforces the invariants needed to construct a transaction. Production then paid again for diagnostic structural walks and a final scan over every retained host, even when a commit touched only two rows. Those checks are valuable when developing a broken renderer, but their cost scales with retained state instead of changed state.

This layer keeps the load-bearing checks that own behavior and failure containment, while limiting deep diagnostics to development. It is the independently mergeable P4 replacement for #13, frozen at dcb334afa415fda63e94c09a77a31d107dea6128 over source parent b4b0ab92df2f4b4ec0cf3b1a203158f8aa6943ec. The historical PR remains untouched.

The old #13 body reported a 3.8x aggregate prepare win from a now-retired in-process whole-run profile. That headline is not carried forward: the current durable fresh-page runner separates create and storms and does not reproduce a large create@10k wall-clock gain. The defensible product claim is the prepare-stage reduction for small commits over retained state, not a 3.8x create speedup.

Changes

  • packages/lynx/src/core/protocol.ts: retain exact envelope/identity checks in all builds; gate deep command, handle-delta, and first-tree node walks behind the existing LYNX_DEVELOPMENT constant.
  • packages/lynx/src/core/host-driver.ts: gate diagnostic structural assertions and the final whole-tree consistency pass in production; keep staging/ownership/transition checks unconditional; collect native-list updates from accepted records; avoid allocating empty destroyed/final-id sets on the production steady path.
  • packages/lynx/src/core/profiling.ts: expose prepareCheckMs, the development-only whole-tree check share of prepare.
  • packages/lynx/tests/validation-diet.test.ts: run the real dual-thread graph with fresh NODE_ENV=production modules and prove render/update/replace/unmount, native-list membership, corrupt-command rejection, duplicate-id rejection, and malformed-envelope rejection.
  • benchmarks/lynx-table/attribution/run.mjs: collect main-realm wire stages in CPU/cold/storm captures and fail profiled CPU capture if that attribution is absent.
  • Add a patch changeset for @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

  • Removed the stale issue-[lynx-perf 5] Validation diet: full structural checks in dev, envelope-only in production #6 linkage and the stale 3.8x claim instead of treating historical measurements as evidence for this rebased layer.
  • Corrected the complexity statement: production no longer scans the final retained tree, but native-list preservation still scans accepted records when lists are live; this is not universally O(batch).
  • Kept cloneProps, duplicate/record/attachment checks, protocol envelopes, and apply-time rejection unconditional rather than turning the production path into unchecked input handling.
  • Avoided unconditional empty Set allocations for destroyed/final ids in production steady state.
  • Reused the existing development constant and failure pipeline; no runtime flag, alternate protocol, recovery branch, or new transaction representation was added.
  • Added main-realm attribution to the generic runner rather than a P4-specific benchmark path.

Performance audit

Exact comparison:

  • parent: 4a444b1b9665d8b4020d774a220bd0e21933cdb2
  • candidate: fd2ea96d5fe7fc4d39d0d24fe5e9aee8f196b111
  • 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: P3 6d989b92a0f107f5cd7d71ba443f23a912397997f691cb9fa4825b0e51fb585c, P4 a6cd897fc81277782bbf06c27f4d82a47b4b7fdfaa74a665dff5ddeb475ac916
  • profile bundle SHA-256: P3 d7dae63ae1522b79898f6de98787ea75467a40a6df292c3c5d3a80e3ac0f6c74, P4 27e52fe5d1a1424c50e8653b3ddd6b490d01f4744ea323eb319017a4c96f3875
  • control bundle bytes/gzip: 403,110 / 108,604 → 403,331 / 108,686 (+0.055% / +0.075%)
  • ignored raw inputs: p4-final-candidate-wire.json, p4-final-control-{cpu,heap,cold,cold-reverse}.json, p4-final-profile-{cpu,cpu-reverse,cold,storm}.json under benchmarks/lynx-table/attribution/results/

Reportable attribution captures use five fresh pages/workers per target. CPU wraps create@10k; cold operations use five fresh realms with one cold plus four steady samples per realm; storms use the runner's fixed retained 1k setup. Every raw capture passed quiet-host preflight and records sample order, load, bundle hash, semantic state, and worker lifecycle.

Deterministic behavior and production fail-stop

Three final-candidate iterations are identical to P3 at 1k/10k: create 1 command/row; update10th 100/1,000 commands and Row renders; select 2 commands, 237 bytes, and 2 Row renders; swap 997 commands and 0 Row renders; update storm 50 commits and 5,000/50,000 commands; select storm 30 commits and 60 commands. Paced 1k remains 50 commits / 300 commands with three framed commits over three frames. The final ratio run passes all 25 applicable guards.

The production-module integration suite also proves the diet does not convert corruption into undefined behavior: null create props, duplicate ids, and malformed envelopes each produce one rejection and leave the DOM unchanged; ordinary lifecycle and native-list membership still complete.

The owner signal: small commits over retained state

Profile medians and full observed wall/prepare ranges:

retained 1k operation P3 wall ms P4 wall ms P3 prepare ms P4 prepare ms exact work
update storm 144.7 [142.5–149.7] 147.8 [141.9–153.4] (+2.1%) 27.5 [26.4–28.0] 9.4 [9.1–9.7] (-65.8%, 2.93x) 5 presentations, 500 changed rows, 5 wire commits, 500 commands
select storm 45.2 [38.3–47.5] 43.0 [41.6–44.7] (-4.9%) 7.7 [7.4–8.4] 1.8 [1.7–1.9] (-76.6%, 4.28x) 2 presentations, 3 changed rows, 2 wire commits, 3 commands

Validate also falls 0.6 → 0.2ms for update and 0.1 → 0.0ms for select. Apply and acknowledgement remain within 0.6ms and 0.1ms respectively. The removed whole-tree scan is therefore visible at its named owner without skipped commits, rows, commands, or presentation states. End-to-end update is a wash because RAF/presentation dominates the saved 18ms; select improves modestly.

Create@10k CPU and order controls

capture P3 wall / BTS / MTS ms P4 wall / BTS / MTS ms verdict
control, P3→P4 2,945.7 / 792.6 / 2,243.8 2,890.0 / 751.6 / 2,276.5 wall -1.9%, BTS -5.2%, MTS +1.5%; no observer, no adverse >5%
profile, P3→P4 2,826.0 / 776.9 / 2,150.0 2,978.0 / 834.2 / 2,295.9 wall +5.4% triggered reverse-order repeat
profile, P4→P3 2,925.4 / 784.5 / 2,211.9 2,932.1 / 824.4 / 2,251.3 candidate wall +0.2%, MTS +1.8%; the forward wall regression does not reproduce

In the forward profile, main validate is 30.9 → 0.0ms and prepare is 495.1 → 483.8ms. In reverse order they are 31.5 → 0.1ms and 583.9 → 476.0ms. A single create already touches/materializes the full tree, so removing a final diagnostic scan is a modest share of its total work; it is not the old 3.8x headline.

Profiled apply measures about +10% in both orders although P4 does not change apply behavior. Observer-free total MTS is +1.5%, reverse-profile total MTS is +1.8%, and whole wall is -1.9%/+0.2%; this is disclosed as instrumentation/bundle-layout sensitivity rather than claimed as either a gain or a product regression.

Cold paths, heap, and bundle

The profiled cold first medians are non-adverse across all six cells: swap -0.1%, replace -8.9%, create -4.0%, clear -10.3%, select -8.9%, update10th -5.8%. Control-forward select was +11.0%, so the entire matrix was repeated P4→P3; select became -1.3%. Reverse update10th then landed in the next roughly 13ms RAF bucket (+37.4%) while profile and control-forward measured -5.8% and -1.7%. The direction flips and the exact semantic/wire counts remain constant, so these isolated first samples are classified as frame-phase quantization, not a stable regression.

Explicit-GC retained slopes are 13,842.49 → 13,842.58 bytes/row (+0.0007%). P4 retained medians are slightly lower at 1k, 10k, and 30k (14,285,496; 142,708,824; 416,582,308 bytes), and every clear/view-destroy/page-close worker-release check passes. The control bundle adds 221 raw bytes / 82 gzip bytes, well below the 5% budget.

FCP is not reused from the historical PR because it predates this final candidate and harness. This layer does not make a startup claim; the cumulative P6/P7 checkpoints will remeasure FCP on exact SHAs.

Validation

  • scoped format check: all six changed files use repository Prettier style
  • @octanejs/lynx source/testing/typetest typechecks
  • all Lynx tests: 27 files / 301 tests (the sandboxed run first reported 11 spawnSync node EPERMs; the same command outside that process restriction passes 301/301)
  • attribution harness Node tests: 3/3
  • deterministic benchmark ratio gate: 25/25 applicable guards
  • reportable control/profile CPU, reverse-profile CPU, cold, reverse-control cold, storm, and explicit-GC heap captures on exact SHAs
  • pnpm sync; git diff --check; final worktree clean

Risk / follow-ups

  • Production diagnostics become coarser: corrupt application-generated commands can fail at a staging/apply guard instead of a labeled deep protocol assertion. The invariant is loud rejection before DOM mutation, covered by the production suite.
  • Native-list preservation still scans accepted records when lists are live. The general final-tree verification pass is gone, but list work is intentionally not described as O(batch).
  • The profiled apply cell is about 10% higher in both target orders while observer-free/reverse total MTS remains below 2%; retain this as a measurement sensitivity in cumulative checkpoints.
  • FCP is deliberately unclaimed here and must be measured at the later startup-owning P6/P7 layers.

Provenance

Envelope validation and load-bearing checks (duplicate ids, record
existence, attach-state transitions, cloneProps ownership) stay
unconditional; the per-command, per-handle-delta, and per-snapshot-node
structural walks plus the whole-tree prepare check pass become
development-only. Production keeps fail-stop: a corrupt commit rejects
loudly through the staging guards and the apply-time fault path, pinned
by a new production-module dual-thread suite. The profile hook gains
prepareCheckMs, the dev-only check share of prepareMs.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013br9KPN1riTSP683xEvUuU
@Huxpro

Huxpro commented Aug 11, 2026

Copy link
Copy Markdown
Owner Author

Conclusion: close as not currently justified. The historical validation-diet optimization no longer has a material live-main owner and should be reconsidered only with a new profile.

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