Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/rstack/src/fmt/workerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ interface FmtWorkerPool {
terminate: () => Promise<void>;
}

/**
* 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.
Expand Down
8 changes: 1 addition & 7 deletions packages/rstack/tests/fmt/workerPool.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});