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
recyclefunction 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
}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;/* `recycle`: optional sweep hook. If non-NULL, sp_gc_collect calls recycle(h) on the unmarked object instead of finalize+free. The hook is responsible for deciding whether to keep the storage (pool push) or free it. Used by class-instance free-list pools. */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;static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;/* ---- String GC ---- */static int sp_gc_cycle=0;static sp_gc_hdr*sp_gc_old_heap=NULL;static size_t sp_gc_old_bytes=0;#define SP_GC_FULL_INTERVAL 8static 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);}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);}static size_t sp_gc_threshold_init=256*1024;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);}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);}/* Pool-aware alloc. The recycle hook is stored in the gc_hdr; sweep calls it on unmarked objects instead of finalize+free. The hook decides whether to push the storage onto a per-class free-list or actually free it. */static void *sp_gc_alloc_pool(size_t sz, void(*scn)(void*), void(*recycle)(sp_gc_hdr*)) { void *p = sp_gc_alloc(sz, NULL, scn); sp_gc_hdr *h = (sp_gc_hdr *)((char *)p - sizeof(sp_gc_hdr)); h->recycle = recycle; return p;}/* Re-link a previously-pooled slot back into sp_gc_heap so the next GC cycle visits it. Called by class _new functions when reusing a pooled instance. The storage was kept alive by the pool free-list since the last sweep unlinked it from sp_gc_heap. */static void sp_gc_pool_relink(sp_gc_hdr *h) { h->marked = 0; h->next = sp_gc_heap; sp_gc_heap = h; sp_gc_bytes += h->size;}/* Per-class free-list pool boilerplate. SP_POOL_DEFINE(CLS) goes at file scope, near the class _new function. SP_POOL_NEW(CLS, scan) replaces the body of an `sp_gc_alloc(sizeof(sp_CLS), NULL, scan)` call, popping from the per-class free-list if non-empty. Default cap can be overridden at runtime via SP_POOL_MAX envvar (uniform across classes). SP_POOL_REPORT=1 dumps per-class stats at exit. */#define SP_POOL_DEFAULT_MAX 1048576L#define SP_POOL_DEFINE(CLS) \ static sp_gc_hdr *sp_##CLS##_pool_head = NULL; \ static long sp_##CLS##_pool_count = 0; \ static long sp_##CLS##_pool_max = SP_POOL_DEFAULT_MAX; \ static long sp_##CLS##_pool_pushes = 0; \ static long sp_##CLS##_pool_pops = 0; \ static long sp_##CLS##_pool_freed = 0; \ static long sp_##CLS##_pool_hwm = 0; \ __attribute__((constructor)) static void sp_##CLS##_pool_init(void) { \ const char *m = getenv("SP_POOL_MAX"); \ if (m && *m) { long v = atol(m); if (v >= 0) sp_##CLS##_pool_max = v; } \ } \ static void sp_##CLS##_pool_recycle(sp_gc_hdr *h) { \ if (sp_##CLS##_pool_count >= sp_##CLS##_pool_max) { \ free(h); sp_##CLS##_pool_freed++; return; \ } \ h->next = sp_##CLS##_pool_head; \ sp_##CLS##_pool_head = h; \ sp_##CLS##_pool_count++; \ sp_##CLS##_pool_pushes++; \ if (sp_##CLS##_pool_count > sp_##CLS##_pool_hwm) sp_##CLS##_pool_hwm = sp_##CLS##_pool_count; \ } \ __attribute__((destructor)) static void sp_##CLS##_pool_report(void) { \ const char *e = getenv("SP_POOL_REPORT"); \ if (!e || !e[0] || e[0] == '0') return; \ fprintf(stderr, #CLS " pool: pops=%ld pushes=%ld over_cap_freed=%ld hwm=%ld retained=%ld cap=%ld\n", \ sp_##CLS##_pool_pops, sp_##CLS##_pool_pushes, sp_##CLS##_pool_freed, \ sp_##CLS##_pool_hwm, sp_##CLS##_pool_count, sp_##CLS##_pool_max); \ }#define SP_POOL_NEW(CLS, SCAN) (__extension__ ({ \ sp_##CLS *_p; \ if (sp_##CLS##_pool_head) { \ sp_gc_hdr *_h = sp_##CLS##_pool_head; \ sp_##CLS##_pool_head = _h->next; \ sp_##CLS##_pool_count--; \ sp_##CLS##_pool_pops++; \ sp_gc_pool_relink(_h); \ _h->recycle = sp_##CLS##_pool_recycle; \ _p = (sp_##CLS *)((char *)_h + sizeof(sp_gc_hdr)); \ } else { \ _p = (sp_##CLS *)sp_gc_alloc_pool(sizeof(sp_##CLS), SCAN, sp_##CLS##_pool_recycle); \ } \ _p; \}))/* `Object.new` — a sentinel object whose only meaningful property is identity. Each call returns a fresh GC-managed allocation, so twospinel_codegen.rbmodifiedView file
Loading diff…