Commit df80cf40

df80cf402cbdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(analyze,codegen): fuse each_cons(n).map { |pair| } chain

Blockless arr.each_cons(n) returns an Enumerator in CRuby. The common shape arr.each_cons(n).map { ... } materialises that Enumerator immediately. Fuse this two-step chain in codegen so no Enumerator object is allocated and the result drops straight into the accumulator.

  • analyze: each_cons(n) / each_slice(n) blockless on a typed array recv returns the corresponding <elem>_ptr_array. Lets the downstream .map's accumulator pick the right element type, and lets later .drop / .take chained on the result also infer correctly.
  • codegen: in compile_map_expr, detect a CallNode receiver named each_cons with no block and emit a single loop over the windows. Block-param shape:
    • |pair|: bind pair as a fresh per-iteration sub-array of the receiver's array kind (matches CRuby semantics for the yielded window).
    • |(a, b, ...)|: parse MultiTargetNode at requireds[0] and bind each inner name directly to a window slot, skipping the sub-array allocation.

The map accumulator type follows infer_type(body_last): scalar bodies collect into the matching typed-array; array bodies into sp_PtrArray.

Refs #566.

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

Details

Commit
df80cf402cbd
Parent
b1604e893600

spinel_analyze.rbmodifiedView file

@@ -4566,6 +4566,32 @@
45664566 end
45674567 return "int_array"
45684568 end
4569+ # `each_cons(n)` / `each_slice(n)` blockless: returns an array of
4570+ # sub-arrays of the same kind as the receiver. The chain-fusion
4571+ # paths in codegen (compile_map_expr / compile_each_block for
4572+ # ptr_array) consume this so a downstream `.map`, `.drop`, `.take`
4573+ # sees the correct ptr_array element type and doesn't fall back
4574+ # to the `cannot resolve call` emit-0 default.
4575+ if mname == "each_cons" || mname == "each_slice"
4576+ if recv >= 0 && @nd_block[nid] < 0
4577+ rt_ec = infer_type(recv)
4578+ if rt_ec == "int_array"
4579+ return "int_array_ptr_array"
4580+ end
4581+ if rt_ec == "str_array"
4582+ return "str_array_ptr_array"
4583+ end
4584+ if rt_ec == "float_array"
4585+ return "float_array_ptr_array"
4586+ end
4587+ if rt_ec == "sym_array"
4588+ return "sym_array_ptr_array"
4589+ end
4590+ if rt_ec == "poly_array"
4591+ return "poly_array_ptr_array"
4592+ end
4593+ end
4594+ end
45694595 if mname == "sort"
45704596 if recv >= 0
45714597 return infer_type(recv)