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 type
2623+ # Check fill value type. Pointer-type fills must produce a typed
2624+ # PtrArray; falling through to int_array would leave the
2625+ # elements unscanned by GC.
26242626 args_id = @nd_arguments[nid]
26252627 if args_id >= 0
26262628 aargs = get_args(args_id)
@@ -2632,6 +2634,18 @@
26322634 if vt == "string"
26332635 return "str_array"
26342636 end
2637+ if vt == "symbol"
2638+ return "sym_array"
2639+ end
2640+ if vt == "poly"
2641+ @needs_rb_value = 1
2642+ return "poly_array"
2643+ end
2644+ if type_is_pointer(vt) == 1
2645+ @needs_ptr_array = 1
2646+ @needs_gc = 1
2647+ return vt + "_ptr_array"
2648+ end
26352649 end
26362650 end
26372651 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_type
4401+ # Check fill value type for Array.new(n, val).
4402+ # Pointer-type fills must produce a typed PtrArray; falling
4403+ # through to int_array would leave the elements unscanned by GC.
43884404 args_id = @nd_arguments[nid]
43894405 if args_id >= 0
43904406 aargs = get_args(args_id)
@@ -4396,6 +4412,18 @@
43964412 if vt == "string"
43974413 return "str_array"
43984414 end
4415+ if vt == "symbol"
4416+ return "sym_array"
4417+ end
4418+ if vt == "poly"
4419+ @needs_rb_value = 1
4420+ return "poly_array"
4421+ end
4422+ if type_is_pointer(vt) == 1
4423+ @needs_ptr_array = 1
4424+ @needs_gc = 1
4425+ return vt + "_ptr_array"
4426+ end
43994427 end
44004428 end
44014429 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 tmp
1350013528 end
13529+ # Pointer-type fills (objects, other arrays) need a typed PtrArray
13530+ # so the GC scans the elements. Without this they'd be pushed
13531+ # into an int_array and silently swept.
13532+ if type_is_pointer(vt) == 1
13533+ @needs_ptr_array = 1
13534+ @needs_gc = 1
13535+ tmp = new_temp
13536+ 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 tmp
13539+ end
1350113540 @needs_int_array = 1
1350213541 tmp = new_temp
1350313542 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")