Public: anyone can read this; the crew can change it.
Take a CPU and heap profile on the stage rig at about 420 cells and name the 1-4 s pause #16122
Check · Review · Land · Live done — Landed and live in production.
The 1–4 s pause is heapSize() from bun:jsc, not a garbage collection and not the egress sweep. I couldn't reproduce production's real 8 GiB heap, only a retained heap of millions of small objects.
shipped production is serving it deploy 3fede26
1 earlier comment · 1 event
The 1–4 s pause is heapSize() from bun:jsc, not a garbage collection and not the egress sweep. I couldn't reproduce production's real 8 GiB heap, only a retained heap of millions of small objects.
What I found
- The rig:
scripts/pause-rig.tsruns 420 egress servers with four connections each, a retained heap, and a 10 ms loop-lag probe. I ran it underbun --cpu-prof. - The profile: with 420 cells and a 1 GiB heap,
heapSizeheld the thread for 47.9 s of 57 s. The sweep's own loop (egress.ts:1006) took 67 ms andduringtook 19 ms. - Why:
src/perf/job_alloc.tscallsheapSize()twice inside everyduring(). In Bun 1.4.4 it isn't constant time; it costs about 17 ms per GiB of heap per call. Oneduring()takes 24 ms at 1 GiB, 123 ms at 4 GiB and 263 ms at 7.6 GiB. 420 timers each pay that every slice, and the stall watcher names the job that ran across the stall, soegress-idlegets the label. - GC: a full collection of the 1 GiB heap took 1.1–1.6 s. That's the right size for a pause, but it is not what blocked the thread.
- Not proven: a heap that falls across a stall is how the stall line infers a collection. It said "no collection seen" because the pause was the size read, not a collection.
What changed
createTallynow spends at most 0.5% of wall time, plus a first 50 ms, reading the heap size. Slices past that budget still run, just uncounted. Thememory allocated by jobsline is therefore a sample on a big heap.- On the rig at 420 cells and 1 GiB, the 20–25 s stalls are gone; only 0.3–0.6 s pauses remain.
- The finding and the size table are written up in
docs/features/performance-watcher.md. test/job-alloc.test.tshas a new test for the budget: a 100 ms-per-read heap does about 100 reads, not 2,000. The existing tests there now pass a stopped clock so the real heap's read time can't spend the budget.
What I didn't do
- I didn't remove the allocation count or touch the once-a-minute
heapSize()inmemory_watch.ts. - I didn't run the whole suite.
job-alloc.test.tspasses three runs in a row,tsc --noEmitis clean, andgate --fixis committed. src/apps/egress.tsstill has the per-serversetInterval, so #16108's timer removal isn't on this base.
To try it: bun --cpu-prof --cpu-prof-dir=$TMPDIR/prof scripts/pause-rig.ts 420 4 1000 30. The arguments are cells, connections per cell, retained MiB and seconds.
🤖 Generated with Claude Code
The diagnosis is well measured and the fix is bounded and tested. One change is needed: the budget makes the memory allocated by jobs line quietly wrong, and nothing on the line says so.
- Skipped slices are invisible. At 8 GiB a read costs about 130 ms, so 0.5% of wall time allows roughly one counted slice per 50 s. After the 50 ms allowance, which the first few sweeps use up, the line is a sample of whichever jobs ran when the budget refilled. It is not a floor any more, which is what the header comment still promises. The sampling is described only in the docs, not on the line a reader sees. Count the uncounted slices (a
skippedtotal on the tally) and haveallocationLinesay it, for example(sampled: N slices not counted), and omit it when N is 0. - Add a test that would fail without that: a tally over budget yields a line that carries the skipped count. Also assert in the existing budget test that the count equals the number of slices that ran uncounted.
- Fix the header comment of
job_alloc.ts. "this is a floor" is no longer true once slices are skipped.
Minor: scripts/pause-rig.ts has no test, which is acceptable for a measurement rig. Say in the doc that it is not run by CI. I did not exercise the preview, because the change is not a page.
Slices skipped over the heap-read budget are now counted, and the memory allocated by jobs line says how many. I pushed this to the same branch.
- Skipped count:
take()now returns{ jobs, skipped }instead of a bare array.countedincrementsskippedwhenever the budget makes it run a slice uncounted, andtakeresets it.allocationsOfsums it across the thread's unowned jobs and this forge's jobs. - The line: it ends
(sampled: N slices not counted), and omits that when N is 0. - Header comment:
job_alloc.tsno longer says "this is a floor". It says the totals undercount, and that they are a floor only whenskippedis 0. - Tests:
- The existing budget test now asserts
skippedequals the slices that ran uncounted (1000 minus reads / 2). - A new test shows a tally over its budget yields a line ending in the skipped count, and that reading it resets the count.
- The quiet-run test asserts no "sampled" text appears when nothing was skipped.
- The other tests in
test/job-alloc.test.tschanged only to read.jobsfrom the new return shape.
- The existing budget test now asserts
- Doc:
docs/features/performance-watcher.mdnow describes the(sampled: …)suffix and saysscripts/pause-rig.tsis a measurement rig thatbun testand CI do not run.
test/job-alloc.test.ts passes, bun x tsc --noEmit is clean, and gate.ts --fix rewrapped one file, which is in the commit. I did not exercise the preview, since this is not a page change.
I did not add a test for the rig, as you said that is acceptable.
Approved. The earlier ask was that the line stop calling the tally a floor and say how many slices ran uncounted. That is met. take() returns { jobs, skipped }, and allocationsOf sums skipped across the thread and this forge. The line ends (sampled: N slices not counted) and leaves the suffix off when N is 0. The header comment now says the totals are a floor only when skipped is 0. Tests cover the skipped count and the reset on take. The quiet-run test asserts no "sampled" text. The budget test asserts skipped equals 1000 minus reads/2. The doc is under docs/features, README is untouched, and there is no new dependency. The rig is a script that CI does not run, and the doc says so. There is no security surface here: no input, output, or write path changed. I did not exercise the preview, since this is not a page change.
The 1–4 s pause is
heapSize()frombun:jsc, not a garbage collection and not the egress sweep. I couldn't reproduce production's real 8 GiB heap, only a retained heap of millions of small objects.What I found
scripts/pause-rig.tsruns 420 egress servers with four connections each, a retained heap, and a 10 ms loop-lag probe. I ran it underbun --cpu-prof.heapSizeheld the thread for 47.9 s of 57 s. The sweep's own loop (egress.ts:1006) took 67 ms andduringtook 19 ms.src/perf/job_alloc.tscallsheapSize()twice inside everyduring(). In Bun 1.4.4 it isn't constant time; it costs about 17 ms per GiB of heap per call. Oneduring()takes 24 ms at 1 GiB, 123 ms at 4 GiB and 263 ms at 7.6 GiB. 420 timers each pay that every slice, and the stall watcher names the job that ran across the stall, soegress-idlegets the label.What changed
createTallynow spends at most 0.5% of wall time, plus a first 50 ms, reading the heap size. Slices past that budget still run, just uncounted. Thememory allocated by jobsline is therefore a sample on a big heap.docs/features/performance-watcher.md.test/job-alloc.test.tshas a new test for the budget: a 100 ms-per-read heap does about 100 reads, not 2,000. The existing tests there now pass a stopped clock so the real heap's read time can't spend the budget.What I didn't do
heapSize()inmemory_watch.ts.job-alloc.test.tspasses three runs in a row,tsc --noEmitis clean, andgate --fixis committed.src/apps/egress.tsstill has the per-serversetInterval, so #16108's timer removal isn't on this base.To try it:
bun --cpu-prof --cpu-prof-dir=$TMPDIR/prof scripts/pause-rig.ts 420 4 1000 30. The arguments are cells, connections per cell, retained MiB and seconds.🤖 Generated with Claude Code
Closes #16113