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

@@ -98,7 +98,21 @@
9898 h->next = sp_str_heap;
9999 h->size = total;
100100 sp_str_heap = h;
101- sp_gc_bytes += total;
101+ /* Don't fold string-heap pressure into sp_gc_bytes (issue #99): the
102+ threshold heuristic in sp_gc_alloc is keyed on heap survivors, and
103+ the str-heap mark-sweep that runs alongside (sp_str_sweep,
104+ called from sp_gc_collect) doesn't add surviving strings back into
105+ sp_gc_bytes. Each string alloc increments sp_gc_bytes by its full
106+ size, but a sweep that reaps a string subtracts its size — and a
107+ surviving string's size isn't re-added on the way out of
108+ sp_gc_collect. Net effect on a workload that mixes heap allocs
109+ with frequent small string allocs (e.g. `puts <float>` in a tight
110+ loop): sp_gc_bytes drifts low after each collect, freed/before
111+ looks artificially large, the threshold-recompute branch in
112+ sp_gc_alloc takes the `sp_gc_bytes*4` path with a too-small base,
113+ and the GC starts firing on every allocation. Strings are still
114+ reaped via sp_str_sweep on every gc cycle, so dropping the
115+ accounting only removes the threshold noise. */
102116 char *body = (char *)(h + 1);
103117 body[0] = (char)0xfe;
104118 body[1 + len] = 0;
@@ -148,7 +162,8 @@
148162 pp = &h->next;
149163 } else {
150164 *pp = h->next;
151- sp_gc_bytes -= h->size;
165+ /* String allocs no longer fold into sp_gc_bytes (see
166+ sp_str_alloc); the matching subtract here drops too. */
152167 free(h);
153168 }
154169 }