Skip to content

perf(lynx): cut keyed component materialization cost - #42

Closed
Huxpro wants to merge 1 commit into
perf/lynx-upstream-worklet-bridgefrom
perf/lynx-upstream-materialization
Closed

Huxpro wants to merge 1 commit into
perf/lynx-upstream-worklet-bridgefrom
perf/lynx-upstream-materialization

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Cut keyed-component create@10k background-thread CPU from 812.5 ms to 415.2 ms while keeping main-thread CPU flat (2,037.7 → 2,036.1 ms).
  • Remove per-row component-owner materialization and compact compiler-proven plan instances before per-host command staging: owner drafts fall from 20,001 to 1 and staged commands from 160,000 to 10,000.
  • Reduce retained background heap slope from 14,066 to 6,966 bytes/row across 1k/10k/30k, with exact storm semantics and worker release preserved.
  • Keep every specialization capability-gated and preserve ownerful, dynamic, unsupported-plan, collision, corruption, adoption, first-screen, and reverse-renderer fallbacks.

Why

P6 still spends most create@10k BTS work materializing a component owner for every keyed row, generating per-host commands that the Lynx plan wire immediately folds back into one instantiate command, and reconstructing those commands again for acknowledgement handle publication. The compiler already knows when a keyed list item is exactly one component, and the Lynx transport already knows whether its plan is announced and wire-safe. This layer carries that proof through the runtime so the intermediate representation is never built.

This is the independently mergeable P7 replacement for the product behavior in #31 and the concise heap/teardown/storm evidence from #32/#33. Those source PRs were refetched immediately before publication at frozen heads f501941d22b6f550cf8709b8235f72339b860db4, ae7e5d72e0765fc12613f235d36ebab96ddec929, and e9d0fab6d0dde33a70c77be05f4d2e5372431fbf. Their historical bodies and state remain untouched.

Changes

  • Add the explicit compiler-component-items renderer capability and opt only the Lynx background/main compiler configurations into it; other universal renderers keep their existing component-owner shape.
  • Teach the compiler to mark a keyed single-component list body and pass direct component props without synthesizing a per-item prop program.
  • Add a lazy component owner that elides its logical range only when component evaluation proves no owner-scoped hook/context/error/Suspense/nested-component behavior was used.
  • Preserve the existing trusted three-host row specialization, then use the generic compiler proof for other eligible single-component items.
  • Carry compact plan provenance directly from staging, emit one instantiate command per row, and retain exact plan snapshots only until acknowledgement.
  • Derive background handle identity/topology from instantiate plans without rebuilding 150,000 covered per-host commands or a retained topology mirror.
  • Mirror the compact shape in first-screen rendering so adoption assigns identical host IDs; ownerful/dynamic paths retain their range.
  • Add compiler/runtime, renderer, plan-wire, acknowledgement, first-screen, fallback, and capability-regression coverage plus a patch changeset.

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

  • Re-proved the opportunity on exact P6 instead of transplanting perf(lynx): cut keyed component materialization cost #31's assumptions: P6 still records 20,001 owner drafts / plan values / materializations, 90,000 logical records, and 160,000 staged commands at create@10k.
  • Kept P5's scalar parent topology and P6's plan registry/collision ownership; this layer adds no second topology map, child sets, protocol version, or fallback registry.
  • Replaced the first implementation's per-row eligibility result arrays with a boolean proof and constructs wire values only during the single fold pass.
  • Visits only host IDs/types/placements during acknowledgement. Props, listeners, and per-node command objects are not regenerated there.
  • Removed two unnecessary deep freezes on compact ids/events arrays. A diagnostic run proved P6/P7 had identical clone graph counts, while frozen array element kinds raised dispatch from about 23 to 29 ms; leaving the transaction-owned readonly arrays packed restores final dispatch to 23.3 ms without weakening the outer frozen batch/command boundary.
  • Kept exact plans alive until ACK rather than resolving against a mutable global registry after dispatch; the snapshot map is one entry per distinct plan, not per host.
  • Did not generalize the optimization to authored keys, spreads/children, nested components, renderer regions, portals, lifecycles, local callbacks, invisible hosts, unannounced plans, or non-wire values. Those shapes use the existing path.

