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 / .min when elem_type is int_array and 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 / .min on a ptr_array returns the element type so downstream [i] / .length etc. 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

@@ -662,6 +662,14 @@
662662 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++;}}
663663 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;}
664664 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;}
665+/* Array#<=> for IntArray. Lexicographic: per-element compare,
666+ shorter array sorts before longer if all common elements match
667+ (matches CRuby `[1,2] <=> [1,2,3] == -1`). Used by `.max` /
668+ `.min` on `int_array_ptr_array` to dispatch the standard
669+ sort/compare protocol over arrays of arrays. NULL recv compares
670+ equal to NULL, lower than any non-NULL (matches the NULL-safe
671+ pattern other compare helpers use). */
672+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;}
665673
666674 typedef struct{mrb_float*data;mrb_int len;mrb_int cap;}sp_FloatArray;
667675 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);}