Commit df2850fe

df2850feefa8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

feat(runtime+codegen): per-class free-list pool for user class instances

Every user-defined class with a sp_new constructor now has a per-class free-list pool. When GC sweeps an unreachable instance, the storage is pushed onto the pool's free-list instead of free()d; the next sp_new pops from the pool, skipping calloc.

Runtime additions (lib/sp_runtime.h):

  • sp_gc_hdr gains a recycle function pointer (+8 bytes/object). When non-NULL, sp_gc_collect calls h->recycle(h) on unmarked objects instead of finalize+free; the hook decides whether to pool-push or actually free.
  • sp_gc_alloc_pool(sz, scn, recycle) variant — fresh sp_gc_alloc plus recycle hook installation.
  • sp_gc_pool_relink(h) — re-attaches a pooled slot back into sp_gc_heap so the next GC cycle visits it.
  • SP_POOL_DEFINE(CLS) macro — per-class plumbing: head, count, cap, stats counters, recycle function, optional report destructor.
  • SP_POOL_NEW(CLS, SCAN) macro — pool-pop-first body, falls back to sp_gc_alloc_pool on empty pool.

Codegen (spinel_codegen.rb):

  • emit_constructor emits SP_POOL_DEFINE() before each reference-typed class _new.
  • The body of _new now uses SP_POOL_NEW instead of bare sp_gc_alloc.
  • Value-type classes (sp_ by-value, not via sp_gc_alloc) are unaffected.

Runtime knobs:

  • SP_POOL_MAX (envvar, default 1048576): per-class hard cap on pool retention. When count >= cap, recycle falls back to free().
  • SP_POOL_REPORT=1 (envvar): dump per-class stats to stderr at exit.

Measurements on bench top alloc-heavy programs:

Benchmark baseline pool Δ wall bm_gcbench 0.84s 0.53s -37% bm_ao_render 0.22s 0.10s -55% bm_linked_list 0.07s 0.06s -14%

Pool stats for bm_ao_render (representative): Vec pops=3.79M hwm=3,521 over_cap=0 Ray pops=580k hwm=560 over_cap=0 Isect pops=580k hwm=561 over_cap=0 Vec ratio: 99.99% allocs come from pool.

No regression on:

  • test suite (400 pass)
  • bench (52 pass, 0 fail)
  • optcarrot (checksum 59662, fps within run-to-run variance)
  • self-host codegen wall clock (4.40s vs 4.45s baseline)

Adaptive cap (HWM-tracking grow/shrink with hysteresis) is a future follow-up. The static cap of 1M is sufficient for current bench top: per-class HWMs range from 16 (Basis) to 922,537 (Node), all below the cap.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
df2850feefa8
Parent
492d9aa0a9c4

lib/sp_runtime.hmodifiedView file