Performance audit

Exact comparison:

  • parent: 68b2b0546b28d54b7cd8d44665f95d36b58e48b3
  • candidate: ff7d2c71c2296b7936f17c809e46637a18963338
  • host: 32× Intel Xeon Platinum 8336C, Linux 5.15.120.bsk.3-amd64, Node 22.22.2, Chromium 149.0.7827.55
  • protocol: fresh page/worker per sample, n=5, exact CDP GC for heap, original sample order retained, identical app/driver/oracles, quiet-host preflight passed, zero DNF/null medians
  • final bundle hashes: control P6 1a7e231e…77b842, P7 99b7877f…bd771; profile P6 53bfa7ca…c9cd, P7 f68fbd8c…2d0f
  • ignored raw outputs: benchmarks/lynx-table/attribution/results/p7-{control,profile}-final-sha.json, p7-{summary,report}-final-sha.{json,md}; startup files /tmp/p7-p6-stages-{10k,30k}.{json,md} and /tmp/p7-v2-p7-stages-{10k,30k}.{json,md}; decoded bundles /tmp/p7-v2-{p6,p7}-{control,profile}-bundle.json

Commands:

node benchmarks/bench.mjs --only lynx-table --ratios
pnpm bench:lynx-attribution:build --targets p6=68b2b0546b28d54b7cd8d44665f95d36b58e48b3,p7=ff7d2c71c2296b7936f17c809e46637a18963338 --instrument --profile
pnpm bench:lynx-attribution:run --targets p6,p7 --modes heap,cpu,cold,storm --scales 1000,10000,30000 --reps 5 --cold-samples 5 --output p7-profile-final-sha.json
pnpm bench:lynx-attribution:build --targets p6=68b2b0546b28d54b7cd8d44665f95d36b58e48b3,p7=ff7d2c71c2296b7936f17c809e46637a18963338
pnpm bench:lynx-attribution:run --targets p6,p7 --modes heap,cpu,cold --scales 1000,10000,30000 --reps 5 --cold-samples 5 --output p7-control-final-sha.json
pnpm bench:lynx-attribution:analyze --baseline p6 --candidate p7 benchmarks/lynx-table/attribution/results/p7-control-final-sha.json benchmarks/lynx-table/attribution/results/p7-profile-final-sha.json
node benchmarks/lynx-table/stages/run.mjs --rows 10000 --reps 5
node benchmarks/lynx-table/stages/run.mjs --rows 30000 --reps 5

Create@10k CPU and wall clock

build metric P6 median (min–max) P7 median (min–max) change
profile wall 2,682.8 (2,666.3–2,737.8) ms 2,342.1 (2,315.5–2,441.0) ms -12.7%
profile BTS active 812.5 (782.4–860.6) ms 415.2 (409.7–428.4) ms -48.9%, 1.96×
profile MTS active 2,037.7 (2,017.4–2,109.7) ms 2,036.1 (2,015.2–2,143.6) ms -0.1%
control wall 2,718.4 (2,661.3–2,804.0) ms 2,379.2 (2,356.0–2,460.7) ms -12.5%
control BTS active 783.3 (749.3–826.0) ms 402.2 (399.1–421.3) ms -48.6%, 1.95×
control MTS active 2,078.2 (2,047.0–2,172.3) ms 2,079.0 (2,054.5–2,141.4) ms +0.04%

The P7 acceptance gates are met: profiled BTS is below 500 ms and more than 1.5× faster; observer-free end-to-end create improves more than 10%; MTS is flat. The named analyzer covers 78.3% of P6 BTS but only 65.2% of P7, below its 80% diagnostic gate. The unassigned remainder is dominated by GC, postMessage, and minified runtime frames after the named owners shrink; direct allocation counters and the observer-free control reproduce the owner movement, so no 80% coverage claim is made.

