Stream: git-wasmtime

Topic: wasmtime / issue #15206 Copying collector: grow-vs-collec...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 18:59):

voces opened issue #15206:

The grow-vs-collect heuristic from #12942 collects instead of growing while the predicted live set is under half the GC heap's capacity. The copying collector only allocates into one semi-space, though, which is half the heap. So with the copying collector the heap grows only once the live set nearly fills a semi-space, and every collection copies most of the semi-space to free a small remainder.

Numbers

Test case below: 200,000 linked structs kept live in a global for the whole run (6.4 MB), then 20,000,000 short-lived structs. Wasmtime main (31307410b), default config (copying collector), x86_64 Linux:

collections final heap live after last GC user CPU max RSS
main 337 16 MiB 76% of a semi-space 0.65 s 38 MB
main, -O gc-heap-initial-size=67108864 23 64 MiB 19% 0.09 s 88 MB
main + patch below 71 32 MiB 38% 0.17 s 54 MB

The last collection's trace on main reports GC heap utilization = 38.15%, but that is against the whole heap; the live set fills 76% of the semi-space that allocation can actually use.

Test Case

repro.wat:

(module
  (rec (type $cell (struct (field i32) (field (ref null $cell)))))
  (global $sum (export "sum") (mut i32) (i32.const 0))
  (global $keep (mut (ref null $cell)) (ref.null $cell))
  (func $main
    (local $i i32)
    (local $s i32)
    (local $t (ref null $cell))
    (local.set $i (i32.const 0))
    (loop $live
      (global.set $keep (struct.new $cell (local.get $i) (global.get $keep)))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br_if $live (i32.lt_u (local.get $i) (i32.const 200000))))
    (local.set $i (i32.const 0))
    (loop $churn
      (local.set $t (struct.new $cell (local.get $i) (ref.null $cell)))
      (local.set $s (i32.add (local.get $s) (struct.get $cell 0 (local.get $t))))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br_if $churn (i32.lt_u (local.get $i) (i32.const 20000000))))
    (global.set $sum (local.get $s)))
  (start $main))

Steps to Reproduce

WASMTIME_LOG=wasmtime=trace wasmtime run -W gc=y,function-references=y repro.wat 2>&1 \
  | grep -c "Begin GC"
/usr/bin/time -f "%U s %M KB" wasmtime run -W gc=y,function-references=y repro.wat

Expected Results

The heap grows once the live set passes half of what the collector can allocate into between collections, as it does for the DRC collector.

Actual Results

The heap stays at 16 MiB and collects 337 times.

Cause

should_collect_first returns predicted_usage < gc_heap_capacity / 2, where gc_heap_capacity is the whole heap. For the copying collector, allocated_bytes() counts only the active semi-space, which can hold at most gc_heap_capacity / 2, so the comparison is true for nearly any live set that still leaves room for the allocation.

I have a patch that adds a GcHeap::allocatable_bytes() method (defaulting to the heap's capacity, overridden by the copying collector to return the active semi-space's size) and uses it at both grow-vs-collect sites in store/gc.rs, with a test. Happy to open a PR if this approach looks right.

Versions and Environment

Wasmtime version or commit: 31307410b (also reproduces on 49.0.0)

Operating system: Linux (WSL2)

Architecture: x86_64

Extra Info

Related: #13470 discusses this heuristic's throughput/RSS tradeoff for the DRC collector. This issue is independent of that tradeoff: it is the ratio being measured against capacity the copying collector cannot allocate into.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 20:58):

fitzgen closed issue #15206:

The grow-vs-collect heuristic from #12942 collects instead of growing while the predicted live set is under half the GC heap's capacity. The copying collector only allocates into one semi-space, though, which is half the heap. So with the copying collector the heap grows only once the live set nearly fills a semi-space, and every collection copies most of the semi-space to free a small remainder.

Numbers

Test case below: 200,000 linked structs kept live in a global for the whole run (6.4 MB), then 20,000,000 short-lived structs. Wasmtime main (31307410b), default config (copying collector), x86_64 Linux:

collections final heap live after last GC user CPU max RSS
main 337 16 MiB 76% of a semi-space 0.65 s 38 MB
main, -O gc-heap-initial-size=67108864 23 64 MiB 19% 0.09 s 88 MB
main + patch below 71 32 MiB 38% 0.17 s 54 MB

The last collection's trace on main reports GC heap utilization = 38.15%, but that is against the whole heap; the live set fills 76% of the semi-space that allocation can actually use.

Test Case

repro.wat:

(module
  (rec (type $cell (struct (field i32) (field (ref null $cell)))))
  (global $sum (export "sum") (mut i32) (i32.const 0))
  (global $keep (mut (ref null $cell)) (ref.null $cell))
  (func $main
    (local $i i32)
    (local $s i32)
    (local $t (ref null $cell))
    (local.set $i (i32.const 0))
    (loop $live
      (global.set $keep (struct.new $cell (local.get $i) (global.get $keep)))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br_if $live (i32.lt_u (local.get $i) (i32.const 200000))))
    (local.set $i (i32.const 0))
    (loop $churn
      (local.set $t (struct.new $cell (local.get $i) (ref.null $cell)))
      (local.set $s (i32.add (local.get $s) (struct.get $cell 0 (local.get $t))))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br_if $churn (i32.lt_u (local.get $i) (i32.const 20000000))))
    (global.set $sum (local.get $s)))
  (start $main))

Steps to Reproduce

WASMTIME_LOG=wasmtime=trace wasmtime run -W gc=y,function-references=y repro.wat 2>&1 \
  | grep -c "Begin GC"
/usr/bin/time -f "%U s %M KB" wasmtime run -W gc=y,function-references=y repro.wat

Expected Results

The heap grows once the live set passes half of what the collector can allocate into between collections, as it does for the DRC collector.

Actual Results

The heap stays at 16 MiB and collects 337 times.

Cause

should_collect_first returns predicted_usage < gc_heap_capacity / 2, where gc_heap_capacity is the whole heap. For the copying collector, allocated_bytes() counts only the active semi-space, which can hold at most gc_heap_capacity / 2, so the comparison is true for nearly any live set that still leaves room for the allocation.

I have a patch that adds a GcHeap::allocatable_bytes() method (defaulting to the heap's capacity, overridden by the copying collector to return the active semi-space's size) and uses it at both grow-vs-collect sites in store/gc.rs, with a test. Happy to open a PR if this approach looks right.

Versions and Environment

Wasmtime version or commit: 31307410b (also reproduces on 49.0.0)

Operating system: Linux (WSL2)

Architecture: x86_64

Extra Info

Related: #13470 discusses this heuristic's throughput/RSS tradeoff for the DRC collector. This issue is independent of that tradeoff: it is the ratio being measured against capacity the copying collector cannot allocate into.


Last updated: Nov 19 2026 at 20:05 UTC