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 = 0111111
@in_loop = 0112+
@hoisted_strlen_var = ""112113
@in_yield_method = 0113114
@in_gc_scope = 0114115
@@ -11317,6 +11318,9 @@
1131711318
def compile_string_method_expr(nid, mname, rc)1131811319
@needs_string_helpers = 11131911320
if mname == "length"11321+
if @hoisted_strlen_var != ""11322+
return @hoisted_strlen_var11323+
end1132011324
return "(mrb_int)strlen(" + rc + ")"1132111325
end1132211326
if mname == "to_i"@@ -13496,15 +13500,71 @@
1349613500
emit(" }")1349713501
end1349813502
13503+
# Check if while condition uses strlen and hoist if safe13504+
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+
end13509+
op = @nd_name[pred_nid]13510+
if op != "<" && op != "<=" && op != ">" && op != ">="13511+
return ""13512+
end13513+
# Find the .length/.size call13514+
len_nid = -113515+
args_id = @nd_arguments[pred_nid]13516+
if args_id >= 013517+
a = get_args(args_id)13518+
if a.length > 013519+
len_nid = a[0]13520+
end13521+
end13522+
# For > or >=, the length call may be on the receiver side13523+
if op == ">" || op == ">="13524+
len_nid = @nd_receiver[pred_nid]13525+
end13526+
if len_nid < 013527+
return ""13528+
end13529+
if @nd_type[len_nid] != "CallNode"13530+
return ""13531+
end13532+
mn = @nd_name[len_nid]13533+
if mn != "length" && mn != "size"13534+
return ""13535+
end13536+
recv = @nd_receiver[len_nid]13537+
if recv < 013538+
return ""13539+
end13540+
rt = infer_type(recv)13541+
if rt != "string"13542+
return ""13543+
end13544+
# Hoist: emit len variable before the loop13545+
tmp = new_temp13546+
rc = compile_expr(recv)13547+
emit(" mrb_int " + tmp + " = (mrb_int)strlen(" + rc + ");")13548+
tmp13549+
end13550+
1349913551
def compile_while_stmt(nid)1350013552
old = @in_loop1350113553
@in_loop = 113554+
# Try to hoist strlen from condition13555+
len_tmp = try_hoist_strlen(@nd_predicate[nid])13556+
if len_tmp != ""13557+
@hoisted_strlen_var = len_tmp13558+
end1350213559
cond = compile_expr(@nd_predicate[nid])1350313560
emit(" while (" + cond + ") {")1350413561
@indent = @indent + 11350513562
compile_stmts_body(@nd_body[nid])1350613563
@indent = @indent - 11350713564
emit(" }")13565+
if len_tmp != ""13566+
@hoisted_strlen_var = ""13567+
end1350813568
@in_loop = old1350913569
end1351013570