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

@@ -415,6 +415,14 @@
415415 @needs_rb_value = 0
416416 @needs_regexp = 0
417417 @needs_rand = 0
418+ # sp_argv is declared in lib/sp_runtime.h as a static global,
419+ # zero-initialized in BSS. main() only needs to malloc + dup
420+ # the argv strings when user code actually reaches for ARGV;
421+ # otherwise the GC mark loop (sp_runtime.h, sp_mark_externals)
422+ # sees len=0 and skips, and the cost is zero. Flag is flipped
423+ # by the ARGV ConstantReadNode arm and the ARGV.length /
424+ # ARGV[i] dispatch sites.
425+ @needs_argv = 0
418426 @regexp_patterns = "".split(",")
419427 @regexp_flags = "".split(",")
420428 # Dynamic-regex (InterpolatedRegularExpressionNode) call-site cache.
@@ -9138,10 +9146,48 @@
91389146 emit_raw("}")
91399147 end
91409148
9149+ # Walk the AST looking for `ARGV` references (ConstantReadNode
9150+ # named "ARGV"). Flips @needs_argv so emit_main's init gate
9151+ # has the right value before it runs. Single global name, so
9152+ # the scan is a flat tree walk -- linear in AST size.
9153+ def scan_for_argv(nid)
9154+ if nid < 0
9155+ return
9156+ end
9157+ if @needs_argv == 1
9158+ return
9159+ end
9160+ if @nd_type[nid] == "ConstantReadNode" && @nd_name[nid] == "ARGV"
9161+ @needs_argv = 1
9162+ return
9163+ end
9164+ cs = []
9165+ push_child_ids(nid, cs)
9166+ k = 0
9167+ while k < cs.length
9168+ scan_for_argv(cs[k])
9169+ k = k + 1
9170+ end
9171+ end
9172+
91419173 def emit_main
91429174 stmts = get_body_stmts(@root_id)
9175+ # Pre-scan the entire AST for ARGV ConstantReadNode references
9176+ # so @needs_argv lands before emit_main writes the init line.
9177+ # The compile-side arms (compile_expr's ARGV branch, the
9178+ # ARGV.length / [] dispatch) flip the flag too, but they run
9179+ # after the init line has already been emitted -- this scan
9180+ # is what makes the gate actually effective.
9181+ scan_for_argv(@root_id)
91439182 emit_raw("int main(int argc,char**argv){")
9144- 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]);}")
9183+ # Gate the ARGV ingest: sp_argv is BSS-zero-initialized
9184+ # (len=0, data=NULL), and sp_mark_externals' loop reads
9185+ # `sp_argv.len` first so an unused-ARGV program never
9186+ # touches the NULL data pointer. Programs that don't
9187+ # reference ARGV save the per-startup malloc + strdup loop.
9188+ if @needs_argv == 1
9189+ 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]);}")
9190+ end
91459191 if @needs_rand == 1
91469192 emit_raw(" srand((unsigned)time(NULL));")
91479193 end
@@ -9777,6 +9823,7 @@
97779823 end
97789824 if t == "ConstantReadNode"
97799825 if @nd_name[nid] == "ARGV"
9826+ @needs_argv = 1
97809827 return "sp_argv"
97819828 end
97829829 rname = resolve_const_read_name(@nd_name[nid])
@@ -16092,9 +16139,11 @@
1609216139 # ARGV methods
1609316140 if rcname == "ARGV"
1609416141 if mname == "length"
16142+ @needs_argv = 1
1609516143 return "sp_argv.len"
1609616144 end
1609716145 if mname == "[]"
16146+ @needs_argv = 1
1609816147 idx_expr = compile_arg0(nid)
1609916148 return "((" + idx_expr + " < sp_argv.len) ? sp_argv.data[(int)" + idx_expr + "] : NULL)"
1610016149 end