Commit f0063046
f0063046556dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · ~5 modified
Description
feat(analyze,codegen,runtime): Array#<=> based max/min on *_ptr_array
Closes the last gap for the Enumerator-chain reproducer in #566:
<chain>.max on an int_array_ptr_array (e.g. an array of
[diff, idx] pairs produced by the each_cons.with_index.map
fusion) returns the lex-greatest inner array per Array#<=>.
- runtime: add
sp_IntArray_cmp(a, b)-- per-element compare, shorter wins ties (matches CRuby[1,2] <=> [1,2,3] == -1). NULL-safe like the surrounding helpers. - codegen compile_array_method_expr ptr_array branch: emit a
one-pass running-winner loop for
.max/.minwhen elem_type isint_arrayand the call has no block. Other ptr_array element shapes (str_array, etc.) stay on the unresolved-call path until they're needed. - analyze infer_method_name_type:
.max/.minon a ptr_array returns the element type so downstream[i]/.lengthetc. dispatch on the right shape.
Updates the #566 fixture (test/ptr_array_null_guards.rb) to
expect 3 -- the original report's CRuby reference value --
instead of the warned-fallback 0. The test still exercises
the NULL-safe sp_PtrArray_* helpers (regression coverage) and
now also exercises the end-to-end fused chain.
Closes #566.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f0063046556d
- Parent
- cd7f832d64fc
lib/sp_runtime.hmodifiedView file
static void sp_IntArray_unshift(sp_IntArray*a,mrb_int v){if(a->start>0){a->start--;a->data[a->start]=v;a->len++;}else{mrb_int e=a->len+1;if(e>a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*a->cap;h->size-=sizeof(mrb_int)*a->cap;a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}memmove(a->data+1,a->data,sizeof(mrb_int)*a->len);a->data[0]=v;a->len++;}}static const char*sp_IntArray_join(sp_IntArray*a,const char*sep){size_t sl=strlen(sep),cap=256;char*buf=(char*)malloc(cap);size_t len=0;for(mrb_int i=0;i<a->len;i++){if(i>0){if(len+sl>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,sep,sl);len+=sl;}char tmp[32];int n=snprintf(tmp,32,"%lld",(long long)a->data[a->start+i]);if(len+n>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,tmp,n);len+=n;}buf[len]=0;char*r=sp_str_alloc(len);memcpy(r,buf,len);free(buf);return r;}static mrb_bool sp_IntArray_eq(sp_IntArray*a,sp_IntArray*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]!=b->data[b->start+i])return FALSE;return TRUE;}/* Array#<=> for IntArray. Lexicographic: per-element compare, shorter array sorts before longer if all common elements match (matches CRuby `[1,2] <=> [1,2,3] == -1`). Used by `.max` / `.min` on `int_array_ptr_array` to dispatch the standard sort/compare protocol over arrays of arrays. NULL recv compares equal to NULL, lower than any non-NULL (matches the NULL-safe pattern other compare helpers use). */static mrb_int sp_IntArray_cmp(sp_IntArray*a,sp_IntArray*b){if(!a||!b)return a==b?0:(a?1:-1);mrb_int n=a->len<b->len?a->len:b->len;for(mrb_int i=0;i<n;i++){mrb_int av=a->data[a->start+i],bv=b->data[b->start+i];if(av<bv)return -1;if(av>bv)return 1;}if(a->len<b->len)return -1;if(a->len>b->len)return 1;return 0;}typedef struct{mrb_float*data;mrb_int len;mrb_int cap;}sp_FloatArray;static void sp_FloatArray_fin(void*p){sp_FloatArray*a=(sp_FloatArray*)p;sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*a->cap;h->size-=sizeof(mrb_float)*a->cap;free(a->data);}spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/ptr_array_null_guards.rbmodifiedView file
Loading diff…
test/ptr_array_null_guards.rb.expectedmodifiedView file
Loading diff…