Commit 1dc8b0f5
1dc8b0f5255cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: GC free list for object reuse (capped at 4096 entries)
During sweep, dead objects are added to a free list instead of immediately freed. On allocation, the free list head is checked for a size match before falling back to calloc.
Free list is capped at SP_GC_FREELIST_MAX (4096) entries to prevent unbounded memory growth. Excess entries are freed to OS.
binary-trees n=18: 2.70s → 2.55s (~6% improvement).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 1dc8b0f5255c
- Parent
- a0346691c038
spinel_codegen.rbmodifiedView file
@@ -6531,9 +6531,11 @@
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 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));free(h);}else{h->marked=0;sp_gc_bytes+=h->size;pp=&h->next;}}}")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;}}}")65356537
emit_raw("static size_t sp_gc_threshold_init=256*1024;")6536-
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;}}sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,sizeof(sp_gc_hdr)+sz);h->finalize=fin;h->scan=scn;h->size=sizeof(sp_gc_hdr)+sz;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=sizeof(sp_gc_hdr)+sz;return(char*)h+sizeof(sp_gc_hdr);}")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);}")65376539
emit_raw("")65386540
end65396541