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 barex[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
int bn = 0; const int *bb = bbody >= 0 ? nt_arr(nt, bbody, "body", &bn) : NULL; if (bn > 0 && bb) { TyKind et = infer_type(c, bb[bn - 1]); if (et != TY_UNKNOWN) return ty_array_of(et); } } /* a bare `Array.new` carries no element type; leave it UNKNOWN (like an empty `[]`) so the push-promotion pass can narrow it from `<<`/push. */ if (argc == 0 && blk < 0) return TY_UNKNOWN; return TY_POLY_ARRAY; } if (cn && !strcmp(cn, "Array")) return TY_POLY_ARRAY; /* Array.new / Array.new(n) */src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…