Commit 3b1f6171
3b1f6171d54eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Hoist loop-invariant string length out of while loops
while i < s.length re-ran sp_str_length (an O(n) strlen) every iteration
because the C compiler cannot prove the string is unchanged across the
allocating calls in the body. Legacy computes the length once before the
loop and reuses it.
In emit_while, when the predicate tests s.length/s.size for a string
local s that the body never mutates (reassignment or an in-place mutating
method, per legacy's body_mutates_var?), emit mrb_int _t = sp_str_length(s)
before the loop and substitute the temp for matching s.length reads inside.
Hoist state is saved/restored for nested loops.
bm_template: 1.09x -> 1.03x vs legacy, output matches. General win for any string-scanning while loop. make test 845, make bench 57, optcarrot checksum 59662, self-host bootstrap IR+C fixpoint OK.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 3b1f6171d54e
- Parent
- 8433e129a114
src/codegen_call.cmodifiedView file
buf_printf(b, "sp_re_scan_poly(sp_re_pat_%d, %s)", re_lit_index(c, argv[0]), r); } else if (!strcmp(name, "to_sym") || !strcmp(name, "intern")) buf_printf(b, "sp_sym_intern(%s)", r); else if (!strcmp(name, "length") || !strcmp(name, "size")) buf_printf(b, "sp_str_length(%s)", r); else if (!strcmp(name, "length") || !strcmp(name, "size")) { if (g_hoist_len_var && g_hoist_len_recv && recv >= 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "LocalVariableReadNode") && nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), g_hoist_len_recv)) buf_puts(b, g_hoist_len_var); else buf_printf(b, "sp_str_length(%s)", r); } else if (!strcmp(name, "bytesize")) buf_printf(b, "(mrb_int)sp_str_byte_len(%s)", r); else if (!strcmp(name, "upcase")) buf_printf(b, "sp_str_upcase(%s)", r); else if (!strcmp(name, "downcase")) buf_printf(b, "sp_str_downcase(%s)", r);src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…