Commit bf972ef7
bf972ef7da5bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 10 files · +4 added · ~6 modified
Description
Return value-level nil for sym/int_int_hash missing keys (#801 Step D)
Extends the str_int_hash work to the remaining int-valued hash variants: a missing-key read of a Symbol=>Integer or Integer=>Integer typed hash now returns Ruby nil at the value level (int?), not 0, including when stored in a local first.
Same proven pattern as str_int_hash: the only compiler-internal carrier is @cls_meth_idx_cache (int_int_hash, the composite-key method-index lookup), migrated to fetch under its has_key? guard; the instrumented inner loop confirms no sym_int_hash carriers. The [] read flips to int?, codegen emits sp_*IntHash_get_opt, _new seeds default_v = SP_INT_NIL (no-default miss is nil), and Hash#default / fetch are typed accordingly.
The default_v change exposed counting idioms that read via _get on a miss expecting 0: sp_IntArray_tally_int and sp_SymArray_tally now read through an explicit has_key? guard (mirroring sp_StrArray_tally), so a fresh tally still counts from 0.
Tests whose expectations encoded the old h[missing] == 0 behavior (hash_replace_variants, sym_int_hash_merge, int_array_tally) are updated to the CRuby-correct nil result. (Hash.new(N) with integer keys still promotes to a poly hash, a separate pre-existing limitation, so the int_int fixture covers literal hashes.)
Self-host fixpoint holds; 847 tests, 57 benchmarks, and optcarrot (checksum 59662) are green. int-array (#1180) is intentionally not flipped here; see the next step.
Refs #801
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- bf972ef7da5b
- Parent
- e1ee0aa0879d
lib/sp_runtime.hmodifiedView file
empty), with int-valued slots. Used by Array#tally on int arrays — see #865. */static void sp_IntIntHash_fin(void*p){sp_IntIntHash*h=(sp_IntIntHash*)p;free(h->keys);free(h->vals);free(h->order);free(h->used);}static sp_IntIntHash*sp_IntIntHash_new(void){sp_IntIntHash*h=(sp_IntIntHash*)sp_gc_alloc(sizeof(sp_IntIntHash),sp_IntIntHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;h->default_v=0;return h;}/* default_v is SP_INT_NIL for a hash with no explicit default, so a missing-key `[]` read surfaces Ruby nil (#801). Hash.new(N) sets it via _new_with_default. */static sp_IntIntHash*sp_IntIntHash_new(void){sp_IntIntHash*h=(sp_IntIntHash*)sp_gc_alloc(sizeof(sp_IntIntHash),sp_IntIntHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;h->default_v=SP_INT_NIL;return h;}static sp_IntIntHash*sp_IntIntHash_new_with_default(mrb_int d){sp_IntIntHash*h=sp_IntIntHash_new();h->default_v=d;return h;}static void sp_IntIntHash_grow(sp_IntIntHash*h){mrb_int oc=h->cap,ol=h->len;mrb_int*ok=h->keys;mrb_int*ov=h->vals;mrb_bool*ou=h->used;mrb_int*oo=h->order;h->cap*=2;h->mask=h->cap-1;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=ol;for(mrb_int i=0;i<oc;i++){if(!ou[i])continue;mrb_int k=ok[i];mrb_int v=ov[i];mrb_int di=_sp_istr_idx(h->mask,k);while(h->used[di])di=(di+1)&h->mask;h->used[di]=TRUE;h->keys[di]=k;h->vals[di]=v;}for(mrb_int i=0;i<ol;i++)h->order[i]=oo[i];free(ok);free(ov);free(ou);free(oo);}static void sp_IntIntHash_set(sp_IntIntHash*h,mrb_int k,mrb_int v){if(h->len*2>=h->cap)sp_IntIntHash_grow(h);mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->used[idx]=TRUE;h->keys[idx]=k;h->vals[idx]=v;h->order[h->len++]=k;}static mrb_int sp_IntIntHash_get(sp_IntIntHash*h,mrb_int k){if(!h)return 0;mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return h->vals[idx];idx=(idx+1)&h->mask;}return h->default_v;}/* Issue #801: maybe-missing public `[]` read. Returns default_v on a miss (SP_INT_NIL for a no-default hash = Ruby nil; the explicit default for Hash.new(N)). Proven-present reads keep using _get. */static mrb_int sp_IntIntHash_get_opt(sp_IntIntHash*h,mrb_int k){if(!h)return SP_INT_NIL;mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return h->vals[idx];idx=(idx+1)&h->mask;}return h->default_v;}static mrb_bool sp_IntIntHash_has_key(sp_IntIntHash*h,mrb_int k){mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}static mrb_int sp_IntIntHash_length(sp_IntIntHash*h){return h?h->len:0;}static sp_IntIntHash*sp_IntIntHash_dup(sp_IntIntHash*h){sp_IntIntHash*r=sp_IntIntHash_new();r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++)sp_IntIntHash_set(r,h->order[i],sp_IntIntHash_get(h,h->order[i]));return r;}/* Array#tally on int_array. CRuby returns an Integer-keyed Hash mapping each distinct element to its occurrence count. */static sp_IntIntHash*sp_IntArray_tally_int(sp_IntArray*a){sp_IntIntHash*h=sp_IntIntHash_new();if(!a)return h;for(mrb_int i=0;i<a->len;i++){mrb_int k=a->data[a->start+i];sp_IntIntHash_set(h,k,sp_IntIntHash_get(h,k)+1);}return h;}static sp_IntIntHash*sp_IntArray_tally_int(sp_IntArray*a){sp_IntIntHash*h=sp_IntIntHash_new();if(!a)return h;for(mrb_int i=0;i<a->len;i++){mrb_int k=a->data[a->start+i];mrb_int c=sp_IntIntHash_has_key(h,k)?sp_IntIntHash_get(h,k):0;sp_IntIntHash_set(h,k,c+1);}return h;}/* Reuse an existing StrArray for split, avoiding GC alloc. Clears a->len and refills. Substring strings are still malloc'd. */spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/hash_replace_variants.rb.expectedmodifiedView file
Loading diff…
test/int_array_tally.rb.expectedmodifiedView file
Loading diff…
test/int_int_hash_missing_key_nil.rbaddedView file
Loading diff…
test/int_int_hash_missing_key_nil.rb.expectedaddedView file
Loading diff…
test/sym_int_hash_merge.rb.expectedmodifiedView file
Loading diff…
test/sym_int_hash_missing_key_nil.rbaddedView file
Loading diff…
test/sym_int_hash_missing_key_nil.rb.expectedaddedView file
Loading diff…