Commit f2a01c83

f2a01c8333a4Merged

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

Description

Skip unused main() prologue setup (sp_re_init / ARGV / srand)

A trivial program runs four unconditional setup steps in main() that most of them never need. Gate each on a whole-program feature scan:

  • sp_re_init() -- installs the symbol-name, regex-error, class-exception and global-mark hooks. Emitted (and called) only when at least one applies. The unconditional sp_sym_name_fn assignment was also what kept sp_sym_to_s and its 64 KB sp_dyn_syms pool referenced; dropping it lets a symbol-free program link none of the symbol runtime.
  • the ARGV copy loop -- only when the program reads ARGV / ARGF / $*.
  • srand(time(NULL)) -- only when the program consumes randomness (rand/srand/sample/shuffle/Random).

scan_prologue_features over-approximates every flag (a user method named rand keeps srand; a :sym anywhere keeps the symbol hook), so a feature that is genuinely used is never missed -- a symbol/regex/random value can only originate from one of the scanned nodes. The sp_re_init body parts are each individually gated (symbols / regex / class machinery / global marks), so a program needing one still gets exactly that.

p 42 drops from 19352 to 17304 bytes (--gc-sections) and links no symbol or regex runtime. 964 test (gcc + clang) / 57 bench / optcarrot 59662; symbol sort, regex, ARGV and seeded random verified against CRuby, and GC-stress confirms heap globals/symbols still survive.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
f2a01c8333a4
Parent
74321da8eb6d

src/codegen.cmodifiedView file

