Skip to content

Keep generational collection costs off empty capacity - #21

Open
Qard wants to merge 4 commits into
mainfrom
perf/collect-capacity
Open

Qard wants to merge 4 commits into
mainfrom
perf/collect-capacity

Conversation

@Qard

@Qard Qard commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Two generational collector costs scaled with heap capacity instead of live data. This PR removes both. Report: docs/results/collect-capacity-v1.md.

Changes

  • Forced major with a non-empty nursery. The compactor used to cover the whole span up to old capacity plus the young top, including the untouched gap above the old fill. Every bitmap pass ran over all of it: 4.0 M words at 256 MiB. Now the nursery is evacuated first (an ordinary minor collection), then only the packed old prefix is compacted. A nursery whose survivors cannot fit the old prefix still compacts in place. The pages released afterwards are unchanged.
  • Bitmap clears. The nursery start bits (cleared every minor), the compactor's live bits and the dirty table now clear with set from a permanent zero block instead of the element-by-element fill.
  • verify-heap-tools. It now breaks in evacuateNursery, where both tops are still readable. The old if (minor) line is gone, and in collect the tops are register-only at the new test.

Results (macOS arm64)

Forced major, nursery half full (32 KB live):

Nursery Before After
4 MiB 0.63 ms 0.16 ms
32 MiB 4.05 ms 0.50 ms
256 MiB 31.5 ms 2.35 ms

Frozen v7 (15 paired rounds), neutral to better:

  • cycles 0.9908 (quartiles 0.984–1.009);
  • wall 0.986;
  • instructions 0.9960, lower in all 15 rounds;
  • RSS unchanged.

Allocating feature kernels (15 rotated rounds), cycles:

Kernel Cycles
serialization −1.9%
closures −1.6%
pipelines −0.8%
strings −0.4%
records +0.7%
collections +2.3%

Collections. The kernel runs only minor collections. The profile puts its allocator and collector time at a median 15 ms per run on main and 8 ms with the fix. Instructions fall 0.4%, but cycles rise in unchanged mutator code, which moved by exactly one 4 KiB page because the runtime grew by one page. That is consistent with a placement effect, but not proven. The stacked #22 brings collections to about 1% below main.

Gates

  • Collector native fixtures: 16/16 under LLDB, covering the copying collector, the generational collector and its parallel paths.
  • verify-heap-tools: passes.
  • Self-build: native compiler subset check passes; stage2 == stage3.
  • CI: all three platforms green on the stacked head that contains this code (dispatch run 45296580121).
  • Main: merged after Queue shared gate locks in fair FIFO order #19.

The typed fill codegen improvement is left to the codegen stream (#17).

🤖 Generated with Claude Code

Qard and others added 4 commits November 24, 2026 06:13
Two collector costs scaled with heap capacity rather than with live data.

A major collection while the nursery held objects compacted the whole span
up to oldCapacity + youngTop, including the untouched gap above the old fill.
Its live-bit clear, offset pass, forwarding and packing all ran over every
bitmap word. Such a major now evacuates the nursery first (an ordinary minor
collection) and compacts only the packed old prefix. A nursery whose
survivors cannot fit still compacts in place. The pages released afterwards
are unchanged.

Bitmap clears used fill, which stores element by element. The nursery's
start bits (cleared every minor), the compactor's live bits and the dirty
table now clear with set from a permanent zero block, one memmove per
2 KiB.

Forced major with the nursery half full and 32 KB live, before -> after
(measured on the Worker stream's branch, which shares this code):
- 4 MiB nursery: 0.63 -> 0.16 ms;
- 32 MiB nursery: 4.05 -> 0.50 ms;
- 256 MiB nursery: 31.5 -> 2.35 ms.

Twelve nurseries of minor-heavy churn: -3.3% at 32 MiB and -2.4% at
256 MiB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The collector no longer has an 'if (minor) {' line: a collection now tests
whether the nursery fits the old prefix and evacuates it first. In collect
the young and old tops die at that evacuation call, so they stay in registers
the debug information does not describe, and LLDB cannot read them.
verify-heap-tools now breaks in evacuateNursery after its first call, where
both tops are homed and nothing has moved yet, as the old line was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-phase breakdown found two collector costs that scaled with heap
capacity. Fixing them takes a forced major with a half-full nursery from
31.5 to 2.35 ms at 256 MiB. Frozen v7 is neutral to better: cycles -0.9%,
instructions -0.4% in every round. The allocating kernels are neutral or
better except collections, at +2.3% cycles. Its allocator and collector
time falls (median 15 to 8 ms per run), so the extra cycles are in unchanged
mutator code that moved by one page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant