Commit 49b582e3

49b582e34fb3Merged

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

Description

perf: hoist strlen out of while loop conditions

When a while loop condition uses str.length (compiled to strlen()), compute it once before the loop and cache in a local variable. The cached value is also used for str.length references inside the loop body.

Before: while (i < (mrb_int)strlen(s)) { ... } // O(n) per iteration After: mrb_int _tN = (mrb_int)strlen(s); while (i < _tN) { ... } // O(1) per iteration

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

Details

Commit
49b582e34fb3
Parent
b28557ec9504

spinel_codegen.rbmodifiedView file

@@ -109,6 +109,7 @@
109109 @current_method_return = ""
110110 @in_main = 0
111111 @in_loop = 0
112+ @hoisted_strlen_var = ""
112113 @in_yield_method = 0
113114 @in_gc_scope = 0
114115
@@ -11317,6 +11318,9 @@
1131711318 def compile_string_method_expr(nid, mname, rc)
1131811319 @needs_string_helpers = 1
1131911320 if mname == "length"
11321+ if @hoisted_strlen_var != ""
11322+ return @hoisted_strlen_var
11323+ end
1132011324 return "(mrb_int)strlen(" + rc + ")"
1132111325 end
1132211326 if mname == "to_i"
@@ -13496,15 +13500,71 @@
1349613500 emit(" }")
1349713501 end
1349813502
13503+ # Check if while condition uses strlen and hoist if safe
13504+ def try_hoist_strlen(pred_nid)
13505+ # Pattern: i < str.length → CallNode(<), recv=i, arg=CallNode(length/size)
13506+ if @nd_type[pred_nid] != "CallNode"
13507+ return ""
13508+ end
13509+ op = @nd_name[pred_nid]
13510+ if op != "<" && op != "<=" && op != ">" && op != ">="
13511+ return ""
13512+ end
13513+ # Find the .length/.size call
13514+ len_nid = -1
13515+ args_id = @nd_arguments[pred_nid]
13516+ if args_id >= 0
13517+ a = get_args(args_id)
13518+ if a.length > 0
13519+ len_nid = a[0]
13520+ end
13521+ end
13522+ # For > or >=, the length call may be on the receiver side
13523+ if op == ">" || op == ">="
13524+ len_nid = @nd_receiver[pred_nid]
13525+ end
13526+ if len_nid < 0
13527+ return ""
13528+ end
13529+ if @nd_type[len_nid] != "CallNode"
13530+ return ""
13531+ end
13532+ mn = @nd_name[len_nid]
13533+ if mn != "length" && mn != "size"
13534+ return ""
13535+ end
13536+ recv = @nd_receiver[len_nid]
13537+ if recv < 0
13538+ return ""
13539+ end
13540+ rt = infer_type(recv)
13541+ if rt != "string"
13542+ return ""
13543+ end
13544+ # Hoist: emit len variable before the loop
13545+ tmp = new_temp
13546+ rc = compile_expr(recv)
13547+ emit(" mrb_int " + tmp + " = (mrb_int)strlen(" + rc + ");")
13548+ tmp
13549+ end
13550+
1349913551 def compile_while_stmt(nid)
1350013552 old = @in_loop
1350113553 @in_loop = 1
13554+ # Try to hoist strlen from condition
13555+ len_tmp = try_hoist_strlen(@nd_predicate[nid])
13556+ if len_tmp != ""
13557+ @hoisted_strlen_var = len_tmp
13558+ end
1350213559 cond = compile_expr(@nd_predicate[nid])
1350313560 emit(" while (" + cond + ") {")
1350413561 @indent = @indent + 1
1350513562 compile_stmts_body(@nd_body[nid])
1350613563 @indent = @indent - 1
1350713564 emit(" }")
13565+ if len_tmp != ""
13566+ @hoisted_strlen_var = ""
13567+ end
1350813568 @in_loop = old
1350913569 end
1351013570