Commit ec0f7689
ec0f76891c9bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(runtime): drop string-heap accounting from sp_gc_bytes
A workload that mixes heap allocations with frequent small string
allocations (e.g. puts <float> in a tight loop alongside Mat-style
sp_gc_alloc calls) entered a pathological GC cadence after commit
4514466 ("Flatten Float#to_s and puts on float arrays"): the new
sp_float_to_s allocates 64 bytes per call, and each call counted
against sp_gc_bytes even though sp_gc_collect's reset path
(sp_gc_bytes = sp_gc_old_bytes;) doesn't add surviving strings
back. Net effect: post-collect sp_gc_bytes drifts artificially
low, the threshold-recompute branch in sp_gc_alloc reads
freed/before as artificially large, takes the
sp_gc_threshold = sp_gc_bytes*4 path with a too-small base, and
the GC starts firing on every allocation. Reported in #99 as a
1000x slowdown and OOM kill on a small Transformer LM.
Symmetric fix proposed by @rramsden: don't fold string-heap pressure
into sp_gc_bytes at all. The increment in sp_str_alloc and the
matching subtract in sp_str_sweep both go away. Strings continue
to be reaped — sp_str_sweep runs at the end of every
sp_gc_collect, gated on the GC mark walk that already handles
sp_mark_string — so dropping the accounting only removes the
threshold noise. The threshold heuristic now reflects only the GC
heap (where sp_gc_alloc does properly add and sp_gc_collect
properly re-account survivors).
Co-Authored-By: Richard Ramsden [email protected] Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Refs #99
Details
- Commit
- ec0f76891c9b
- Parent
- b0ba3d43fddb
lib/sp_runtime.hmodifiedView file
h->next = sp_str_heap; h->size = total; sp_str_heap = h; sp_gc_bytes += total; /* Don't fold string-heap pressure into sp_gc_bytes (issue #99): the threshold heuristic in sp_gc_alloc is keyed on heap survivors, and the str-heap mark-sweep that runs alongside (sp_str_sweep, called from sp_gc_collect) doesn't add surviving strings back into sp_gc_bytes. Each string alloc increments sp_gc_bytes by its full size, but a sweep that reaps a string subtracts its size — and a surviving string's size isn't re-added on the way out of sp_gc_collect. Net effect on a workload that mixes heap allocs with frequent small string allocs (e.g. `puts <float>` in a tight loop): sp_gc_bytes drifts low after each collect, freed/before looks artificially large, the threshold-recompute branch in sp_gc_alloc takes the `sp_gc_bytes*4` path with a too-small base, and the GC starts firing on every allocation. Strings are still reaped via sp_str_sweep on every gc cycle, so dropping the accounting only removes the threshold noise. */ char *body = (char *)(h + 1); body[0] = (char)0xfe; body[1 + len] = 0; pp = &h->next; } else { *pp = h->next; sp_gc_bytes -= h->size; /* String allocs no longer fold into sp_gc_bytes (see sp_str_alloc); the matching subtract here drops too. */ free(h); } }