Commit f853ee4b
f853ee4b2e1fMerged
Ori Pekelman <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
Infer typed array from Array.new(n, fill) for all fill types
infer_ivar_init_type and infer_constructor_type both returned int_array unconditionally for Array.new(n, val), regardless of the fill argument's type. The codegen path in compile_constructor_expr also fell through to sp_IntArray for any non-float, non-string fill.
Three places updated to mirror each other:
- infer_ivar_init_type: dispatches on infer_type(aargs[1])
→ float → float_array
→ string → str_array
→ symbol → sym_array
→ poly → poly_array (sets @needs_rb_value)
→ type_is_pointer(vt) → vt + _ptr_array
(sets @needs_ptr_array, @needs_gc — without this, pointer
elements are stored in a container with no GC scan and get
swept while still live)
→ otherwise int_array
- infer_constructor_type: same dispatch, used by
scan_writer_calls when refining ivar types and by infer_type
for local variables.
- compile_constructor_expr: emit sp_PtrArray with cast to void *
for pointer-typed fills, so Array.new(n, Foo.new) actually
constructs a typed PtrArray with the GC scanner attached.
test/bm_ivar_float_array.rb covers float, symbol, and object fills across class boundaries. All 103 tests pass; bootstrap closes (gen2.c == gen3.c).
Details
- Commit
- f853ee4b2e1f
- Parent
- b04c9ea874d0
spinel_codegen.rbmodifiedView file
@@ -2620,7 +2620,9 @@
26202620
rn = constructor_class_name(recv)26212621
if rn != ""26222622
if rn == "Array"2623-
# Check fill value type2623+
# Check fill value type. Pointer-type fills must produce a typed2624+
# PtrArray; falling through to int_array would leave the2625+
# elements unscanned by GC.26242626
args_id = @nd_arguments[nid]26252627
if args_id >= 026262628
aargs = get_args(args_id)@@ -2632,6 +2634,18 @@
26322634
if vt == "string"26332635
return "str_array"26342636
end2637+
if vt == "symbol"2638+
return "sym_array"2639+
end2640+
if vt == "poly"2641+
@needs_rb_value = 12642+
return "poly_array"2643+
end2644+
if type_is_pointer(vt) == 12645+
@needs_ptr_array = 12646+
@needs_gc = 12647+
return vt + "_ptr_array"2648+
end26352649
end26362650
end26372651
return "int_array"@@ -4384,7 +4398,9 @@
43844398
rname = constructor_class_name(r)43854399
if rname != ""43864400
if rname == "Array"4387-
# Check fill value type for Array.new(n, val) -- match infer_constructor_type4401+
# Check fill value type for Array.new(n, val).4402+
# Pointer-type fills must produce a typed PtrArray; falling4403+
# through to int_array would leave the elements unscanned by GC.43884404
args_id = @nd_arguments[nid]43894405
if args_id >= 043904406
aargs = get_args(args_id)@@ -4396,6 +4412,18 @@
43964412
if vt == "string"43974413
return "str_array"43984414
end4415+
if vt == "symbol"4416+
return "sym_array"4417+
end4418+
if vt == "poly"4419+
@needs_rb_value = 14420+
return "poly_array"4421+
end4422+
if type_is_pointer(vt) == 14423+
@needs_ptr_array = 14424+
@needs_gc = 14425+
return vt + "_ptr_array"4426+
end43994427
end44004428
end44014429
return "int_array"@@ -13498,6 +13526,17 @@
1349813526
emit(" { mrb_int _n = " + compile_expr(aargs.first) + "; const char *_v = " + compile_expr(aargs[1]) + "; for (mrb_int _i = 0; _i < _n; _i++) sp_StrArray_push(" + tmp + ", _v); }")1349913527
return tmp1350013528
end13529+
# Pointer-type fills (objects, other arrays) need a typed PtrArray13530+
# so the GC scans the elements. Without this they'd be pushed13531+
# into an int_array and silently swept.13532+
if type_is_pointer(vt) == 113533+
@needs_ptr_array = 113534+
@needs_gc = 113535+
tmp = new_temp13536+
emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")13537+
emit(" { mrb_int _n = " + compile_expr(aargs.first) + "; void *_v = (void *)(" + compile_expr(aargs[1]) + "); for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp + ", _v); }")13538+
return tmp13539+
end1350113540
@needs_int_array = 11350213541
tmp = new_temp1350313542
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")test/bm_ivar_float_array.rbmodifiedView file
Loading diff…