Commit ce25ea1b
ce25ea1b00d8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: size-segregated GC free lists, fix global constant GC roots
GC allocator:
- Replace single free list with 32 size-bucketed free lists
- Dead objects go to the matching bucket during sweep (no free())
- Allocation checks bucket head for exact size match (O(1))
- No cap on free list size — all dead objects are reused
- Eliminates most calloc calls for same-size object allocations
- Removed arena allocator (sweep cost exceeded alloc savings)
GC correctness:
- Register SP_GC_ROOT for pointer-type global constants
- Fixes latent bug where constants (e.g., RAND = Rand.new) could be collected if GC triggers after their scope ends
binary-trees n=18: 2.44s → 1.96s (20% improvement). Total vs Ruby: 16.7s → 1.96s (8.5x faster).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ce25ea1b00d8
- Parent
- 1dc8b0f5255c
spinel_codegen.rbmodifiedView file
@@ -6531,11 +6531,13 @@
65316531
emit_raw("static void sp_gc_mark(void*obj){if(!obj)return;sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->marked)return;h->marked=1;if(h->scan){if(sp_gc_mark_stack&&sp_gc_mark_top<SP_GC_MARK_STACK_MAX){sp_gc_mark_stack[sp_gc_mark_top++]=obj;}else{h->scan(obj);}}}")65326532
emit_raw("static void sp_gc_mark_all(void){if(!sp_gc_mark_stack)sp_gc_mark_stack=(void**)malloc(sizeof(void*)*SP_GC_MARK_STACK_MAX);sp_gc_mark_top=0;for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}while(sp_gc_mark_top>0){void*obj=sp_gc_mark_stack[--sp_gc_mark_top];sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->scan)h->scan(obj);}}")65336533
emit_raw("static void sp_gc_cleanup(int*p){sp_gc_nroots=*p;}")6534-
emit_raw("static sp_gc_hdr*sp_gc_freelist=NULL;static int sp_gc_freelist_len=0;")6535-
emit_raw("#define SP_GC_FREELIST_MAX 4096")6536-
emit_raw("static void sp_gc_collect(void){sp_gc_mark_all();sp_gc_hdr**pp=&sp_gc_heap;sp_gc_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));if(sp_gc_freelist_len<SP_GC_FREELIST_MAX){h->next=sp_gc_freelist;sp_gc_freelist=h;sp_gc_freelist_len++;}else{free(h);}}else{h->marked=0;sp_gc_bytes+=h->size;pp=&h->next;}}}")6534+
# Size-segregated free lists (no arena overhead during sweep)6535+
emit_raw("#define SP_GC_NBUCKETS 32")6536+
emit_raw("static sp_gc_hdr*sp_gc_buckets[SP_GC_NBUCKETS];")6537+
emit_raw("static inline int sp_gc_bucket(size_t sz){int b=(int)(sz/16);return b<SP_GC_NBUCKETS?b:SP_GC_NBUCKETS-1;}")6538+
emit_raw("static void sp_gc_collect(void){sp_gc_mark_all();sp_gc_hdr**pp=&sp_gc_heap;sp_gc_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));int b=sp_gc_bucket(h->size);h->next=sp_gc_buckets[b];sp_gc_buckets[b]=h;}else{h->marked=0;sp_gc_bytes+=h->size;pp=&h->next;}}}")65376539
emit_raw("static size_t sp_gc_threshold_init=256*1024;")6538-
emit_raw("static 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=NULL;if(sp_gc_freelist&&sp_gc_freelist->size==need){h=sp_gc_freelist;sp_gc_freelist=h->next;sp_gc_freelist_len--;memset((char*)h+sizeof(sp_gc_hdr),0,sz);}if(!h){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);}")6540+
emit_raw("static 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;int b=sp_gc_bucket(need);sp_gc_hdr*h=NULL;if(sp_gc_buckets[b]&&sp_gc_buckets[b]->size==need){h=sp_gc_buckets[b];sp_gc_buckets[b]=h->next;memset((char*)h+sizeof(sp_gc_hdr),0,sz);}if(!h){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);}")65396541
emit_raw("")65406542
end65416543
@@ -9033,6 +9035,9 @@
90339035
while i < @const_names.length90349036
val = compile_expr(@const_expr_ids[i])90359037
emit(" cst_" + @const_names[i] + " = " + val + ";")9038+
if type_is_pointer(@const_types[i]) == 19039+
emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")9040+
end90369041
i = i + 190379042
end90389043