Skip to content

Commit e6476c2

Browse files
perf(engine): Macs with a hardware GPU render with more capture workers (#6636)
* perf(engine): Macs with a hardware GPU render with more capture workers * perf(engine): Apple-GPU worker sizing skips layered renders; fewer comment lines * test(producer): format
1 parent 2ddf41e commit e6476c2

6 files changed

Lines changed: 90 additions & 5 deletions

File tree

‎packages/engine/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ export {
310310
type WorkerResult,
311311
type WorkerSizing,
312312
type WorkerSizingBound,
313+
type WorkerSizingConfig,
313314
type LandedFrame,
314315
type ParallelProgress,
315316
type ParallelWorkerPhaseDiagnostic,

‎packages/engine/src/services/parallelCoordinator.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ describe("computeWorkerSizing", () => {
167167
expect(computeWorkerSizing(1300, undefined, { concurrency: "auto" }).workers).toBe(5);
168168
});
169169

170+
it("sizes Apple-GPU disk capture by its own heap and core budget", () => {
171+
vi.mocked(cpus).mockReturnValue(
172+
Array.from({ length: 14 }, () => ({}) as ReturnType<typeof cpus>[number]),
173+
);
174+
vi.mocked(totalmem).mockReturnValue(24 * 1024 ** 3);
175+
vi.mocked(getHeapStatistics).mockReturnValue({
176+
heap_size_limit: 4192 * 1024 ** 2,
177+
} as ReturnType<typeof getHeapStatistics>);
178+
const apple = { concurrency: "auto" as const, appleGpuDiskCapture: true };
179+
const sizing = computeWorkerSizing(1300, undefined, apple);
180+
expect(sizing.workers).toBe(8);
181+
expect(sizing.boundBy).toBe("memory");
182+
expect(sizing.exceedsHeapAdvisory).toBe(false);
183+
expect(computeWorkerSizing(1300, undefined, { ...apple, coresPerWorker: 4 }).workers).toBe(3);
184+
});
185+
170186
it("matches calculateOptimalWorkers and reports every constraint", () => {
171187
const config = { concurrency: "auto" as const };
172188
const sizing = computeWorkerSizing(900, undefined, config);

‎packages/engine/src/services/parallelCoordinator.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export interface WorkerSizingConfig extends Partial<
131131
* than a plain DOM screenshot. Explicit --workers requests ignore this hint.
132132
*/
133133
captureCostMultiplier?: number;
134+
/** macOS, hardware GPU, screenshot frames written to disk (not streamed through this process). */
135+
appleGpuDiskCapture?: boolean;
134136
}
135137

136138
type WorkerBrowserPoolDecision = {
@@ -151,6 +153,10 @@ const HEAP_RESERVED_MB = 1024;
151153
// default heap ⇒ >~500MB/worker + base. Caps auto sizing; an explicit
152154
// `--workers N` is still the operator's call.
153155
const HEAP_PER_WORKER_MB = 640;
156+
// Apple-GPU disk capture measured ~15-25 MB parent heap per worker; 14 cores kept gaining to 8.
157+
const APPLE_DISK_HEAP_PER_WORKER_MB = 128;
158+
const APPLE_DISK_CORES_PER_WORKER = 1.75;
159+
const APPLE_DISK_MAX_WORKERS = 8;
154160
const MIN_WORKERS = 1;
155161
const MAX_WORKER_DIAGNOSTIC_LINES = 8;
156162
// Hard ceiling on explicit `--workers N` requests. Above this, the cost of
@@ -321,9 +327,13 @@ export function computeWorkerSizing(
321327
// is immediately reclaimable.
322328
const totalMemoryMb = getSystemTotalMb();
323329
const heapLimitMb = Math.round(getHeapStatistics().heap_size_limit / (1024 * 1024));
330+
const appleDisk = config?.appleGpuDiskCapture === true;
324331
const heapBasedWorkers = Math.max(
325332
1,
326-
Math.floor((heapLimitMb - HEAP_RESERVED_MB) / HEAP_PER_WORKER_MB),
333+
Math.floor(
334+
(heapLimitMb - HEAP_RESERVED_MB) /
335+
(appleDisk ? APPLE_DISK_HEAP_PER_WORKER_MB : HEAP_PER_WORKER_MB),
336+
),
327337
);
328338
const cpuBasedWorkers = Math.max(1, cpuCount - 2);
329339
const memoryBasedWorkers = Math.max(1, Math.floor((totalMemoryMb * 0.5) / MEMORY_PER_WORKER_MB));
@@ -362,9 +372,15 @@ export function computeWorkerSizing(
362372
if (concurrency !== "auto") {
363373
return Math.max(MIN_WORKERS, Math.min(ABSOLUTE_MAX_WORKERS, Math.floor(concurrency)));
364374
}
365-
return defaultSafeMaxWorkers();
375+
return appleDisk
376+
? Math.max(defaultSafeMaxWorkers(), APPLE_DISK_MAX_WORKERS)
377+
: defaultSafeMaxWorkers();
366378
})();
367-
const effectiveCoresPerWorker = config?.coresPerWorker ?? DEFAULT_CONFIG.coresPerWorker;
379+
const configuredCoresPerWorker = config?.coresPerWorker ?? DEFAULT_CONFIG.coresPerWorker;
380+
const effectiveCoresPerWorker =
381+
appleDisk && configuredCoresPerWorker === DEFAULT_CONFIG.coresPerWorker
382+
? APPLE_DISK_CORES_PER_WORKER
383+
: configuredCoresPerWorker;
368384
const effectiveMinParallelFrames = config?.minParallelFrames ?? DEFAULT_CONFIG.minParallelFrames;
369385
const effectiveLargeRenderThreshold =
370386
config?.largeRenderThreshold ?? DEFAULT_CONFIG.largeRenderThreshold;

‎packages/producer/src/services/render/captureCost.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type CaptureSession,
1818
type EngineConfig,
1919
type WorkerSizing,
20+
type WorkerSizingConfig,
2021
calculateOptimalWorkers,
2122
captureFrameToBuffer,
2223
closeCaptureSession,
@@ -138,7 +139,7 @@ export function buildHeapAdvisoryWarning(
138139
export function resolveRenderWorkerCount(
139140
totalFrames: number,
140141
requestedWorkers: number | undefined,
141-
cfg: EngineConfig,
142+
cfg: EngineConfig & Pick<WorkerSizingConfig, "appleGpuDiskCapture">,
142143
compiled: Pick<CompiledComposition, "hasShaderTransitions" | "renderModeHints">,
143144
log: ProducerLogger = defaultLogger,
144145
measuredCaptureCost?: CaptureCostEstimate,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
SEGMENTED_MIN_SECONDS_AFTER_SOAK,
6565
resolveParallelCaptureMode,
6666
shouldStreamParallelCapture,
67+
isAppleGpuDiskCapture,
6768
shouldUseStreamingEncode,
6869
resolveObservedCaptureMode,
6970
createCaptureObservabilityUpdater,
@@ -3665,6 +3666,23 @@ describe("calibrationCaptureConfig", () => {
36653666
});
36663667
});
36673668

3669+
describe("isAppleGpuDiskCapture", () => {
3670+
const disk = {
3671+
platform: "darwin",
3672+
browserGpuMode: "hardware",
3673+
layered: false,
3674+
parallelStream: false,
3675+
} as const;
3676+
3677+
it("holds only for hardware-GPU Mac renders that write frames to disk", () => {
3678+
expect(isAppleGpuDiskCapture(disk)).toBe(true);
3679+
expect(isAppleGpuDiskCapture({ ...disk, layered: true })).toBe(false);
3680+
expect(isAppleGpuDiskCapture({ ...disk, parallelStream: true })).toBe(false);
3681+
expect(isAppleGpuDiskCapture({ ...disk, browserGpuMode: "software" })).toBe(false);
3682+
expect(isAppleGpuDiskCapture({ ...disk, platform: "win32" })).toBe(false);
3683+
});
3684+
});
3685+
36683686
describe("shouldClampDefaultDrawElement (default-on drawElement clamp)", () => {
36693687
const unverifiedParallel = {
36703688
useDrawElement: true,

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2797,6 +2797,21 @@ export function shouldSegmentCapture(args: {
27972797
return args.durationSeconds >= resolveSegmentedMinSeconds(args.env);
27982798
}
27992799

2800+
/** Worker sizing's Apple-GPU budget fits only frames written to disk by screenshot workers. */
2801+
export function isAppleGpuDiskCapture(args: {
2802+
platform: NodeJS.Platform;
2803+
browserGpuMode: "software" | "hardware";
2804+
layered: boolean;
2805+
parallelStream: boolean;
2806+
}): boolean {
2807+
return (
2808+
args.platform === "darwin" &&
2809+
args.browserGpuMode === "hardware" &&
2810+
!args.layered &&
2811+
!args.parallelStream
2812+
);
2813+
}
2814+
28002815
export function shouldStreamParallelCapture(args: {
28012816
/** Router switch for this render — see isCaptureParallelStreamRouterEnabled. */
28022817
routerEnabled: boolean;
@@ -4037,10 +4052,28 @@ async function executeRenderPipeline(input: {
40374052

40384053
// Low-memory safe-mode's single-worker pin lives inside
40394054
// resolveRenderWorkerCount so its "why workers=N" logging stays coherent.
4055+
const appleGpuDiskCapture = isAppleGpuDiskCapture({
4056+
platform: process.platform,
4057+
browserGpuMode: resolvedBrowserGpuMode,
4058+
layered: shouldUseLayeredComposite({
4059+
hasHdrContent,
4060+
hasShaderTransitions: compiled.hasShaderTransitions && !isGif,
4061+
isPngSequence,
4062+
}),
4063+
parallelStream: isCaptureParallelStreamRouterEnabled(
4064+
process.env,
4065+
resolveParallelCaptureMode({
4066+
platform: process.platform,
4067+
headlessShellPath: resolveHeadlessShellPath(cfg),
4068+
forceScreenshot: captureForceScreenshot,
4069+
deviceScaleFactor: captureOptions.deviceScaleFactor,
4070+
}),
4071+
),
4072+
});
40404073
workerCount = resolveRenderWorkerCount(
40414074
totalFrames,
40424075
job.config.workers,
4043-
cfg,
4076+
{ ...cfg, appleGpuDiskCapture },
40444077
compiled,
40454078
log,
40464079
captureCalibration?.estimate,

0 commit comments

Comments
 (0)