Skip to content

Commit 4222971

Browse files
vanceingallsclaude
andcommitted
perf(producer): parallelize transition-frame phase across worker DOM sessions
The prior hf#732 hybrid path kept transition frames pinned to the main DOM session while only parallelizing the cheaper normal-frame portion. perf- summary.json showed transitionCompositeMs summed to 109,838ms over 141 frames (avg 779ms) on a 175,973ms total capture, so the sequential transition phase was structurally upper-bound at ~110s of the 171s wall. This fix removes that structural cap. Every worker now walks a contiguous slice of the full frame range and runs both normal-frame and transition- frame compositing against its own browser session — processLayeredTransition Frame is self-contained per call (the dual-scene seek/mask/screenshot/ blend pattern only requires the same window.__hf state across the two scenes within ONE call), so there's no inter-frame dependency that would tie transitions to a specific session. Per-worker scratch buffers (bufferA/bufferB/output, 3 × width*height*6 bytes each) are allocated up front. The FrameReorderBuffer already supports arbitrary completion order, so encoder writes still hit the muxer in ascending index order. The new distributeLayeredHybridFrameRanges helper is exported so the partitioning contract is unit-testable. Five new tests pin: - non-overlapping, contiguous, full-coverage worker slices - the hf#732 transition shape (14 × 10-frame transitions on 840 frames) spreads transition work across multiple workers instead of pinning it all to worker 0 - the workerCount=0 / totalFrames=0 / workers > frames edge cases Empirical results on the SDR shader-transition repro (15 scenes × ~2s, 854×480, 14 × 0.3s shader transitions) match the structural change but do NOT close the predicted 5× gap. Wall times (current VM, w=6, 2 trials per cell, baseline collected on the same machine in the same session): Cell | BEFORE (hf#732) | AFTER | Speedup shader auto | 220s / 221s | 184s | ~1.18× shader w=6 explicit | 209s / 183s | 184s | ~1.07× hardcut auto | 9s / 10s | 9-10s | ~1.0× hardcut w=6 | 8s / 9s | 8-9s | ~1.0× The structural fix is correct — perf-summary.json shows transitions now run concurrently across workers (avg transitionCompositeMs climbs from 779ms to 882ms per frame due to expected cross-worker contention, with total work summed across all 6 workers). But the wall clock does not collapse to ~30s as predicted, because the actual bottleneck is NOT DOM-session serialization — it's the JS shader-blend in TRANSITIONS[shader](...) running on the Node main thread. Complex shaders like domain-warp, swirl-vortex, glitch iterate every pixel in JS with multiple noise/sample calls per pixel; for 854×480 that's hundreds of milliseconds per call on the main event loop. Six workers all firing shader-blends saturate the single Node thread, so observable parallelism caps at ~1.2× regardless of worker count (verified on this fixture: w=1=218s, w=2=183s, w=6=184s, w=12=188s — the curve flattens after w=2, consistent with a single-threaded JS bottleneck downstream of the worker pool). Follow-up to actually close the 5× gap is a separate change: move the shader-blend (TRANSITIONS[shader](...)) onto worker_threads so the per-pixel JS doesn't serialize on the orchestrator's event loop. The rgb48le scratch buffers are well-suited to zero-copy transfer via transferList. That refactor is out of scope for the hf#732 fix-up. — Vai Co-Authored-By: Vai <[email protected]>
1 parent cc80f91 commit 4222971

2 files changed

Lines changed: 234 additions & 101 deletions

File tree

‎packages/producer/src/services/renderOrchestrator.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getNextRetryWorkerCount,
1919
isRecoverableParallelCaptureError,
2020
materializeExtractedFramesForCompiledDir,
21+
distributeLayeredHybridFrameRanges,
2122
partitionTransitionFrames,
2223
resolveRenderWorkerCount,
2324
resolveCompositeTransfer,
@@ -718,6 +719,85 @@ describe("shouldUseHybridLayeredPath", () => {
718719
});
719720
});
720721

