Commit 3878fcc2
3878fcc26dfeMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: sticky mark bits with two-list generational GC
Implement simplified generational GC using sticky mark bits:
- Two heap lists: sp_gc_heap (young) and sp_gc_old_heap (old)
- Minor GC: only sweep young list, promote survivors to old
- Full GC (every 8 cycles): clear all marks, sweep both lists
- Minor GC skips marking already-marked old objects
binary-trees n=18: 1.88s → 1.77s (6% improvement) nbody n=5M: 0.88s → 0.34s (2.6x improvement!)
66 tests pass, bootstrap clean.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 3878fcc26dfe
- Parent
- be97c6b78d08
spinel_codegen.rbmodifiedView file
@@ -6535,7 +6535,10 @@
65356535
emit_raw("#define SP_GC_NBUCKETS 32")65366536
emit_raw("static sp_gc_hdr*sp_gc_buckets[SP_GC_NBUCKETS];")65376537
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;}}}")6538+
emit_raw("static int sp_gc_cycle=0;")6539+
emit_raw("static sp_gc_hdr*sp_gc_old_heap=NULL;static size_t sp_gc_old_bytes=0;")6540+
emit_raw("#define SP_GC_FULL_INTERVAL 8")6541+
emit_raw("static void sp_gc_collect(void){int full=(sp_gc_cycle%SP_GC_FULL_INTERVAL==0);sp_gc_cycle++;if(full){sp_gc_hdr*hh=sp_gc_old_heap;while(hh){hh->marked=0;hh=hh->next;}sp_gc_mark_all();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));int b=sp_gc_bucket(h->size);h->next=sp_gc_buckets[b];sp_gc_buckets[b]=h;}else{h->marked=1;sp_gc_old_bytes+=h->size;pp=&h->next;}}}else{sp_gc_mark_all();}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));int b=sp_gc_bucket(h->size);h->next=sp_gc_buckets[b];sp_gc_buckets[b]=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;}}}")65396542
emit_raw("static size_t sp_gc_threshold_init=256*1024;")65406543
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;}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);}")65416544
emit_raw("")