worker_threads: allocating workers block on each other's GC marking; BUN_JSC_numberOfGCMarkers=1 halves 8-worker wall time
What version of Bun is running?
1.4.2, and 1.4.3-canary (the #44103 build, 47eeafc2c)
What platform is your computer?
Linux 7.2 x86_64 (WSL2), 16 vCPU
What steps can reproduce the bug?
Each worker does the same fixed amount of work: it allocates short-lived objects holding fresh strings. The script times 1 worker, then 8 at once, and records each worker's own thread CPU time from /proc/thread-self/stat next to its wall time.
// alloc-workers.mjs — run with: bun alloc-workers.mjs / node alloc-workers.mjs
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';
import { readFileSync } from 'node:fs';
const threadCpuMs = () => { const f = readFileSync('/proc/thread-self/stat', 'utf8').split(') ')[1].split(' '); return (Number(f[11]) + Number(f[12])) * 10; };
const ITER = 3_000_000;
if (!isMainThread) {
const t = performance.now(), c = threadCpuMs();
let acc = 0;
for (let i = 0; i < ITER; i++) {
const n = (i + workerData.seed) % 20000, v = 'v' + (n % 97);
const o = { id: 'n' + n, name: 'symbol' + n, kind: 'function', file: 'src/file' + (n % 500) + '.ts', line: n % 900, sig: 'sig', doc: 'doc', c0: v, c1: n, c2: v, c3: n, c4: v, c5: n, c6: v, c7: n, c8: v, c9: n, c10: v, c11: n, c12: v, c13: n };
acc += o.line + o.id.length;
}
parentPort.postMessage({ ms: performance.now() - t, cpu: threadCpuMs() - c, acc });
} else {
const run = (n) => Promise.all(Array.from({ length: n }, (_, seed) => new Promise((res) => {
const w = new Worker(new URL(import.meta.url), { workerData: { seed } });
w.once('message', (m) => { w.terminate(); res(m); });
}))).then((r) => ({ wall: r.reduce((a, b) => a + b.ms, 0) / n, cpu: r.reduce((a, b) => a + b.cpu, 0) / n }));
await run(1);
const one = await run(1), eight = await run(8);
const rt = typeof Bun !== 'undefined' ? `bun ${Bun.version}` : `node ${process.version}`;
console.log(`${rt}: 1 worker ${one.wall.toFixed(0)} ms (thread cpu ${one.cpu.toFixed(0)}); 8 workers ${eight.wall.toFixed(0)} ms each (thread cpu ${eight.cpu.toFixed(0)}); 8/1 = ${(eight.wall / one.wall).toFixed(2)}`);
}What is the expected behavior?
8 workers doing independent work on a 16-vCPU machine should each take about as long as one worker does alone, the way Node's do (8/1 = 1.1–1.6 below). Each worker's wall time should be close to its own CPU time.
What do you see instead?
On Bun, each of the 8 workers spends most of its extra time blocked, not running. Thread CPU per worker rises only a little (120–180 → 180–200 ms), but wall time per worker roughly triples.
Setting BUN_JSC_numberOfGCMarkers=1 removes most of the wait. Wall time falls back to within about 15% of thread CPU, and the single-worker case gets faster too. These are 3 alternating rounds on the same host:
| runtime | 1 worker wall (thread cpu) | 8 workers wall each (thread cpu) | 8/1 |
|---|---|---|---|
| node 24.21.0 | 35–38 ms (30–40) | 40–57 ms (36–50) | 1.11–1.62 |
| bun 1.4.2 | 156–219 ms (130–180) | 300–452 ms (185–196) | 1.61–2.07 |
bun 1.4.2, numberOfGCMarkers=1 |
97–117 ms (90–120) | 183–200 ms (171–184) | 1.70–2.00 |
| bun 1.4.3-canary | 141–181 ms (120–150) | 377–474 ms (179–199) | 2.18–3.22 |
bun 1.4.3-canary, numberOfGCMarkers=1 |
95–115 ms (90–120) | 183–215 ms (170–189) | 1.87–1.92 |
(Node's absolute times are low because V8 optimizes the object away; the comparison that matters is wall against thread CPU.)
With one marker, a smaller on-CPU cost remains: each worker's own CPU time still rises about 1.7× from 1 to 8 workers, against about 1.2× on Node. I haven't isolated that part.
Additional information
A perf profile of the 8-worker run, made with the symbolized bun-profile build, shows the same thing. Per-worker CPU samples grow only 1.16× from 1 to 8 workers, while wall time grows about 3×. The waiting appears as parkConditionallyImpl and __condvar_* frames.
The GC helper threads that parallel marking uses appear to be shared by the whole process (heapHelperPool()), so each worker's collection seems to queue behind the others' marking work. #37777 already sets numberOfGCMarkers=1 for Bun's own short-lived build helpers, for a different reason (thread count).
This shows up in real use as a pool of query workers answering concurrent requests. There, 8 concurrent queries take about 1.4× as long on Bun as on Node, while one query at a time is at parity. The SQLite side of that workload has a separate cause, which I'm filing on its own.
Source: oven-sh/bun