Owner and protocol signal

create@10k median counter/stage P6 P7 change
owner drafts 20,001 1 -99.995%
plan values / materializations 20,001 / 20,001 10,001 / 10,001 -50.0% / -50.0%
logical records 90,000 70,000 -22.2%
staged commands / frozen command objects 160,000 / 160,002 10,000 / 10,002 -93.75% / -93.75%
owner materialization 222.7 ms 122.6 ms -45.0%
transaction staging 364.9 ms 133.5 ms -63.4%
plan folding 48.7 ms 14.5 ms -70.2%
clone visits / blueprint hosts 40,028 / 40,028 40,028 / 40,028 invariant
item callbacks / plan instances 10,000 / 10,000 10,000 / 10,000 invariant

Final profile wire work is invariant: one paced commit, 10,000 instantiate commands, two main-to-background responses, and 156 response bytes. Commit bytes move 3,297,894 → 3,294,798 (-0.1%); dispatch is 23.2 → 23.3 ms (+0.4%); main prepare is 474.4 → 459.6 ms and apply is 1,393.6 → 1,394.5 ms. The optimization neither hides commands nor shifts a material cost into structured clone/main apply.

Retained heap and teardown

scale P6 retained median P7 retained median change P7 clear / destroy / worker release median
1k 14,532,184 B 7,387,732 B -49.2% 68.1 / 1.2 / 6.3 ms
10k 144,982,588 B 72,977,696 B -49.7% 737.5 / 1.3 / 5.4 ms
30k 423,308,052 B 210,048,904 B -50.4% 2,459.4 / 1.6 / 5.7 ms
fitted slope 14,066 B/row 6,966 B/row -50.5% —

Clear residual also falls at every scale (4.32 → 2.65 MiB at 1k, 38.90 → 22.68 MiB at 10k, 113.74 → 64.97 MiB at 30k). Every sample passes the semantic oracle and releases its background worker after page close; no super-linear candidate slope is present.

Cold paths and deterministic storms

operation @1k P6 first P7 first change P6 steady P7 steady change
swap 83.5 76.0 -9.0% 67.3 66.9 -0.6%
replace 385.8 349.8 -9.3% 326.1 284.8 -12.7%
create 334.5 275.0 -17.8% 254.9 204.2 -19.9%
clear 71.6 70.1 -2.1% 59.6 56.8 -4.7%
select 34.1 28.5 -16.4% 21.0 19.2 -8.6%
update10th 33.6 28.5 -15.2% 25.7 23.1 -10.1%

Update storm is 143.4 (141.9–159.1) → 129.2 (126.9–137.0) ms (-9.9%) with exactly 5 presentation commits / 500 changed rows / 5 wire commits / 500 commands in every sample. Select storm is 46.0 (40.6–47.8) → 39.0 (34.2–39.9) ms (-15.2%) with exactly 2 / 3 / 2 / 3. The deterministic non-browser run independently passes all 25 ratio guards at 1k and 10k.

Startup and bundle budget

Startup uses separate reportable AB/BA n=5 windows per exact source tree. The final test-only amend rebuilds both profile/control artifacts to the same SHA-256 as the measured product tree.

raw stage boundary P6 median (min–max) P7 median (min–max) change
FCP@10k profile 2,415.5 (2,269.2–2,424.9) ms 2,332.8 (2,258.4–2,518.4) ms -3.4%
FCP@10k control 2,317.2 (2,197.5–2,477.0) ms 2,321.1 (2,256.9–2,400.6) ms +0.2%
create@10k profile 2,626.8 (2,532.3–2,746.8) ms 2,250.2 (2,239.1–2,344.5) ms -14.3%
create@10k control 2,537.2 (2,477.1–2,690.4) ms 2,211.6 (2,195.5–2,274.8) ms -12.8%
FCP@30k profile 6,862.5 (6,540.4–7,029.7) ms 6,996.2 (6,516.3–7,227.8) ms +1.9%
FCP@30k control 6,625.4 (6,369.5–6,691.3) ms 6,674.5 (6,382.2–6,916.9) ms +0.7%
create@30k profile 7,664.0 (7,471.8–7,822.5) ms 6,729.0 (6,377.4–6,964.1) ms -12.2%
create@30k control 7,335.3 (7,262.8–8,086.8) ms 6,434.8 (6,341.9–6,505.8) ms -12.3%

