Commit 10da3e9a

10da3e9a08d1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

fix(analyze,codegen,runtime): body-usage param inference for Array and Hash iteration (Option C)

Implements Option C from the #544/#545 design analysis: extend #542's body-driven param widening with two new signal families chosen to avoid the optcarrot regression that the broader Enumerable surface tripped on.

Path A: Array body inference (#545, conservative)

infer_array_param_from_body walks each method body for Array-unique method calls on int/nil-defaulted params and widens to poly_array. The classifier (is_array_only_method) covers push / pop / shift / unshift / concat / compact / flatten / transpose. Critically EXCLUDES the overlap-with- Integer set: <<, &, |, *, +, -. The probe showed optcarrot's poke(data) style int params hit data << 8 and data & 0xff -- without exclusion they'd false-positive to poly_array and propagate through ivar inference, breaking optcarrot. The conservative list catches Sam's #545 repro shape (arr.push(42)) without touching optcarrot.

Runs ONCE post-fixpoint so call-site inference can pin the narrower variant (int_array / float_array / sym_array / obj__ptr_array) first. Only params still at int/nil after the fixpoint converges fall to the poly_array catch-all, which is the "no typed caller" surface Sam's roundhouse seeded with type_seeds.rb.

Path B: Hash iteration inference (#545 iteration shape, lifted into #542's pass)

param_used_as_hash? detects Hash-unique method calls (.keys / .values / .each_pair / .merge / .merge! / .has_key? / .key? / .fetch / .store / .delete / .transform_values / .transform_keys / .to_h). These methods don't exist on Array / String / Integer, so the signal is unambiguous.

infer_hash_param_from_body now consults this helper as a fallback when no literal-string/symbol-key access has already pinned a more-specific variant. Widens to str_poly_hash (CRuby's default key kind) -- the actual key kind isn't determinable from iteration alone, so this is the conservative default.

This catches Sam's roundhouse blog cases that #542 missed (SqliteAdapter#insert/update via attrs.keys then attrs[k] where k is computed; ActionDispatch::Flash#merge / Session#merge via other.each do |k, v|).

Call-site cast + runtime NULL-guards

compile_call_args_with_defaults's existing #542 hash-cast arm is extended to also fire when the param's type is poly_array. Untyped callers (uninit ivar, etc.) get (sp_PolyArray *)NULL cast at the call site.

PolyArray's runtime helpers gain matching NULL-guards: sp_PolyArray_length(NULL) -> 0, sp_PolyArray_get(NULL, ...) -> sp_box_nil(), sp_PolyArray_push(NULL, ...) -> no-op. sp_StrPolyHash_keys(NULL) -> empty StrArray was the one remaining unhardened helper for path B's blog cases.

Composition limitation (carried over from #542)

Mixed typed + untyped callers on the SAME narrow specialization (e.g. typed int_array caller + untyped caller of the same method) still trip at C compile time -- the typed caller pins the param to int_array, the untyped caller's int can't cast to int_array (only the broader is_hash_type/poly_array arms have call-site casts). Sam's seed for Router.match's table param fits the workaround pattern (1) "seed the untyped source upstream"; this commit doesn't change that balance. Adding cast + NULL-guards to every narrow array variant (int_array / str_array / float_array / sym_array / obj_*_ptr_array) is the natural follow-up but multiplies runtime helper surface.

Measurement results (with SP_INT_PARAM_PROBE=1 -- probe code itself is not committed):

  • optcarrot: 0 false-positive widenings ✓ (was 10 with the broader Enumerable classifier that included <<).
  • spinel self-bootstrap: 0 widenings, no perf regression.
  • Sam's roundhouse blog (archive): 4 of 4 Hash iteration candidates widened (SqliteAdapter insert/update, Flash/Session merge). Of Sam's 5 explicit type_seeds.rb lines, 4 retired by #542 + this commit, 1 remaining (Router.match's table) needs the narrow-array cast series to retire too.

Closes #544 Closes #545

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
10da3e9a08d1
Parent
437e9fc7ac3a

lib/sp_runtime.hmodifiedView file

@@ -1354,7 +1354,7 @@
13541354 static void sp_PolyArray_scan(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; for (mrb_int i = 0; i < a->len; i++) sp_mark_rbval(a->data[i]); }
13551355 static void sp_PolyArray_fin(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; free(a->data); }
13561356 static sp_PolyArray *sp_PolyArray_new(void) { sp_PolyArray *a = (sp_PolyArray *)sp_gc_alloc(sizeof(sp_PolyArray), sp_PolyArray_fin, sp_PolyArray_scan); a->cap = 16; a->data = (sp_RbVal *)malloc(sizeof(sp_RbVal) * a->cap); a->len = 0; { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } return a; }
1357-static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (a->len >= a->cap) { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; a->cap = a->cap * 2 + 1; a->data = (sp_RbVal *)realloc(a->data, sizeof(sp_RbVal) * a->cap); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } a->data[a->len++] = v; }
1357+static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (!a) return; if (a->len >= a->cap) { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; a->cap = a->cap * 2 + 1; a->data = (sp_RbVal *)realloc(a->data, sizeof(sp_RbVal) * a->cap); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } a->data[a->len++] = v; }
13581358 static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) {
13591359 /* Dispatch by recv cls_id: an IntArray / PolyArray / etc. boxed
13601360 into a poly slot still wants Array#<< (push), not Integer#<<
@@ -1384,8 +1384,8 @@
13841384 }
13851385 return sp_box_int(sp_poly_to_i(a) << sp_poly_to_i(b));
13861386 }
1387-static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }
1388-static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }
1387+static mrb_int sp_PolyArray_length(sp_PolyArray *a) { if (!a) return 0; return a->len; }
1388+static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (!a) return sp_box_nil(); if (i < 0) i += a->len; return a->data[i]; }
13891389 static void sp_PolyArray_set(sp_PolyArray *a, mrb_int i, sp_RbVal v) { if (i < 0) i += a->len; a->data[i] = v; }
13901390 static sp_PolyArray *sp_PolyArray_slice(sp_PolyArray *a, mrb_int start, mrb_int len) { if (start < 0) start += a->len; if (start < 0) start = 0; sp_PolyArray *b = sp_PolyArray_new(); if (start >= a->len || len <= 0) return b; if (start + len > a->len) len = a->len - start; for (mrb_int i = 0; i < len; i++) sp_PolyArray_push(b, a->data[start + i]); return b; }
13911391 static sp_PolyArray *sp_PolyArray_slice_bang(sp_PolyArray *a, mrb_int from, mrb_int n) {
@@ -1452,7 +1452,7 @@
14521452 static void sp_StrPolyHash_set(sp_StrPolyHash*h,const char*k,sp_RbVal v){if(h->len*2>=h->cap)sp_StrPolyHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->order[h->len]=k;h->len++;}
14531453 static mrb_bool sp_StrPolyHash_has_key(sp_StrPolyHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}
14541454 static mrb_int sp_StrPolyHash_length(sp_StrPolyHash*h){return h->len;}
1455-static sp_StrArray*sp_StrPolyHash_keys(sp_StrPolyHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}
1455+static sp_StrArray*sp_StrPolyHash_keys(sp_StrPolyHash*h){sp_StrArray*a=sp_StrArray_new();if(!h)return a;for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}
14561456 static sp_PolyArray*sp_StrPolyHash_values(sp_StrPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,sp_StrPolyHash_get(h,h->order[i]));return a;}
14571457 /* Hash#merge for str_poly_hash. Same shape as the
14581458 StrIntHash / SymPolyHash siblings -- copy recv's entries into a