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
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]); }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); }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; }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; }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; }static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) { /* Dispatch by recv cls_id: an IntArray / PolyArray / etc. boxed into a poly slot still wants Array#<< (push), not Integer#<< } return sp_box_int(sp_poly_to_i(a) << sp_poly_to_i(b));}static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }static mrb_int sp_PolyArray_length(sp_PolyArray *a) { if (!a) return 0; return a->len; }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]; }static void sp_PolyArray_set(sp_PolyArray *a, mrb_int i, sp_RbVal v) { if (i < 0) i += a->len; a->data[i] = v; }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; }static sp_PolyArray *sp_PolyArray_slice_bang(sp_PolyArray *a, mrb_int from, mrb_int n) {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++;}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;}static mrb_int sp_StrPolyHash_length(sp_StrPolyHash*h){return h->len;}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;}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;}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;}/* Hash#merge for str_poly_hash. Same shape as the StrIntHash / SymPolyHash siblings -- copy recv's entries into aspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/param_body_array_hash_inference.rbaddedView file
Loading diff…
test/param_body_array_hash_inference.rb.expectedaddedView file
Loading diff…