You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit bde9b88
Browse filesBrowse the repository at this point in the historyBrowse files
perf(producer): offload shader-blend to worker_threads to close JS event-loop ceiling
Follow-up to hf#732 (commit 4222971). The prior fix parallelized the
DOM-session work for layered transition frames across N browser sessions,
but the per-pixel JS shader-blend at the tail of
`processLayeredTransitionFrame` (`TRANSITIONS[shader](from, to, output,
w, h, progress)`) still executed on the Node main event loop. Complex
shaders (`domain-warp`, `swirl-vortex`, `glitch`) iterate every pixel of
the rgb48le buffer with multiple noise/sample calls per pixel —
hundreds of milliseconds per call — so N concurrent DOM workers all
firing shader-blends saturated the single thread. The worker-count
sweep on the hf#677 fixture flattened after w=2 (w=1=218s, w=2=183s,
w=6=184s, w=12=188s) — the classic single-threaded-downstream signature.
This change moves the shader-blend onto a Node `worker_threads` pool:
- `shaderTransitionWorkerPool.ts` — pool spawned once per layered render
(sized to `min(layeredWorkerCount, cpuCount)`), terminated in the
`finally`. Idle/busy slots with a FIFO queue; each task posts the
three rgb48le `ArrayBuffer`s to a worker via `transferList` for
zero-copy detach, runs the blend, posts the output back (also via
`transferList`). Main-thread Buffer references are swapped to the
re-attached views post-await.
- `shaderTransitionWorker.ts` — worker entry. Imports `TRANSITIONS` and
`crossfade` from a new `@hyperframes/engine/shader-transitions`
subpath export that points straight at the standalone
`shaderTransitions.ts` file. The subpath sidesteps a tsx / worker_threads
loader-rewrite limitation: the engine's root index transitively
imports `./config.js` and other files, and the `.js → .ts` rewrite
does not survive the Worker boundary in dev/test. The standalone file
has zero internal imports so the worker loads cleanly under both
Node-direct (prod) and tsx/vitest (dev/test).
- `renderOrchestrator.ts` — `processLayeredTransitionFrame` takes an
optional `shaderPool` arg. When the pool is present (hybrid SDR
multi-worker path with transitions), the shader-blend dispatches off
the main thread; otherwise the legacy synchronous path runs (HDR
fallback, single-worker, all-transition edge case). Both branches
produce byte-identical output. A new `transitionShaderBlendMs` timing
key surfaces the actual blend cost in `perf-summary.json`.
- `build.mjs` — adds `shaderTransitionWorker.ts` as a third esbuild
entry point so `dist/services/shaderTransitionWorker.js` is shipped
for production loads. The pool probes for the `.js` first and falls
back to the `.ts` source in dev.
- `cli/tsup.config.ts` — mirrors the `@hyperframes/engine/shader-transitions`
alias so the CLI's tsup bundle resolves the subpath the same way the
producer's build does.
Buffer transfer semantics: `transferList: [bufferA.buffer, bufferB.buffer,
output.buffer]` detaches the original ArrayBuffers on the sender (Buffer
`.length` collapses to 0). The worker posts the same three buffers back,
also via transferList. The orchestrator re-points `buffers.bufferA` /
`buffers.bufferB` / `buffers.output` at the returned Buffer views before
the next iteration touches them. The mutation is documented in the
`LayeredTransitionBuffers` docblock.
Pool lifecycle is bounded by the hybrid dispatch block. If pool spawn
fails (e.g. resource exhaustion) we log a warn and fall back to the
inline blend path rather than failing the render. The pool's
`terminate()` rejects both queued and in-flight tasks before
`Worker.terminate()` to avoid leaks; mid-render worker crashes are
treated as fatal (the in-flight buffers have been transferred and
cannot be reconstructed).
Tests:
- `shaderTransitionWorkerPool.test.ts`: 6 unit tests covering
byte-equivalence with the inline path across all 15 shaders,
transferList detach semantics, unknown-shader fallback to crossfade,
concurrent dispatch correctness across a 4-worker pool, and clean
terminate-without-hanging on a busy pool.
- Existing `renderOrchestrator.test.ts` regressions: 22 layered/hybrid
tests still pass; the one pre-existing Linux/Windows path failure
remains untouched.
— Vai
Co-Authored-By: Vai <[email protected]>
0 commit comments