Commit 1d90f0cc

1d90f0cc6ea8Merged

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

Description

perf: optimize str[i] == "c" to direct char comparison

When comparing a single character from string indexing (s[i]) with a one-character string literal, compile as direct C char array access instead of malloc+strcmp. Applies to ==, !=, <, >, <=, >=.

Before: strcmp(sp_str_sub_range(s, i, 1), "\n") == 0 (malloc per char) After: s[(mrb_int)i] == '\n' (zero allocation)

Template benchmark: 372ms → 227ms (1.6x improvement, 4.3x vs Ruby) Char-by-char scan: 2575ms → 2ms (1000x improvement, 72x vs Ruby)

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

Details

Commit
1d90f0cc6ea8
Parent
afcf936e27b8

spinel_codegen.rbmodifiedView file

@@ -11278,6 +11278,10 @@
1127811278 if mname == "<"
1127911279 lt = infer_type(recv)
1128011280 if lt == "string"
11281+ cc = try_char_cmp(nid, "<")
11282+ if cc != ""
11283+ return cc
11284+ end
1128111285 return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") < 0)"
1128211286 end
1128311287 return "(" + compile_expr(recv) + " < " + compile_arg0(nid) + ")"
@@ -11285,6 +11289,10 @@
1128511289 if mname == ">"
1128611290 lt = infer_type(recv)
1128711291 if lt == "string"
11292+ cc = try_char_cmp(nid, ">")
11293+ if cc != ""
11294+ return cc
11295+ end
1128811296 return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") > 0)"
1128911297 end
1129011298 if lt == "poly"
@@ -11296,6 +11304,10 @@
1129611304 if mname == "<="
1129711305 lt = infer_type(recv)
1129811306 if lt == "string"
11307+ cc = try_char_cmp(nid, "<=")
11308+ if cc != ""
11309+ return cc
11310+ end
1129911311 return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") <= 0)"
1130011312 end
1130111313 return "(" + compile_expr(recv) + " <= " + compile_arg0(nid) + ")"
@@ -11303,6 +11315,10 @@
1130311315 if mname == ">="
1130411316 lt = infer_type(recv)
1130511317 if lt == "string"
11318+ cc = try_char_cmp(nid, ">=")
11319+ if cc != ""
11320+ return cc
11321+ end
1130611322 return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") >= 0)"
1130711323 end
1130811324 return "(" + compile_expr(recv) + " >= " + compile_arg0(nid) + ")"
@@ -12706,6 +12722,52 @@
1270612722 tmp
1270712723 end
1270812724
12725+ # Try to compile str[i] <op> "c" as direct char comparison
12726+ # Returns "" if not applicable
12727+ def try_char_cmp(nid, c_op)
12728+ recv = @nd_receiver[nid]
12729+ args_id = @nd_arguments[nid]
12730+ if args_id < 0
12731+ return ""
12732+ end
12733+ a = get_args(args_id)
12734+ if a.length == 0
12735+ return ""
12736+ end
12737+ arg_id = a[0]
12738+ if @nd_type[arg_id] != "StringNode"
12739+ return ""
12740+ end
12741+ lit = @nd_content[arg_id]
12742+ if lit == ""
12743+ lit = @nd_unescaped[arg_id]
12744+ end
12745+ if lit.length != 1
12746+ return ""
12747+ end
12748+ if @nd_type[recv] != "CallNode" || @nd_name[recv] != "[]"
12749+ return ""
12750+ end
12751+ sr = @nd_receiver[recv]
12752+ if sr < 0 || infer_type(sr) != "string"
12753+ return ""
12754+ end
12755+ str_c = compile_expr(sr)
12756+ idx_c = compile_arg0(recv)
12757+ ch = lit
12758+ if lit == "\n"
12759+ ch = "\\n"
12760+ elsif lit == "\t"
12761+ ch = "\\t"
12762+ elsif lit == "\r"
12763+ ch = "\\r"
12764+ elsif lit.ord == 92 || lit.ord == 39
12765+ # backslash or single quote — skip char optimization
12766+ return ""
12767+ end
12768+ "(" + str_c + "[(mrb_int)" + idx_c + "] " + c_op + " '" + ch + "')"
12769+ end
12770+
1270912771 def compile_eq(nid, op)
1271012772 recv = @nd_receiver[nid]
1271112773 lt = infer_type(recv)
@@ -12734,6 +12796,11 @@
1273412796 return "(" + lc + " != NULL)"
1273512797 end
1273612798 end
12799+ # Optimize: str[i] == "c" → direct char comparison (no malloc)
12800+ cc = try_char_cmp(nid, op)
12801+ if cc != ""
12802+ return cc
12803+ end
1273712804 if op == "=="
1273812805 return "(strcmp(" + lc + ", " + rc + ") == 0)"
1273912806 else