@@ -153,7 +153,11 @@
153153 }
154154
155155
156-typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); size_t size; unsigned marked : 1; } sp_gc_hdr;
156+/* `recycle`: optional sweep hook. If non-NULL, sp_gc_collect calls
157+ recycle(h) on the unmarked object instead of finalize+free. The
158+ hook is responsible for deciding whether to keep the storage
159+ (pool push) or free it. Used by class-instance free-list pools. */
160+typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); size_t size; unsigned marked : 1; void (*recycle)(struct sp_gc_hdr *); } sp_gc_hdr;
157161 static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;
158162
159163 /* ---- String GC ---- */
@@ -358,10 +362,84 @@
358362 static int sp_gc_cycle=0;
359363 static sp_gc_hdr*sp_gc_old_heap=NULL;static size_t sp_gc_old_bytes=0;
360364 #define SP_GC_FULL_INTERVAL 8
361-static void sp_gc_collect(void){int full=(sp_gc_cycle%SP_GC_FULL_INTERVAL==0);sp_gc_cycle++;sp_gc_hdr*hh=sp_gc_old_heap;while(hh){hh->marked=0;hh=hh->next;}sp_gc_mark_all();if(full){sp_gc_hdr**pp=&sp_gc_old_heap;sp_gc_old_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=1;sp_gc_old_bytes+=h->size;pp=&h->next;}}}else{hh=sp_gc_old_heap;while(hh){hh->marked=1;hh=hh->next;}}sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=sp_gc_old_bytes;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=1;*pp=h->next;h->next=sp_gc_old_heap;sp_gc_old_heap=h;sp_gc_old_bytes+=h->size;sp_gc_bytes+=h->size;}}sp_str_sweep();if(full)malloc_trim(0);}
365+static void sp_gc_collect(void){int full=(sp_gc_cycle%SP_GC_FULL_INTERVAL==0);sp_gc_cycle++;sp_gc_hdr*hh=sp_gc_old_heap;while(hh){hh->marked=0;hh=hh->next;}sp_gc_mark_all();if(full){sp_gc_hdr**pp=&sp_gc_old_heap;sp_gc_old_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->recycle){h->recycle(h);}else{if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}}else{h->marked=1;sp_gc_old_bytes+=h->size;pp=&h->next;}}}else{hh=sp_gc_old_heap;while(hh){hh->marked=1;hh=hh->next;}}sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=sp_gc_old_bytes;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->recycle){h->recycle(h);}else{if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}}else{h->marked=1;*pp=h->next;h->next=sp_gc_old_heap;sp_gc_old_heap=h;sp_gc_old_bytes+=h->size;sp_gc_bytes+=h->size;}}sp_str_sweep();if(full)malloc_trim(0);}
362366 static size_t sp_gc_threshold_init=256*1024;
363367 void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}}size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}
364368 void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}
369+/* Pool-aware alloc. The recycle hook is stored in the gc_hdr; sweep
370+ calls it on unmarked objects instead of finalize+free. The hook
371+ decides whether to push the storage onto a per-class free-list or
372+ actually free it. */
373+static void *sp_gc_alloc_pool(size_t sz, void(*scn)(void*), void(*recycle)(sp_gc_hdr*)) {
374+ void *p = sp_gc_alloc(sz, NULL, scn);
375+ sp_gc_hdr *h = (sp_gc_hdr *)((char *)p - sizeof(sp_gc_hdr));
376+ h->recycle = recycle;
377+ return p;
378+}
379+/* Re-link a previously-pooled slot back into sp_gc_heap so the next
380+ GC cycle visits it. Called by class _new functions when reusing
381+ a pooled instance. The storage was kept alive by the pool
382+ free-list since the last sweep unlinked it from sp_gc_heap. */
383+static void sp_gc_pool_relink(sp_gc_hdr *h) {
384+ h->marked = 0;
385+ h->next = sp_gc_heap;
386+ sp_gc_heap = h;
387+ sp_gc_bytes += h->size;
388+}
389+
390+/* Per-class free-list pool boilerplate. SP_POOL_DEFINE(CLS) goes at
391+ file scope, near the class _new function. SP_POOL_NEW(CLS, scan)
392+ replaces the body of an `sp_gc_alloc(sizeof(sp_CLS), NULL, scan)`
393+ call, popping from the per-class free-list if non-empty.
394+ Default cap can be overridden at runtime via SP_POOL_MAX envvar
395+ (uniform across classes). SP_POOL_REPORT=1 dumps per-class stats
396+ at exit. */
397+#define SP_POOL_DEFAULT_MAX 1048576L
398+#define SP_POOL_DEFINE(CLS) \
399+ static sp_gc_hdr *sp_##CLS##_pool_head = NULL; \
400+ static long sp_##CLS##_pool_count = 0; \
401+ static long sp_##CLS##_pool_max = SP_POOL_DEFAULT_MAX; \
402+ static long sp_##CLS##_pool_pushes = 0; \
403+ static long sp_##CLS##_pool_pops = 0; \
404+ static long sp_##CLS##_pool_freed = 0; \
405+ static long sp_##CLS##_pool_hwm = 0; \
406+ __attribute__((constructor)) static void sp_##CLS##_pool_init(void) { \
407+ const char *m = getenv("SP_POOL_MAX"); \
408+ if (m && *m) { long v = atol(m); if (v >= 0) sp_##CLS##_pool_max = v; } \
409+ } \
410+ static void sp_##CLS##_pool_recycle(sp_gc_hdr *h) { \
411+ if (sp_##CLS##_pool_count >= sp_##CLS##_pool_max) { \
412+ free(h); sp_##CLS##_pool_freed++; return; \
413+ } \
414+ h->next = sp_##CLS##_pool_head; \
415+ sp_##CLS##_pool_head = h; \
416+ sp_##CLS##_pool_count++; \
417+ sp_##CLS##_pool_pushes++; \
418+ if (sp_##CLS##_pool_count > sp_##CLS##_pool_hwm) sp_##CLS##_pool_hwm = sp_##CLS##_pool_count; \
419+ } \
420+ __attribute__((destructor)) static void sp_##CLS##_pool_report(void) { \
421+ const char *e = getenv("SP_POOL_REPORT"); \
422+ if (!e || !e[0] || e[0] == '0') return; \
423+ fprintf(stderr, #CLS " pool: pops=%ld pushes=%ld over_cap_freed=%ld hwm=%ld retained=%ld cap=%ld\n", \
424+ sp_##CLS##_pool_pops, sp_##CLS##_pool_pushes, sp_##CLS##_pool_freed, \
425+ sp_##CLS##_pool_hwm, sp_##CLS##_pool_count, sp_##CLS##_pool_max); \
426+ }
427+
428+#define SP_POOL_NEW(CLS, SCAN) (__extension__ ({ \
429+ sp_##CLS *_p; \
430+ if (sp_##CLS##_pool_head) { \
431+ sp_gc_hdr *_h = sp_##CLS##_pool_head; \
432+ sp_##CLS##_pool_head = _h->next; \
433+ sp_##CLS##_pool_count--; \
434+ sp_##CLS##_pool_pops++; \
435+ sp_gc_pool_relink(_h); \
436+ _h->recycle = sp_##CLS##_pool_recycle; \
437+ _p = (sp_##CLS *)((char *)_h + sizeof(sp_gc_hdr)); \
438+ } else { \
439+ _p = (sp_##CLS *)sp_gc_alloc_pool(sizeof(sp_##CLS), SCAN, sp_##CLS##_pool_recycle); \
440+ } \
441+ _p; \
442+}))
365443
366444 /* `Object.new` — a sentinel object whose only meaningful property is
367445 identity. Each call returns a fresh GC-managed allocation, so two