@@ -1724,15 +1724,21 @@
17241724 buf_puts(b, ")");
17251725 }
17261726
1727-/* Emit the static regex-literal globals and the sp_re_init() that compiles
1728- them at startup. Always defines sp_re_init (empty when no literals) so
1729- main() can call it unconditionally. */
1727+/* Emit the static regex-literal globals and, when g_re_init_needed, the
1728+ sp_re_init() that installs the symbol/regex/class/global-mark hooks and
1729+ compiles the literals at startup. */
17301730 void emit_regex_section(Buf *b) {
17311731 for (int i = 0; i < g_re_count; i++) {
17321732 buf_printf(b, "static mrb_regexp_pattern *sp_re_pat_%d;\n", i);
17331733 }
1734+ /* sp_re_init wires the hooks below. When none apply (a trivial program uses
1735+ no symbols, regex, class machinery, or heap globals) neither the function
1736+ nor its main() call is emitted, so the symbol/regex runtime it would pin
1737+ stays unreferenced and links away. */
1738+ if (!g_re_init_needed) return;
17341739 buf_puts(b, "static void sp_re_init(void) {\n");
1735- buf_puts(b, " sp_sym_name_fn = sp_sym_to_s;\n");
1740+ if (g_uses_symbols)
1741+ buf_puts(b, " sp_sym_name_fn = sp_sym_to_s;\n");
17361742 if (g_needs_class_machinery)
17371743 buf_puts(b, " sp_user_exc_parent_fn = sp_user_exc_parent;\n");
17381744 /* Replace the runtime's hook with the superset that also marks this
@@ -1753,8 +1759,10 @@
17531759 }
17541760 }
17551761 /* From here on (runtime Regexp.new / dynamic patterns), a compile error
1756- raises a catchable RegexpError via sp_raise_cls instead of aborting. */
1757- buf_puts(b, " sp_re_set_error_handler(sp_re_default_error_handler);\n");
1762+ raises a catchable RegexpError via sp_raise_cls instead of aborting. Only
1763+ needed when the program actually constructs a regex. */
1764+ if (g_uses_regex)
1765+ buf_puts(b, " sp_re_set_error_handler(sp_re_default_error_handler);\n");
17581766 buf_puts(b, "}\n\n");
17591767 }
17601768
@@ -2163,6 +2171,43 @@
21632171 return 0;
21642172 }
21652173
2174+/* Whole-program scan for the prologue features (see codegen_internal.h). Each
2175+ flag over-approximates (a user method named `rand` keeps srand; that is
2176+ harmless), so a feature that is genuinely used is never missed: a symbol /
2177+ regex / random value can only originate from one of the nodes below. */
2178+static void scan_prologue_features(Compiler *c) {
2179+ const NodeTable *nt = c->nt;
2180+ g_uses_symbols = (c->nsymbols > 0);
2181+ g_uses_regex = 0; g_uses_argv = 0; g_uses_random = 0;
2182+ for (int i = 0; i < nt->count; i++) {
2183+ const char *ty = nt_type(nt, i);
2184+ if (!ty) continue;
2185+ if (!strcmp(ty, "RegularExpressionNode") || !strcmp(ty, "InterpolatedRegularExpressionNode"))
2186+ g_uses_regex = 1;
2187+ else if (!strcmp(ty, "SymbolNode") || !strcmp(ty, "InterpolatedSymbolNode"))
2188+ g_uses_symbols = 1;
2189+ else if (!strcmp(ty, "ConstantReadNode") || !strcmp(ty, "ConstantPathNode")) {
2190+ const char *nm = nt_str(nt, i, "name");
2191+ if (!nm) continue;
2192+ if (!strcmp(nm, "Regexp")) g_uses_regex = 1;
2193+ else if (!strcmp(nm, "Random")) g_uses_random = 1;
2194+ else if (!strcmp(nm, "ARGV") || !strcmp(nm, "ARGF")) g_uses_argv = 1;
2195+ else if (!strcmp(nm, "Symbol")) g_uses_symbols = 1;
2196+ }
2197+ else if (!strcmp(ty, "GlobalVariableReadNode") || !strcmp(ty, "GlobalVariableWriteNode")) {
2198+ const char *nm = nt_str(nt, i, "name");
2199+ if (nm && !strcmp(nm, "$*")) g_uses_argv = 1;
2200+ }
2201+ else if (!strcmp(ty, "CallNode")) {
2202+ const char *nm = nt_str(nt, i, "name");
2203+ if (!nm) continue;
2204+ if (!strcmp(nm, "to_sym") || !strcmp(nm, "intern")) g_uses_symbols = 1;
2205+ else if (!strcmp(nm, "rand") || !strcmp(nm, "srand") || !strcmp(nm, "sample") ||
2206+ !strcmp(nm, "shuffle") || !strcmp(nm, "shuffle!")) g_uses_random = 1;
2207+ }
2208+ }
2209+}
2210+
21662211 /* Emit one top-level output unit (a method, constructor, BEGIN/END block, or the
21672212 top-level body). Outside SP_COLLECT_ERRORS this is just the bare call. In
21682213 collect mode each unit runs under a setjmp: an `unsupported` gap longjmps back
@@ -2337,6 +2382,7 @@
23372382 buf_puts(&b, "default:return \"\";} }\n\n");
23382383 }
23392384 g_needs_class_machinery = program_needs_class_machinery(c);
2385+ scan_prologue_features(c);
23402386 if (g_needs_class_machinery) {
23412387 /* sp_cls_is_module[i]: 1 if user class i was defined as a module, 0 if class */
23422388 if (c->nclasses > 0) {
@@ -2748,6 +2794,9 @@
27482794 }
27492795 free(mk.p);
27502796 }
2797+ /* sp_re_init is worth emitting only if it would set at least one hook. */
2798+ g_re_init_needed = g_uses_symbols || g_uses_regex || g_needs_class_machinery ||
2799+ g_has_user_global_marks;
27512800
27522801 /* Constructor defs, method defs, and main go into a separate buffer. Any
27532802 proc literals they contain accumulate static functions into g_procs /
@@ -2784,8 +2833,10 @@
27842833
27852834 buf_puts(body, "int main(int argc,char**argv){\n");
27862835 buf_puts(body, " SP_GC_SAVE();\n");
2787- buf_puts(body, " sp_re_init();\n");
2788- buf_puts(body, " { sp_argv.len = argc - 1; sp_argv.data = (const char**)malloc(sizeof(const char*) * (size_t)(argc > 1 ? argc - 1 : 1)); for (int _ai = 0; _ai < argc - 1; _ai++) sp_argv.data[_ai] = sp_str_dup_external(argv[_ai + 1]); }\n");
2836+ if (g_re_init_needed) buf_puts(body, " sp_re_init();\n");
2837+ /* The ARGV copy loop only matters if the program reads ARGV / ARGF / $*. */
2838+ if (g_uses_argv)
2839+ buf_puts(body, " { sp_argv.len = argc - 1; sp_argv.data = (const char**)malloc(sizeof(const char*) * (size_t)(argc > 1 ? argc - 1 : 1)); for (int _ai = 0; _ai < argc - 1; _ai++) sp_argv.data[_ai] = sp_str_dup_external(argv[_ai + 1]); }\n");
27892840 buf_puts(body, " sp_program_name = argc > 0 ? argv[0] : \"\";\n");
27902841 /* Enable the backtrace substrate (Exception#backtrace, Kernel#caller) in
27912842 debug builds only: --debug compiles at -O0 with non-inlined methods, so
@@ -2800,8 +2851,9 @@
28002851 }
28012852 /* Ruby auto-seeds its PRNG at startup, so rand/shuffle/sample vary per
28022853 run. Seed once here; an explicit srand(seed) in user code runs later
2803- and overrides this for reproducible sequences. */
2804- buf_puts(body, " srand((unsigned)time(NULL));\n");
2854+ and overrides this for reproducible sequences. Skipped when the program
2855+ consumes no randomness (no time() / srand call at all). */
2856+ if (g_uses_random) buf_puts(body, " srand((unsigned)time(NULL));\n");
28052857 /* Register END blocks (atexit runs LIFO, so they execute in reverse registration order) */
28062858 for (int e = 1; e <= end_count; e++)
28072859 buf_printf(body, " atexit(sp_end_fn_%d);\n", e);