Commit a5247103

a5247103840fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

perf: generalize loop length hoist to arrays and hashes

try_hoist_strlen previously only handled string.length in while conditions. Extend to int_array/str_array/float_array/ptr_array and all hash types (str_int, str_str, sym_int, sym_str).

Safety: body_mutates_var? scans the loop body for push/pop/<</etc on the hoisted variable. If any mutation is found, hoist is skipped to preserve semantics (length can change mid-loop).

Example: while i < arr.length # hoisted to local mrb_int _t = length(arr) s += arr[i]; i += 1 end

while i < arr.length # NOT hoisted (arr.push mutates length) arr.push(...); i += 1 end

length_c_expr helper centralizes per-type length expressions.

75/75 tests pass, bootstrap stable.

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

Details

Commit
a5247103840f
Parent
b8c7b236f234

spinel_codegen.rbmodifiedView file

@@ -13506,6 +13506,9 @@
1350613506 # Array methods
1350713507 if recv_type == "int_array" || recv_type == "sym_array"
1350813508 if mname == "length"
13509+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13510+ return @hoisted_strlen_var
13511+ end
1350913512 return "sp_IntArray_length(" + rc + ")"
1351013513 end
1351113514 if mname == "[]"
@@ -13704,6 +13707,9 @@
1370413707 # Float array methods
1370513708 if recv_type == "float_array"
1370613709 if mname == "length"
13710+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13711+ return @hoisted_strlen_var
13712+ end
1370713713 return "sp_FloatArray_length(" + rc + ")"
1370813714 end
1370913715 if mname == "[]"
@@ -13726,6 +13732,9 @@
1372613732 elem_type = ptr_array_elem_type(recv_type)
1372713733 ct = c_type(elem_type)
1372813734 if mname == "length" || mname == "size"
13735+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13736+ return @hoisted_strlen_var
13737+ end
1372913738 return "sp_PtrArray_length(" + rc + ")"
1373013739 end
1373113740 if mname == "[]"
@@ -13740,6 +13749,9 @@
1374013749 end
1374113750 if recv_type == "str_array"
1374213751 if mname == "length"
13752+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13753+ return @hoisted_strlen_var
13754+ end
1374313755 return "sp_StrArray_length(" + rc + ")"
1374413756 end
1374513757 if mname == "[]"
@@ -13862,6 +13874,9 @@
1386213874 return "sp_SymIntHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"
1386313875 end
1386413876 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
13877+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13878+ return @hoisted_strlen_var
13879+ end
1386513880 return "sp_SymIntHash_length(" + rc + ")"
1386613881 end
1386713882 if mname == "empty?"
@@ -13905,6 +13920,9 @@
1390513920 return "sp_SymStrHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"
1390613921 end
1390713922 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
13923+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13924+ return @hoisted_strlen_var
13925+ end
1390813926 return "sp_SymStrHash_length(" + rc + ")"
1390913927 end
1391013928 if mname == "empty?"
@@ -13934,6 +13952,9 @@
1393413952 return "sp_StrIntHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")"
1393513953 end
1393613954 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
13955+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
13956+ return @hoisted_strlen_var
13957+ end
1393713958 return "sp_StrIntHash_length(" + rc + ")"
1393813959 end
1393913960 if mname == "empty?"
@@ -14020,6 +14041,9 @@
1402014041 return "sp_StrStrHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")"
1402114042 end
1402214043 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
14044+ if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
14045+ return @hoisted_strlen_var
14046+ end
1402314047 return "sp_StrStrHash_length(" + rc + ")"
1402414048 end
1402514049 if mname == "empty?"
@@ -15684,9 +15708,141 @@
1568415708 emit(" }")
1568515709 end
1568615710
15687- # Check if while condition uses strlen and hoist if safe
15711+ # C expression for computing length of a value of the given type.
15712+ # Returns "" if the type doesn't have a hoist-friendly length op.
15713+ def length_c_expr(rt, rc)
15714+ if rt == "string"
15715+ return "(mrb_int)strlen(" + rc + ")"
15716+ end
15717+ if rt == "int_array" || rt == "sym_array"
15718+ return "sp_IntArray_length(" + rc + ")"
15719+ end
15720+ if rt == "str_array"
15721+ return "sp_StrArray_length(" + rc + ")"
15722+ end
15723+ if rt == "float_array"
15724+ return "sp_FloatArray_length(" + rc + ")"
15725+ end
15726+ if is_ptr_array_type(rt) == 1
15727+ return "sp_PtrArray_length(" + rc + ")"
15728+ end
15729+ if rt == "str_int_hash"
15730+ return "sp_StrIntHash_length(" + rc + ")"
15731+ end
15732+ if rt == "str_str_hash"
15733+ return "sp_StrStrHash_length(" + rc + ")"
15734+ end
15735+ if rt == "sym_int_hash"
15736+ return "sp_SymIntHash_length(" + rc + ")"
15737+ end
15738+ if rt == "sym_str_hash"
15739+ return "sp_SymStrHash_length(" + rc + ")"
15740+ end
15741+ ""
15742+ end
15743+
15744+ # Scan a while-body for any mutation of a local variable (by name).
15745+ # Returns 1 if any mutating method call is found on the receiver
15746+ # (push/pop/shift/unshift/<< / []= / delete / clear / insert /
15747+ # replace / concat). Used to block unsafe hoisting.
15748+ def body_mutates_var?(body_nid, vname)
15749+ if body_nid < 0
15750+ return 0
15751+ end
15752+ t = @nd_type[body_nid]
15753+ if t == "CallNode"
15754+ mn = @nd_name[body_nid]
15755+ recv = @nd_receiver[body_nid]
15756+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode" && @nd_name[recv] == vname
15757+ if mn == "push" || mn == "pop" || mn == "shift" || mn == "unshift" ||
15758+ mn == "<<" || mn == "[]=" || mn == "delete" || mn == "clear" ||
15759+ mn == "insert" || mn == "replace" || mn == "concat" ||
15760+ mn == "sort!" || mn == "reverse!" || mn == "compact!" || mn == "uniq!" ||
15761+ mn == "merge!" || mn == "store" || mn == "update" || mn == "fill"
15762+ return 1
15763+ end
15764+ end
15765+ end
15766+ if t == "LocalVariableWriteNode" && @nd_name[body_nid] == vname
15767+ return 1
15768+ end
15769+ # Recurse into children
15770+ if @nd_body[body_nid] >= 0
15771+ if body_mutates_var?(@nd_body[body_nid], vname) == 1
15772+ return 1
15773+ end
15774+ end
15775+ stmts = parse_id_list(@nd_stmts[body_nid])
15776+ k = 0
15777+ while k < stmts.length
15778+ if body_mutates_var?(stmts[k], vname) == 1
15779+ return 1
15780+ end
15781+ k = k + 1
15782+ end
15783+ if @nd_subsequent[body_nid] >= 0
15784+ if body_mutates_var?(@nd_subsequent[body_nid], vname) == 1
15785+ return 1
15786+ end
15787+ end
15788+ if @nd_receiver[body_nid] >= 0
15789+ if body_mutates_var?(@nd_receiver[body_nid], vname) == 1
15790+ return 1
15791+ end
15792+ end
15793+ args_id = @nd_arguments[body_nid]
15794+ if args_id >= 0
15795+ arr = get_args(args_id)
15796+ k = 0
15797+ while k < arr.length
15798+ if body_mutates_var?(arr[k], vname) == 1
15799+ return 1
15800+ end
15801+ k = k + 1
15802+ end
15803+ end
15804+ 0
15805+ end
15806+
15807+ # Return the local variable name on which .length/.size is called
15808+ # inside a comparison predicate (for mutation scanning). Empty if
15809+ # the predicate doesn't match the hoist pattern.
15810+ def hoist_receiver_var(pred_nid)
15811+ if @nd_type[pred_nid] != "CallNode"
15812+ return ""
15813+ end
15814+ op = @nd_name[pred_nid]
15815+ if op != "<" && op != "<=" && op != ">" && op != ">="
15816+ return ""
15817+ end
15818+ len_nid = -1
15819+ args_id = @nd_arguments[pred_nid]
15820+ if args_id >= 0
15821+ a = get_args(args_id)
15822+ if a.length > 0
15823+ len_nid = a[0]
15824+ end
15825+ end
15826+ if op == ">" || op == ">="
15827+ len_nid = @nd_receiver[pred_nid]
15828+ end
15829+ if len_nid < 0 || @nd_type[len_nid] != "CallNode"
15830+ return ""
15831+ end
15832+ mn = @nd_name[len_nid]
15833+ if mn != "length" && mn != "size"
15834+ return ""
15835+ end
15836+ recv = @nd_receiver[len_nid]
15837+ if recv < 0 || @nd_type[recv] != "LocalVariableReadNode"
15838+ return ""
15839+ end
15840+ @nd_name[recv]
15841+ end
15842+
15843+ # Check if while condition uses .length/.size and hoist if safe.
15844+ # Supports string, arrays, and hashes.
1568815845 def try_hoist_strlen(pred_nid)
15689- # Pattern: i < str.length → CallNode(<), recv=i, arg=CallNode(length/size)
1569015846 if @nd_type[pred_nid] != "CallNode"
1569115847 return ""
1569215848 end
@@ -15722,13 +15878,29 @@
1572215878 return ""
1572315879 end
1572415880 rt = infer_type(recv)
15725- if rt != "string"
15881+ # Must be a local variable so we can check for mutations in the body
15882+ if @nd_type[recv] != "LocalVariableReadNode"
15883+ # string literal or ivar: be conservative, only hoist string (already safe)
15884+ if rt != "string"
15885+ return ""
15886+ end
15887+ tmp = new_temp
15888+ rc = compile_expr(recv)
15889+ emit(" mrb_int " + tmp + " = (mrb_int)strlen(" + rc + ");")
15890+ @hoisted_strlen_recv = rc
15891+ return tmp
15892+ end
15893+ len_c = length_c_expr(rt, "")
15894+ if len_c == ""
1572615895 return ""
1572715896 end
15728- # Hoist: emit len variable before the loop
15897+ # Check that the loop body doesn't mutate this variable
15898+ vname = @nd_name[recv]
15899+ # (The pred_nid is the while predicate; body scan happens below via caller
15900+ # passing @while_body_nid. Here we only check if type is hoistable.)
1572915901 tmp = new_temp
1573015902 rc = compile_expr(recv)
15731- emit(" mrb_int " + tmp + " = (mrb_int)strlen(" + rc + ");")
15903+ emit(" mrb_int " + tmp + " = " + length_c_expr(rt, rc) + ";")
1573215904 @hoisted_strlen_recv = rc
1573315905 tmp
1573415906 end
@@ -15739,10 +15911,21 @@
1573915911 # Save outer hoist state to restore on exit (support nested loops)
1574015912 saved_var = @hoisted_strlen_var
1574115913 saved_recv = @hoisted_strlen_recv
15742- # Try to hoist strlen from condition
15743- len_tmp = try_hoist_strlen(@nd_predicate[nid])
15744- if len_tmp != ""
15745- @hoisted_strlen_var = len_tmp
15914+ # Try to hoist length from condition (string/array/hash). Skip if the
15915+ # loop body mutates the receiver variable (push/pop/<< etc.).
15916+ len_tmp = ""
15917+ can_hoist = 1
15918+ hoist_target = hoist_receiver_var(@nd_predicate[nid])
15919+ if hoist_target != ""
15920+ if body_mutates_var?(@nd_body[nid], hoist_target) == 1
15921+ can_hoist = 0
15922+ end
15923+ end
15924+ if can_hoist == 1
15925+ len_tmp = try_hoist_strlen(@nd_predicate[nid])
15926+ if len_tmp != ""
15927+ @hoisted_strlen_var = len_tmp
15928+ end
1574615929 end
1574715930 cond = compile_expr(@nd_predicate[nid])
1574815931 emit(" while (" + cond + ") {")