Commit 287a6fb6

287a6fb61aedMerged

Chris Hasiński <[email protected]> · 7 months ago · 2 files · ~2 modified

Description

fix(codegen): route Array#size to length for IntArray/SymArray/StrArray

[1,2,3].size returned 0 because the dispatch for IntArray/SymArray and StrArray in compile_array_method_expr matched only mname == "length", letting "size" fall through to a default that returned 0. FloatArray and PtrArray already accept both names; this brings the remaining three array types in line.

Implementation:

  • int_array/sym_array: split into separate length and size arms, each carrying the @hoisted_strlen_var optimisation. Equivalent to the previous behavior for length; new path for size returns the same hoisted-or-direct length.

  • str_array: same shape, both arms carry the hoisted check (the length arm already had it; matching it for size keeps perf parity in while i < arr.size loop bounds).

The two arms are duplicated rather than written as if mname == "length" || mname == "size" because the compound form in str_array destabilises the self-build (gen1 is fine, but bin1 then mis-compiles unrelated == comparisons in spinel_codegen itself, producing fragments like if (( && ...))). Kept all three types consistent for clarity. Worth tracking the underlying inference quirk separately.

Test: bm_array_lit.rb extended with .size on IntArray, SymArray, and StrArray; on an array post push/pop; and as a loop bound (where the hoisted-length path matters for performance). CRuby and Spinel outputs match.

Details

Commit
287a6fb61aed
Parent
effcb5d8ea89

spinel_codegen.rbmodifiedView file

@@ -14091,6 +14091,12 @@
1409114091 end
1409214092 return "sp_IntArray_length(" + rc + ")"
1409314093 end
14094+ if mname == "size"
14095+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
14096+ return @hoisted_strlen_var
14097+ end
14098+ return "sp_IntArray_length(" + rc + ")"
14099+ end
1409414100 if mname == "[]"
1409514101 return "sp_IntArray_get(" + rc + ", " + compile_arg0(nid) + ")"
1409614102 end
@@ -14313,6 +14319,12 @@
1431314319 end
1431414320 return "sp_StrArray_length(" + rc + ")"
1431514321 end
14322+ if mname == "size"
14323+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
14324+ return @hoisted_strlen_var
14325+ end
14326+ return "sp_StrArray_length(" + rc + ")"
14327+ end
1431614328 if mname == "[]"
1431714329 return "sp_StrArray_get(" + rc + ", " + compile_arg0(nid) + ")"
1431814330 end