Commit aee79e2b

aee79e2bad98Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified

Description

Let empty Array.new narrow to a typed array (fixes so_lists 83x)

A bare Array.new inferred as TY_POLY_ARRAY, so a local seeded from it and filled with ints stayed a PolyArray; PolyArray#shift is an O(n) memmove, so a shift-in-a-loop was O(n^2) (so_lists ran 83x slower than legacy, which keeps the list an IntArray with O(1) start-offset shift).

Infer a bare Array.new as TY_UNKNOWN (like []) so the push-promotion pass narrows it from its element usage, and construct it as the variable's resolved element type in emit_assign. Two cascades this exposed, both fixed:

  • a local that elsewhere takes an array-typed write (x = arr.sort_by{}) must not be hash-promoted by a bare x[i] read (mirror the ivar guard);
  • Array.new/[] are arrays for an is_a?/instance_of?(Array) fold even while their element type is UNKNOWN.

bm_so_lists 83x -> 1x vs legacy (li2/li3 are IntArrays, shift is O(1)). make test 845, make bench 57, optcarrot checksum 59662.

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

Details

Commit
aee79e2bad98
Parent
50f7f92849e7

src/analyze_infer.cmodifiedView file

@@ -405,6 +405,9 @@
405405 int bn = 0; const int *bb = bbody >= 0 ? nt_arr(nt, bbody, "body", &bn) : NULL;
406406 if (bn > 0 && bb) { TyKind et = infer_type(c, bb[bn - 1]); if (et != TY_UNKNOWN) return ty_array_of(et); }
407407 }
408+ /* a bare `Array.new` carries no element type; leave it UNKNOWN (like an
409+ empty `[]`) so the push-promotion pass can narrow it from `<<`/push. */
410+ if (argc == 0 && blk < 0) return TY_UNKNOWN;
408411 return TY_POLY_ARRAY;
409412 }
410413 if (cn && !strcmp(cn, "Array")) return TY_POLY_ARRAY; /* Array.new / Array.new(n) */