From 3b6a07bea9c1e254d6d27c4c6b077b26e9cc8366 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 3 Aug 2026 14:46:54 +0800 Subject: [PATCH] perf(fmt): cap default worker count at eight --- packages/rstack/src/fmt/workerPool.ts | 7 ++++++- packages/rstack/tests/fmt/workerPool.test.ts | 8 +------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/rstack/src/fmt/workerPool.ts b/packages/rstack/src/fmt/workerPool.ts index 7ae657d..974dd38 100644 --- a/packages/rstack/src/fmt/workerPool.ts +++ b/packages/rstack/src/fmt/workerPool.ts @@ -14,8 +14,13 @@ interface FmtWorkerPool { terminate: () => Promise; } +/** + * Caps the default worker count at 8 because formatter throughput can + * plateau before all CPU cores are occupied, while additional workers increase + * scheduling and memory pressure. + */ const getFmtWorkerCount = (fileCount: number, maxWorkers?: number): number => - Math.min(fileCount, maxWorkers ?? Math.max(1, availableParallelism() - 1)); + Math.min(fileCount, maxWorkers ?? Math.min(8, Math.max(1, availableParallelism() - 1))); const getFmtWorkerUrl = (): URL => { // Source tests run after build and exercise the same worker artifact as the CLI. diff --git a/packages/rstack/tests/fmt/workerPool.test.ts b/packages/rstack/tests/fmt/workerPool.test.ts index 7dcd83f..39637c0 100644 --- a/packages/rstack/tests/fmt/workerPool.test.ts +++ b/packages/rstack/tests/fmt/workerPool.test.ts @@ -1,17 +1,11 @@ -import { availableParallelism } from 'node:os'; import { expect, test } from 'rstack/test'; import { getFmtWorkerCount } from '../../src/fmt/workerPool.ts'; -test('uses one fewer worker than the available parallelism by default', () => { - const defaultWorkerCount = Math.max(1, availableParallelism() - 1); - - expect(getFmtWorkerCount(defaultWorkerCount + 1)).toBe(defaultWorkerCount); -}); - test.each([ [4, 1, 1], [4, 2, 2], [2, 4, 2], + [12, 10, 10], ])('uses %s files and %s configured workers as %s workers', (files, workers, expected) => { expect(getFmtWorkerCount(files, workers)).toBe(expected); });