Commit d4b9d804
d4b9d8045c13Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf(runtime): inline buffer for sp_StrArray (small-array optimization)
Spinel allocates a 16-slot data buffer for every sp_StrArray, even empty ones — and empty/short StrArrays dominate (the "".split(",") idiom alone produces 225+ at startup, and class-metadata lists typically stay under 4 elements).
Embed the first 4 elements inline in the struct. New StrArrays point data at inline_data and skip the data malloc entirely. sp_StrArray_push detects the inline-to-heap transition the first time the array outgrows SP_STRARR_INLINE; sp_StrArray_fin only frees data when it isn't the inline buffer. sp_StrArray_replace gets the same inline-aware grow path.
Net memory for empty/small StrArrays: ~56 bytes vs ~152 prior (struct grows by 32 bytes, but the 128-byte data malloc disappears for short arrays). Slight overhead (~16 bytes unused inline slots) once an array escapes to heap.
Wall-clock on self-codegen: ~15.83s → ~14.15s, a further ~10.6% on top of -O3 -flto. Bootstrap fixed-point clean; 88 tests + 55 benches green.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d4b9d8045c13
- Parent
- 80b3499d8c13
lib/sp_runtime.hmodifiedView file
static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}typedef struct{const char**data;mrb_int len;mrb_int cap;}sp_StrArray;static void sp_StrArray_fin(void*p){sp_StrArray*a=(sp_StrArray*)p;sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(const char*)*a->cap;h->size-=sizeof(const char*)*a->cap;free(a->data);}/* Small-array optimization: keep the first SP_STRARR_INLINE elements * inside the struct so empty/short StrArrays skip the data malloc. * Common idiom "".split(",") and most class-metadata lists stay small. * data == inline_data is the discriminator for "still on inline storage". */#define SP_STRARR_INLINE 4typedef struct{const char**data;mrb_int len;mrb_int cap;const char*inline_data[SP_STRARR_INLINE];}sp_StrArray;static void sp_StrArray_fin(void*p){sp_StrArray*a=(sp_StrArray*)p;if(a->data!=a->inline_data){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(const char*)*a->cap;h->size-=sizeof(const char*)*a->cap;free(a->data);}}static void sp_StrArray_scan(void*p){sp_StrArray*a=(sp_StrArray*)p;for(mrb_int i=0;i<a->len;i++)sp_mark_string(a->data[i]);}static sp_StrArray*sp_StrArray_new(void){sp_StrArray*a=(sp_StrArray*)sp_gc_alloc(sizeof(sp_StrArray),sp_StrArray_fin,sp_StrArray_scan);a->cap=16;a->data=(const char**)malloc(sizeof(const char*)*a->cap);a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(const char*)*a->cap;sp_gc_bytes+=sizeof(const char*)*a->cap;}return a;}static inline void sp_StrArray_push(sp_StrArray*a,const char*v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(const char*)*a->cap;h->size-=sizeof(const char*)*a->cap;a->cap=a->cap*2+1;a->data=(const char**)realloc(a->data,sizeof(const char*)*a->cap);h->size+=sizeof(const char*)*a->cap;sp_gc_bytes+=sizeof(const char*)*a->cap;}a->data[a->len++]=v;}static void sp_StrArray_replace(sp_StrArray*dst,sp_StrArray*src){dst->len=0;if(src->len>dst->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)dst-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(const char*)*dst->cap;h->size-=sizeof(const char*)*dst->cap;void*nd=realloc(dst->data,sizeof(const char*)*src->len);if(!nd){perror("realloc");exit(1);}dst->data=(const char**)nd;dst->cap=src->len;h->size+=sizeof(const char*)*dst->cap;sp_gc_bytes+=sizeof(const char*)*dst->cap;}memcpy(dst->data,src->data,sizeof(const char*)*src->len);dst->len=src->len;}static sp_StrArray*sp_StrArray_new(void){sp_StrArray*a=(sp_StrArray*)sp_gc_alloc(sizeof(sp_StrArray),sp_StrArray_fin,sp_StrArray_scan);a->cap=SP_STRARR_INLINE;a->data=a->inline_data;a->len=0;return a;}static inline void sp_StrArray_push(sp_StrArray*a,const char*v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));mrb_int nc=a->cap*2+1;if(a->data==a->inline_data){const char**nd=(const char**)malloc(sizeof(const char*)*nc);memcpy(nd,a->data,sizeof(const char*)*a->len);a->data=nd;}else{sp_gc_bytes-=sizeof(const char*)*a->cap;h->size-=sizeof(const char*)*a->cap;a->data=(const char**)realloc(a->data,sizeof(const char*)*nc);}a->cap=nc;h->size+=sizeof(const char*)*a->cap;sp_gc_bytes+=sizeof(const char*)*a->cap;}a->data[a->len++]=v;}static void sp_StrArray_replace(sp_StrArray*dst,sp_StrArray*src){dst->len=0;if(src->len>dst->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)dst-sizeof(sp_gc_hdr));void*nd;if(dst->data==dst->inline_data){nd=malloc(sizeof(const char*)*src->len);if(!nd){perror("malloc");exit(1);}}else{sp_gc_bytes-=sizeof(const char*)*dst->cap;h->size-=sizeof(const char*)*dst->cap;nd=realloc(dst->data,sizeof(const char*)*src->len);if(!nd){perror("realloc");exit(1);}}dst->data=(const char**)nd;dst->cap=src->len;h->size+=sizeof(const char*)*dst->cap;sp_gc_bytes+=sizeof(const char*)*dst->cap;}memcpy(dst->data,src->data,sizeof(const char*)*src->len);dst->len=src->len;}static const char*sp_StrArray_pop(sp_StrArray*a){return a->data[--a->len];}static inline mrb_int sp_StrArray_length(sp_StrArray*a){return a->len;}static inline mrb_bool sp_StrArray_empty(sp_StrArray*a){return a->len==0;}