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
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;}/* ---- PtrArray: array of void* pointers ---- */typedef struct{void**data;mrb_int len;mrb_int cap;}sp_PtrArray;typedef struct{void**data;mrb_int len;mrb_int cap;void(*scan_elem)(void*);}sp_PtrArray;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);}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;}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]);}}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;}static sp_PtrArray*sp_PtrArray_new(void){return sp_PtrArray_new_scan(sp_gc_mark);}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;}static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}spinel_codegen.rbmodifiedView file
Loading diff…