Skip to content

perf(lynx): lazy-load the main-thread worklet bridge - #41

Closed
Huxpro wants to merge 1 commit into
perf/lynx-upstream-protocol-v2from
perf/lynx-upstream-worklet-bridge
Closed

Huxpro wants to merge 1 commit into
perf/lynx-upstream-protocol-v2from
perf/lynx-upstream-worklet-bridge

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 11, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Resolve the main-thread worklet registry and cross-thread call bridge through a side-effect-free slot instead of a static value import from worklets.ts.
  • Provide that slot only from the public entry points that can actually put a worklet feature in front of the receiver. A no-worklet main-thread bundle therefore tree-shakes the registry/call machinery; worklet bundles retain it.
  • Add behavioral and production-bundle boundary tests plus absolute per-slice gzip guards. Worklet-addressed traffic arriving at a no-worklet receiver fails cleanly instead of crashing or resolving against missing state.

Why

The Lynx main-thread receiver is required by every app, but most apps do not compile a main-thread worklet. A static edge from the receiver into worklets.ts made those apps retain the registry, isolation, and call-bridge implementation anyway. The safe removal boundary is usage, not a flag: compiled worklet entry points provide the bridge before receiver installation, while ordinary first-screen/commit paths never edge the implementation into the final main slice.

This is the independently mergeable P6 replacement for #15, refetched immediately before publication at frozen head 4a53620fe811a016cb9966fab53ca181a89159c8 over source parent e627ca5979ed77fbaf8ef5eafbea86c482f64cf5. The historical PR remains untouched.

Changes

  • packages/lynx/src/core/worklet-bridge.ts: add a side-effect-free, first-provider-wins bridge slot. Its dependencies on worklets.ts are type-only.
  • packages/lynx/src/main-renderer.ts: turn the thread-function re-exports into bridge-providing wrappers; provide before useMainThreadRef creates its descriptor; expose an explicit ensure entry point for hosts that hand-register through the lower-level module.
  • packages/lynx/src/main-thread.ts: remove value imports from worklets.ts, resolve the bridge once per install, null-guard registry setup/teardown and ref-owner publication, and report clean no-worklet errors for remote/host dispatches.
  • packages/lynx/src/first-screen.ts: re-export the bridge-providing runOn* wrappers.
  • benchmarks/lynx-bundle-size: emit frozen preview/IFR slice targets and gate preview main, IFR main, background, and whole IFR gzip independently.
  • Add a patch changeset for @octanejs/lynx and focused public API, thread-call, main-event, compatibility, behavioral no-bridge, and real esbuild tree-shaking coverage.

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 and rejected levers

  • Replaced the source patch's per-call bridge object construction with one module-scope constant. This avoids a fresh object on every registration/bind/invoke/run/ref path while the production bundle-boundary test proves the constant still disappears from no-worklet output.
  • Kept the wrappers explicit so their public signatures remain readable and independently tree-shakable; a generic forwarding helper would hide types without removing the required usage boundary.
  • Kept a single lazy boundary. There is no runtime flag, fallback registry, second install lifecycle, or dynamic import.
  • The source PR's two other experiments remain deliberate non-changes: removing all production diagnostics had measured only 234 gzip bytes, and unifying the live first-screen renderer with the static wire-plan stamper offered a small projected byte win at a disproportionate semantic risk. Neither is carried into this layer, and no result from those historical experiments is presented as a current-HEAD benchmark.

Performance audit

Exact comparison:

  • parent: b41944e6effd65d3d967af9233e8cf6569ed8301
  • candidate: 68b2b0546b28d54b7cd8d44665f95d36b58e48b3
  • host: 32x Intel Xeon Platinum 8336C, Linux 5.15.120.bsk.3-amd64, Node 22.22.2, Chromium 149.0.7827.55
  • bundle input: fresh build from each exact revision; tasm decode plus deterministic gzip level 9
  • ignored raw outputs: benchmarks/results/p6-parent/lynx-bundle-size.json in the exact-P5 worktree; benchmarks/results/p6-final-bundle/lynx-bundle-size.json, benchmarks/results/p6-final-wire-full/lynx-table.json, and benchmarks/lynx-table/stages/results/{report.json,report.md} on P6

Bundle owner signal

artifact P5 raw P6 raw P5 gzip P6 gzip gzip change
preview main 158,266 151,844 64,855 62,420 -2,435 (-3.75%)
preview background 230,314 230,314 61,906 61,907 +1 (+0.002%)
preview whole bundle 389,232 382,810 126,736 124,312 -2,424 (-1.91%)
IFR main 161,503 154,815 69,245 66,685 -2,560 (-3.70%)
IFR background 230,314 230,314 61,906 61,907 +1 (+0.002%)
IFR whole bundle 392,469 385,781 130,953 128,456 -2,497 (-1.91%)