722+
describe("distributeLayeredHybridFrameRanges", () => {
723+
it("splits a frame range into contiguous, non-overlapping, full-coverage worker slices", () => {
724+
const ranges = distributeLayeredHybridFrameRanges(840, 6);
725+
expect(ranges).toHaveLength(6);
726+
expect(ranges[0]!.start).toBe(0);
727+
expect(ranges[ranges.length - 1]!.end).toBe(840);
728+
// Contiguous: each range begins where the previous ended.
729+
for (let i = 1; i < ranges.length; i++) {
730+
expect(ranges[i]!.start).toBe(ranges[i - 1]!.end);
731+
}
732+
// Full coverage: total frames == sum of slice sizes.
733+
const covered = ranges.reduce((acc, r) => acc + (r.end - r.start), 0);
734+
expect(covered).toBe(840);
735+
});
736+
737+
it("hf#732 fix-up: transition frames are spread across workers, not pinned to one session", () => {
738+
// Same shape as the #677 repro: 14 × 10-frame transitions on 840 frames at
739+
// evenly-spaced beats. Verify that any non-trivial slice of these
740+
// transition frames falls inside a NON-WORKER-ZERO slice — i.e. that the
741+
// hybrid path actually drives transitions on multiple worker sessions
742+
// (the prior implementation pinned all transitions to the main session
743+
// and only parallelized non-transition frames).
744+
const transitions: Array<{ startFrame: number; endFrame: number }> = [];
745+
for (let k = 0; k < 14; k++) {
746+
const startFrame = 30 + k * 50;
747+
transitions.push({ startFrame, endFrame: startFrame + 9 });
748+
}
749+
const totalFrames = 840;
750+
const ranges = distributeLayeredHybridFrameRanges(totalFrames, 6);
751+
const transitionFrames = partitionTransitionFrames(transitions, totalFrames);
752+
753+
const transitionFramesPerWorker = ranges.map(
754+
(range) => [...transitionFrames].filter((f) => f >= range.start && f < range.end).length,
755+
);
756+
757+
// Every worker should be doing transition work (in this shape: 14 evenly
758+
// spaced transitions across 6 contiguous slices, each slice covers ≥2 of
759+
// them). The PRIOR behavior would have given worker 0 all 140 transition
760+
// frames; the new behavior distributes them.
761+
const workersWithTransitions = transitionFramesPerWorker.filter((c) => c > 0).length;
762+
expect(workersWithTransitions).toBeGreaterThanOrEqual(2);
763+
764+
// Specifically: no single worker should own >70% of transition frames
765+
// (would indicate the partition collapsed to the old shape).
766+
const maxOnOneWorker = Math.max(...transitionFramesPerWorker);
767+
expect(maxOnOneWorker).toBeLessThan(Math.ceil(transitionFrames.size * 0.7));
768+
769+
// Sanity: total transition frames covered == partition cardinality.
770+
expect(transitionFramesPerWorker.reduce((a, b) => a + b, 0)).toBe(transitionFrames.size);
771+
});
772+
773+
it("clamps workerCount to 1 for non-positive inputs", () => {
774+
const ranges = distributeLayeredHybridFrameRanges(100, 0);
775+
expect(ranges).toHaveLength(1);
776+
expect(ranges[0]).toEqual({ start: 0, end: 100 });
777+
});
778+
779+
it("produces zero-width tail ranges when workers outnumber frames", () => {
780+
const ranges = distributeLayeredHybridFrameRanges(3, 6);
781+
// framesPerWorker = ceil(3 / 6) = 1; first 3 workers get one frame each,
782+
// remaining 3 get empty ranges.
783+
expect(ranges).toHaveLength(6);
784+
expect(ranges[0]).toEqual({ start: 0, end: 1 });
785+
expect(ranges[1]).toEqual({ start: 1, end: 2 });
786+
expect(ranges[2]).toEqual({ start: 2, end: 3 });
787+
expect(ranges[3]).toEqual({ start: 3, end: 3 });
788+
expect(ranges[4]).toEqual({ start: 3, end: 3 });
789+
expect(ranges[5]).toEqual({ start: 3, end: 3 });
790+
});
791+
792+
it("handles totalFrames = 0", () => {
793+
const ranges = distributeLayeredHybridFrameRanges(0, 4);
794+
expect(ranges).toHaveLength(4);
795+
for (const r of ranges) {
796+
expect(r).toEqual({ start: 0, end: 0 });
797+
}
798+
});
799+
});
800+
721801
describe("shouldUseLayeredComposite", () => {
722802
it("uses the layered compositor for SDR shader transition renders", () => {
723803
expect(

0 commit comments

Comments
 (0)