Commit f7aed7c8
f7aed7c8ffebMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
perf(codegen): gate sp_argv init on actual ARGV usage
main() unconditionally ran a malloc + per-arg sp_str_dup_external
loop to populate sp_argv from argv. Programs that never reach
for ARGV paid the startup cost for nothing -- sp_argv is a
static global in lib/sp_runtime.h, BSS-zero-initialized to
{len=0, data=NULL}, and the GC mark hook (sp_mark_externals)
reads sp_argv.len first so the NULL data pointer is never
deref'd when the array stays empty.
Gate the init on a new @needs_argv flag. Flag is flipped by:
- The ConstantReadNode "ARGV" branch in compile_expr.
- The ARGV.length / ARGV[i] dispatch arms in compile_constant_recv_expr.
- A small pre-pass (scan_for_argv) called from emit_main before the init line is emitted. The compile-time arms fire too late to land before the header -- main()'s init is emitted before stmt compilation, same shape as the @needs_class_table situation -- so the pre-scan is what makes the gate effective. Linear in AST size; the early-exit at the first hit keeps the cost trivial.
A program with no ARGV references now emits a bare
int main(int argc,char**argv){ with zero startup work,
saving the per-arg strdup loop. Bootstrap fixpoint OK
(spinel_codegen / analyze read ARGV at the top so the flag
fires). Optcarrot checksum 59662 unchanged, fps 661.9
(within noise). 382 tests pass.
Details
- Commit
- f7aed7c8ffeb
- Parent
- a4ccdc1de7e8
spinel_codegen.rbmodifiedView file
@needs_rb_value = 0 @needs_regexp = 0 @needs_rand = 0 # sp_argv is declared in lib/sp_runtime.h as a static global, # zero-initialized in BSS. main() only needs to malloc + dup # the argv strings when user code actually reaches for ARGV; # otherwise the GC mark loop (sp_runtime.h, sp_mark_externals) # sees len=0 and skips, and the cost is zero. Flag is flipped # by the ARGV ConstantReadNode arm and the ARGV.length / # ARGV[i] dispatch sites. @needs_argv = 0 @regexp_patterns = "".split(",") @regexp_flags = "".split(",") # Dynamic-regex (InterpolatedRegularExpressionNode) call-site cache. emit_raw("}") end # Walk the AST looking for `ARGV` references (ConstantReadNode # named "ARGV"). Flips @needs_argv so emit_main's init gate # has the right value before it runs. Single global name, so # the scan is a flat tree walk -- linear in AST size. def scan_for_argv(nid) if nid < 0 return end if @needs_argv == 1 return end if @nd_type[nid] == "ConstantReadNode" && @nd_name[nid] == "ARGV" @needs_argv = 1 return end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length scan_for_argv(cs[k]) k = k + 1 end end def emit_main stmts = get_body_stmts(@root_id) # Pre-scan the entire AST for ARGV ConstantReadNode references # so @needs_argv lands before emit_main writes the init line. # The compile-side arms (compile_expr's ARGV branch, the # ARGV.length / [] dispatch) flip the flag too, but they run # after the init line has already been emitted -- this scan # is what makes the gate actually effective. scan_for_argv(@root_id) emit_raw("int main(int argc,char**argv){") emit_raw(" sp_argv.len=argc-1;sp_argv.data=(const char**)malloc(sizeof(const char*)*(argc>1?argc-1:1));{int _i;for(_i=0;_i<sp_argv.len;_i++)sp_argv.data[_i]=sp_str_dup_external(argv[_i+1]);}") # Gate the ARGV ingest: sp_argv is BSS-zero-initialized # (len=0, data=NULL), and sp_mark_externals' loop reads # `sp_argv.len` first so an unused-ARGV program never # touches the NULL data pointer. Programs that don't # reference ARGV save the per-startup malloc + strdup loop. if @needs_argv == 1 emit_raw(" sp_argv.len=argc-1;sp_argv.data=(const char**)malloc(sizeof(const char*)*(argc>1?argc-1:1));{int _i;for(_i=0;_i<sp_argv.len;_i++)sp_argv.data[_i]=sp_str_dup_external(argv[_i+1]);}") end if @needs_rand == 1 emit_raw(" srand((unsigned)time(NULL));") end end if t == "ConstantReadNode" if @nd_name[nid] == "ARGV" @needs_argv = 1 return "sp_argv" end rname = resolve_const_read_name(@nd_name[nid]) # ARGV methods if rcname == "ARGV" if mname == "length" @needs_argv = 1 return "sp_argv.len" end if mname == "[]" @needs_argv = 1 idx_expr = compile_arg0(nid) return "((" + idx_expr + " < sp_argv.len) ? sp_argv.data[(int)" + idx_expr + "] : NULL)" end