The main slices lose exactly the worklet edge: raw background length is unchanged, and the one-byte gzip movement there is not claimed as a regression or win. The final bundle run passes all 6 applicable guards. Red-checking a static worklets.ts value edge makes the no-worklet marker probe fail and restores roughly 2.4–2.6 KB gzip to the main slices.

Startup FCP and zero-DNF sanity

The exact P5 and P6 revisions were each built and run at 10k with five fresh pages for the control bundle and five for the stage-profile bundle. The harness aborts on a DNF/null FCP; all 20/20 samples completed. Runs were sequential on the same host, so the table is a regression sanity check rather than a portable speedup claim.

raw view-attach FCP@10k P5 median (min–max) P6 median (min–max) change
control 2,282.1 ms (2,217.8–2,364.4) 2,344.7 ms (2,278.6–2,418.6) +2.7%
stage-profile 2,342.3 ms (2,283.8–2,369.9) 2,372.2 ms (2,266.1–2,425.8) +1.3%

There is no resolvable FCP improvement. The directly observed main-slice evaluation median moves 18.9 → 17.8 ms, but total FCP is dominated by layout/Web Core publication/observer residual (about 72%) and the raw ranges overlap. The small adverse raw medians are below the run spread and are disclosed, not converted into a claim.

An additional exact-P6 one-repetition end-to-end sanity run completed create, update, select, update storm, and select storm at both 1k and 10k with zero DNFs. Its host-bound 1k values were 320/40/26/103/261 ms and 10k values were 2,684/246/114/773/3,130 ms; they are completion evidence only, not performance claims.

Deterministic protocol behavior

The final exact-HEAD 1k/10k ratio run passes all 25 applicable guards. P6 does not change transport work: create remains one command per row; update10th is 100/1,000 commands and Row renders; select is 2 commands / 237 commit bytes / 2 Row renders; swap is 997 commands / 0 Row renders; unpaced storms remain 50 update commits and 30 select commits. Paced update remains 3 commits / 300 commands over 3 frames.

Validation

  • source PR perf(lynx): receiver diet — lazy worklet bridge, per-slice gzip gates ([lynx-perf 7]) #15 refetched immediately before publication; head remains 4a53620fe811a016cb9966fab53ca181a89159c8
  • pnpm typecheck — repo-wide package, typetest, example, website, and benchmark matrix
  • all Lynx tests: 28 files / 307 tests (the sandbox-only run first recorded 296 passes plus 11 spawnSync node EPERMs; the identical unrestricted run passed 307/307)
  • focused bridge/thread/public API tests: 4 files / 36 tests
  • node benchmarks/bench.mjs --ratios --results-dir=benchmarks/results/p6-final-bundle lynx-bundle-size — 6/6 guards
  • node benchmarks/bench.mjs --ratios --results-dir=benchmarks/results/p6-final-wire-full lynx-table — 25/25 guards
  • exact-P5 and exact-P6 reportable n=5 startup/stage runs; 20/20 FCP samples complete
  • exact-P6 1k/10k all-operation web sanity; zero DNFs
  • scoped format check across all 12 changed files; pnpm sync; git diff --check; final worktree clean

Risk / follow-ups

  • Provision is usage-driven. A hypothetical app that dynamically imports its only worklet after installLynxMainThread would leave that install bridge-less. Supported Lynx main-thread output is a statically evaluated slice; unsupported late traffic now fails loudly and cleanly.
  • A host-provided executeMainThreadWorklet with no bridge owns the returned value's serializability, so that exceptional path does not run Octane's worklet isolation helper. Normal compiled worklet installs isolate exactly as before.
  • The bridge slot is process-global and first-provider-wins, matching the existing module-global worklet implementation. Install/teardown ownership remains per receiver.
  • This layer owns a bundle-shape improvement, not a startup wall-clock claim. P7 and the cumulative neutral checkpoint must remeasure FCP rather than inheriting this layer's byte result as a time result.

Provenance

Resolve the main-thread worklet registry and call bridge through a side-effect-free slot provided only by actual worklet entry points. No-worklet bundles now shed the registry and cross-thread call machinery, while worklet bundles retain the public behavior and no-worklet traffic reports a clean error.

On the exact P5 parent and final P6 candidate, preview main gzip falls 64,855 to 62,420 bytes and IFR main gzip falls 69,245 to 66,685 bytes. Background raw length is unchanged; background gzip moves by one byte. Dedicated bundle-boundary tests pin both the absent and retained cases, and the deterministic bundle harness gates each slice.
@Huxpro

Huxpro commented Aug 11, 2026

Copy link
Copy Markdown
Owner Author

Conclusion: close as failed by the live benchmark gate. The reimplemented worklet bridge regressed selectStorm; only the narrower optional-worklet cold-path hypothesis in issue 44 remains worth testing.

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