Commit b183d910

b183d910bd09Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: GC robustness - always root self/params, remove conservative scan

Three GC improvements:

  1. Instance/toplevel methods now always open a GC scope and root self and pointer parameters, even when no pointer-type local variables exist. Previously, methods like Basis.compute that call Vec_new (triggering GC) but have no pointer locals would leave self unrooted.

  2. Constructor parameters are now GC-rooted. Ray.new(org, dir) would not root org/dir during gc_alloc, allowing GC to collect them.

  3. Remove conservative stack scanner (sp_gc_mark_stack). The O(heap*stack) scan caused false retention (keeping dead objects alive because stale stack values matched heap pointers), which prevented threshold growth and led to excessive GC cycles. All GC roots are now explicit via SP_GC_ROOT, making collection precise and predictable.

Also improves GC threshold adaptation: when collection frees less than 25% of memory, threshold doubles the pre-collection size to reduce GC frequency under high allocation pressure.

ao_render still crashes due to remaining unrooted temporaries in deeply nested expression chains. Other 58 tests and bootstrap pass.

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

Details

Commit
b183d910bd09
Parent
4f544d489843

spinel_codegen.rbmodifiedView file

@@ -6038,13 +6038,12 @@
60386038 emit_raw("#define SP_GC_SAVE() int __attribute__((cleanup(sp_gc_cleanup))) _gc_saved = sp_gc_nroots")
60396039 emit_raw("#define SP_GC_ROOT(v) do{if(sp_gc_nroots<SP_GC_STACK_MAX)sp_gc_roots[sp_gc_nroots++]=(void**)&(v);}while(0)")
60406040 emit_raw("#define SP_GC_RESTORE() sp_gc_nroots = _gc_saved")
6041- emit_raw("static char *sp_gc_stack_bottom = NULL;")
6041+ # stack_bottom removed (no conservative scan)
60426042 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)h->scan(obj);}")
60436043 emit_raw("static void sp_gc_cleanup(int*p){sp_gc_nroots=*p;}")
6044- emit_raw("static void sp_gc_mark_stack(void){volatile char dummy;char*sp=(char*)&dummy;char*lo=sp,*hi=sp_gc_stack_bottom;if(lo>hi){char*t=lo;lo=hi;hi=t;}for(sp_gc_hdr*h=sp_gc_heap;h;h=h->next){void*obj=(char*)h+sizeof(sp_gc_hdr);void**p=(void**)lo;while((char*)p<hi){if(*p==obj){sp_gc_mark(obj);break;}p++;}}}")
6045- emit_raw("static void sp_gc_collect(void){for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}sp_gc_mark_stack();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;}}}")
6044+ emit_raw("static void sp_gc_collect(void){for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}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;}}}")
60466045 emit_raw("static size_t sp_gc_threshold_init=256*1024;")
6047- 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();if(sp_gc_bytes>sp_gc_threshold/2){sp_gc_threshold*=2;}else{size_t nt=sp_gc_bytes*4;if(nt<sp_gc_threshold_init)nt=sp_gc_threshold_init;if(nt>sp_gc_threshold)sp_gc_threshold=nt;}}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);}")
6046+ 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);}")
60486047 emit_raw("")
60496048 end
60506049
@@ -7009,6 +7008,28 @@
70097008 emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)sp_gc_alloc(sizeof(sp_" + cname + "), NULL, " + scan_fn + ");")
70107009 emit_raw(" SP_GC_ROOT(self);")
70117010
7011+ # Root pointer-type constructor parameters
7012+ if init_idx >= 0
7013+ all_params_str = @cls_meth_params[ci].split("|")
7014+ all_ptypes_str = @cls_meth_ptypes[ci].split("|")
7015+ if init_idx < all_params_str.length
7016+ cp_names = all_params_str[init_idx].split(",")
7017+ cp_types = "".split(",")
7018+ if init_idx < all_ptypes_str.length
7019+ cp_types = all_ptypes_str[init_idx].split(",")
7020+ end
7021+ cpi = 0
7022+ while cpi < cp_names.length
7023+ if cpi < cp_types.length
7024+ if type_is_pointer(cp_types[cpi]) == 1
7025+ emit_raw(" SP_GC_ROOT(lv_" + cp_names[cpi] + ");")
7026+ end
7027+ end
7028+ cpi = cpi + 1
7029+ end
7030+ end
7031+ end
7032+
70127033 init_ci = find_init_class(ci)
70137034 actual_init_idx = -1
70147035 if init_ci >= 0
@@ -7210,6 +7231,12 @@
72107231
72117232 if bid >= 0
72127233 declare_method_locals(bid, pnames)
7234+ if @in_gc_scope == 0
7235+ if @needs_gc == 1
7236+ emit(" SP_GC_SAVE();")
7237+ @in_gc_scope = 1
7238+ end
7239+ end
72137240 if @in_gc_scope == 1
72147241 emit(" SP_GC_ROOT(self);")
72157242 j = 0
@@ -7377,6 +7404,12 @@
73777404 bid = @meth_body_ids[mi]
73787405 if bid >= 0
73797406 declare_method_locals(bid, pnames)
7407+ if @in_gc_scope == 0
7408+ if @needs_gc == 1
7409+ emit(" SP_GC_SAVE();")
7410+ @in_gc_scope = 1
7411+ end
7412+ end
73807413 if @in_gc_scope == 1
73817414 j = 0
73827415 while j < pnames.length
@@ -7805,9 +7838,6 @@
78057838 emit_raw("static sp_Argv sp_argv;")
78067839 emit_raw("")
78077840 emit_raw("int main(int argc,char**argv){")
7808- if @needs_gc == 1
7809- emit_raw(" volatile char _sb; sp_gc_stack_bottom = (char*)&_sb;")
7810- end
78117841 emit_raw(" sp_argv.data=(const char**)(argv+1);sp_argv.len=argc-1;")
78127842 if @needs_regexp == 1
78137843 emit_raw(" sp_re_init();")