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 cc11284+
end1128111285
return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") < 0)"1128211286
end1128311287
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 cc11295+
end1128811296
return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") > 0)"1128911297
end1129011298
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 cc11310+
end1129911311
return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") <= 0)"1130011312
end1130111313
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 cc11321+
end1130611322
return "(strcmp(" + compile_expr(recv) + ", " + compile_arg0(nid) + ") >= 0)"1130711323
end1130811324
return "(" + compile_expr(recv) + " >= " + compile_arg0(nid) + ")"@@ -12706,6 +12722,52 @@
1270612722
tmp1270712723
end1270812724
12725+
# Try to compile str[i] <op> "c" as direct char comparison12726+
# Returns "" if not applicable12727+
def try_char_cmp(nid, c_op)12728+
recv = @nd_receiver[nid]12729+
args_id = @nd_arguments[nid]12730+
if args_id < 012731+
return ""12732+
end12733+
a = get_args(args_id)12734+
if a.length == 012735+
return ""12736+
end12737+
arg_id = a[0]12738+
if @nd_type[arg_id] != "StringNode"12739+
return ""12740+
end12741+
lit = @nd_content[arg_id]12742+
if lit == ""12743+
lit = @nd_unescaped[arg_id]12744+
end12745+
if lit.length != 112746+
return ""12747+
end12748+
if @nd_type[recv] != "CallNode" || @nd_name[recv] != "[]"12749+
return ""12750+
end12751+
sr = @nd_receiver[recv]12752+
if sr < 0 || infer_type(sr) != "string"12753+
return ""12754+
end12755+
str_c = compile_expr(sr)12756+
idx_c = compile_arg0(recv)12757+
ch = lit12758+
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 == 3912765+
# backslash or single quote — skip char optimization12766+
return ""12767+
end12768+
"(" + str_c + "[(mrb_int)" + idx_c + "] " + c_op + " '" + ch + "')"12769+
end12770+
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
end1273612798
end12799+
# Optimize: str[i] == "c" → direct char comparison (no malloc)12800+
cc = try_char_cmp(nid, op)12801+
if cc != ""12802+
return cc12803+
end1273712804
if op == "=="1273812805
return "(strcmp(" + lc + ", " + rc + ") == 0)"1273912806
else