Commit 3f8b35a1

3f8b35a1c1d5Merged

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

Description

Fold s[i] == "c" into a raw byte comparison

Comparing a single-character string index against a one-byte literal (template[i] == "{") emitted sp_str_eq(sp_str_char_at_or_nil(s, i), "{"), which allocates a fresh one-character string for every access and then strcmp's it. Legacy emits a direct byte load and compare. In a string scanner like the template engine this dominates the hot loop.

Add emit_strchar_cmp: when one operand of ==/!= is s[i] (a string []/slice with a single int index) and the other is a single-byte string literal, emit ((unsigned char)(s)[(mrb_int)(i)] == <byte>). A negative integer-literal index is excluded (Ruby s[-1] indexes from the end), so it falls back to the general path.

bm_template: 1.36x -> 1.11x vs legacy, output matches. make test 845, make bench 57, optcarrot checksum 59662.

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

Details

Commit
3f8b35a1c1d5
Parent
416ebcfa1092

src/codegen_call.cmodifiedView file

@@ -76,6 +76,60 @@
7676 return 1;
7777 }
7878
79+/* `s[i]` on a string with a single non-negative-style int index. Records the
80+ string receiver and index nodes. Used to fold `s[i] == "c"` into a raw byte
81+ comparison (no per-access 1-char string allocation). */
82+static int str_index1(Compiler *c, int node, int *out_recv, int *out_idx) {
83+ const NodeTable *nt = c->nt;
84+ const char *ty = nt_type(nt, node);
85+ if (!ty || strcmp(ty, "CallNode")) return 0;
86+ const char *nm = nt_str(nt, node, "name");
87+ if (!nm || (strcmp(nm, "[]") && strcmp(nm, "slice"))) return 0;
88+ if (nt_ref(nt, node, "block") >= 0) return 0;
89+ int recv = nt_ref(nt, node, "receiver");
90+ if (recv < 0 || comp_ntype(c, recv) != TY_STRING) return 0;
91+ int args = nt_ref(nt, node, "arguments");
92+ int an = 0;
93+ const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
94+ if (an != 1 || comp_ntype(c, av[0]) != TY_INT) return 0;
95+ *out_recv = recv;
96+ *out_idx = av[0];
97+ return 1;
98+}
99+
100+/* A bare single-byte string literal, e.g. `"{"`. */
101+static int single_byte_lit(Compiler *c, int node, unsigned char *out) {
102+ const char *ty = nt_type(c->nt, node);
103+ if (!ty || strcmp(ty, "StringNode")) return 0;
104+ const char *s = nt_str(c->nt, node, "unescaped");
105+ if (!s) s = nt_str(c->nt, node, "content");
106+ if (!s || s[0] == '\0' || s[1] != '\0') return 0;
107+ *out = (unsigned char)s[0];
108+ return 1;
109+}
110+
111+/* Emit `s[i] == "c"` / `!=` as a raw byte compare when one operand is a
112+ single-char string index and the other a single-byte literal. The index is
113+ guarded against negatives (Ruby `s[-1]` indexes from the end) by falling
114+ back to the general path. Returns 1 if it emitted the optimized form. */
115+static int emit_strchar_cmp(Compiler *c, int recv, int arg, int eq, Buf *b) {
116+ int sr, si;
117+ unsigned char ch;
118+ int ok = (str_index1(c, recv, &sr, &si) && single_byte_lit(c, arg, &ch)) ||
119+ (str_index1(c, arg, &sr, &si) && single_byte_lit(c, recv, &ch));
120+ if (!ok) return 0;
121+ /* A negative literal index would read out of bounds; only fold when the
122+ index can't be a negative literal. */
123+ const char *ity = nt_type(c->nt, si);
124+ if (ity && !strcmp(ity, "IntegerNode") && nt_int(c->nt, si, "value", 0) < 0) return 0;
125+ buf_puts(b, "((unsigned char)(");
126+ emit_expr(c, sr, b);
127+ buf_puts(b, ")[(mrb_int)(");
128+ emit_expr(c, si, b);
129+ buf_printf(b, ")] %s %u)", eq ? "==" : "!=", (unsigned)ch);
130+ return 1;
131+}
132+
79133 void emit_call(Compiler *c, int id, Buf *b) {
80134 const NodeTable *nt = c->nt;
81135 if (emit_partition_expr(c, id, b)) return;
@@ -4243,6 +4297,7 @@
42434297 int fr = eq_family(rt), fa = eq_family(a0);
42444298 /* same comparable family: compare by value */
42454299 if (fr && fa && fr == fa) {
4300+ if (fr == 2 && emit_strchar_cmp(c, recv, argv[0], eq, b)) return;
42464301 if (fr == 2) { buf_puts(b, eq ? "sp_str_eq(" : "(!sp_str_eq("); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, eq ? ")" : "))"); }
42474302 else if (fr == 5) { buf_puts(b, eq ? "sp_range_eq(" : "(!sp_range_eq("); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, eq ? ")" : "))"); }
42484303 else { buf_puts(b, "("); emit_expr(c, recv, b); buf_printf(b, " %s ", eq ? "==" : "!="); emit_expr(c, argv[0], b); buf_puts(b, ")"); }