Commit 59477089

59477089ef6eMerged

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

Description

feat: PtrArray scans its elements during GC

sp_PtrArray previously allocated with scan_fn = NULL, so GC never traced into its element storage. Anything held only via a PtrArray could be collected mid-use — this was the blocker that aborted the parse_id_list cache attempt earlier in this branch.

Runtime changes (lib/sp_runtime.h, also duplicated in codegen's emit_ptr_array_runtime):

  • Add scan_elem function pointer to the struct.
  • Add sp_PtrArray_gc_scan that walks a->data[0..len] and invokes scan_elem on each non-null entry.
  • Add sp_PtrArray_new_scan(fn) to create a ptr_array that delegates element marking to fn (NULL means "leaf", skip scanning).
  • Default sp_PtrArray_new() now delegates to ...new_scan(sp_gc_mark), which is the right behaviour for every current spinel use of PtrArray (elements are always GC-allocated pointers).

No codegen change needed — all existing call sites keep using the zero-arg sp_PtrArray_new().

Bootstrap byte-identical (gen2.c == gen3.c). 74 tests, 55 benchmarks pass. Minor ~3s regression on bin2 self-compile from the extra indirection; unlocks parse_id_list caching as a follow-up.

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

Details

Commit
59477089ef6e
Parent
4e825eeb7a7c

lib/sp_runtime.hmodifiedView file

@@ -172,9 +172,11 @@
172172 static inline void sp_FloatArray_set(sp_FloatArray*a,mrb_int i,mrb_float v){if(i<0)i+=a->len;while(i>=a->cap){a->cap=a->cap*2+1;a->data=(mrb_float*)realloc(a->data,sizeof(mrb_float)*a->cap);}while(i>=a->len){a->data[a->len]=0.0;a->len++;}a->data[i]=v;}
173173
174174 /* ---- PtrArray: array of void* pointers ---- */
175-typedef struct{void**data;mrb_int len;mrb_int cap;}sp_PtrArray;
175+typedef struct{void**data;mrb_int len;mrb_int cap;void(*scan_elem)(void*);}sp_PtrArray;
176176 static void sp_PtrArray_fin(void*p){sp_PtrArray*a=(sp_PtrArray*)p;sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(void*)*a->cap;h->size-=sizeof(void*)*a->cap;free(a->data);}
177-static sp_PtrArray*sp_PtrArray_new(void){sp_PtrArray*a=(sp_PtrArray*)sp_gc_alloc(sizeof(sp_PtrArray),sp_PtrArray_fin,NULL);a->cap=16;a->data=(void**)malloc(sizeof(void*)*a->cap);a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}return a;}
177+static void sp_PtrArray_gc_scan(void*p){sp_PtrArray*a=(sp_PtrArray*)p;if(!a->scan_elem)return;for(mrb_int i=0;i<a->len;i++){if(a->data[i])a->scan_elem(a->data[i]);}}
178+static sp_PtrArray*sp_PtrArray_new_scan(void(*scan_elem)(void*)){sp_PtrArray*a=(sp_PtrArray*)sp_gc_alloc(sizeof(sp_PtrArray),sp_PtrArray_fin,scan_elem?sp_PtrArray_gc_scan:NULL);a->cap=16;a->data=(void**)malloc(sizeof(void*)*a->cap);a->len=0;a->scan_elem=scan_elem;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}return a;}
179+static sp_PtrArray*sp_PtrArray_new(void){return sp_PtrArray_new_scan(sp_gc_mark);}
178180 static inline void sp_PtrArray_push(sp_PtrArray*a,void*v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(void*)*a->cap;h->size-=sizeof(void*)*a->cap;a->cap=a->cap*2+1;a->data=(void**)realloc(a->data,sizeof(void*)*a->cap);h->size+=sizeof(void*)*a->cap;sp_gc_bytes+=sizeof(void*)*a->cap;}a->data[a->len++]=v;}
179181 static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}
180182 static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}