@lynx-js/[email protected] decoded main.lynx.bundle; gzip uses Node zlib over the exact decoded main/background bytes and whole artifacts. main.web.bundle is a Web wrapper rather than a tasm input, so it is measured directly.

control artifact P6 raw / gzip P7 raw / gzip gzip change
decoded main 159,168 / 68,396 B 159,558 / 68,604 B +0.30%
decoded background 235,247 / 63,162 B 244,850 / 65,926 B +4.38%
main.lynx.bundle 397,316 / 131,802 B 407,309 / 134,331 B +1.92%
main.web.bundle 399,373 / 108,130 B 409,352 / 111,105 B +2.75%

The profile build has the same conclusion: decoded main +0.31% gzip, background +4.34%, whole Lynx +2.08%, and Web +2.75%. Every bundle gzip cell remains below the 5% P7 budget; the background increase is the explicit cost of the new generic compiler/runtime path and tests do not ship in these bundles.

Validation

  • source PRs perf(lynx): cut keyed component materialization cost #31/perf(lynx): prove retained heap stretch #32/perf(lynx): lock teardown and storm constants #33 refetched immediately before publication; frozen heads remain exact
  • full Lynx project: 28 files / 307 tests
  • full Octane project: 421 files / 6,436 tests
  • focused compiler/renderer/plan-wire/integration neighborhood: 6 files / 218 tests
  • stage + attribution harness tests: 15/15
  • deterministic Lynx table semantics: 25/25 ratio guards
  • product-source scoped typecheck: 6 Lynx + 2 Octane source files
  • all 12 changed files pass pnpm format:files:check
  • pnpm sync; git diff --check; final worktree clean
  • exact-parent/final-SHA profile and control n=5 attribution captures; all oracles and worker release pass, zero DNF/null medians
  • reportable startup@10k/30k AB/BA n=5 plus decoded bundle/gzip audit

pnpm typecheck:files was also attempted with the changed Octane test files. It reaches pre-existing test-project declaration failures for @tsrx/core and .tsrx fixtures plus existing implicit-any sites in universal-renderer.test.ts; product-source scoped typecheck passes, and the full 6,436-test Octane project validates those test files through the repository's intended transform.

Risk / follow-ups

  • The optimization trusts a compiler capability and a transport proof together. A renderer that does not opt in, or a plan that is not announced/wire-safe, never enters the compact path.
  • Main first-screen rendering mirrors lazy owner use so adoption IDs remain aligned. Hooks, context, boundaries, nested components, renderer regions, and unsupported output force the ownerful shape.
  • Exact plan snapshots remain live only until acknowledgement. This intentionally trades one distinct-plan map entry for avoiding 150,000 command objects; retained-heap results include that lifetime.
  • Background bundle gzip grows 4.38%, close to but below the 5% budget. A follow-up should treat that as a budget to defend, not as free capacity.
  • Named CPU coverage falls to 65.2% after the named owners shrink. Further BTS work should first improve minified/GC attribution instead of assuming the remaining anonymous share belongs to this layer.
  • This PR does not edit or close perf(lynx): cut keyed component materialization cost #31/perf(lynx): prove retained heap stretch #32/perf(lynx): lock teardown and storm constants #33, their linked issues, or earlier reorganized PRs. Cleanup waits for explicit authority after the replacement stack is verified.

Provenance

@Huxpro

Huxpro commented Aug 11, 2026

Copy link
Copy Markdown
Owner Author

Conclusion: close as superseded. Upstream PRs 693 and 700 own the materialization architecture; the only remaining independently testable teardown idea is issue 43.

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