Commit e37b7ecf

e37b7ecf5d83Merged

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

Description

codegen: only emit the class-introspection helper bank when used

A minimal program like p 42 emitted the whole class-machinery bank unconditionally -- sp_class_superclass, sp_class_is_ancestor, sp_class_ancestors, sp_poly_is_a, sp_poly_get_class, the sp_class_lt/le/ gt/ge comparators, sp_class_le_mod, sp_class_is_module_val, and sp_user_exc_parent -- roughly a hundred lines of helpers it never references (p 42 dropped from 139 to 29 lines of C).

Gate the bank on a new g_needs_class_machinery flag, computed by a conservative pre-scan: any user class/module forces it on (struct/scan emission and dispatch may touch it); otherwise it is on only when the program does explicit builtin introspection -- a .class / is_a? / kind_of? / instance_of? / ancestors / superclass / === call, or a builtin class constant used as a value (puts Integer). The scan over-approximates on purpose: emitting an unused helper is harmless, whereas missing one would be a link error.

sp_class_to_s stays unconditional -- the runtime itself references it (sp_poly_puts / sp_poly_to_s SP_TAG_CLASS arms). sp_user_exc_parent is safe to gate because the runtime reaches it only through the NULL-initialised sp_user_exc_parent_fn pointer, whose assignment is gated by the same flag.

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

Details

Commit
e37b7ecf5d83
Parent
0762c603cbb2

src/codegen.cmodifiedView file

@@ -1479,7 +1479,8 @@
14791479 }
14801480 buf_puts(b, "static void sp_re_init(void) {\n");
14811481 buf_puts(b, " sp_sym_name_fn = sp_sym_to_s;\n");
1482- buf_puts(b, " sp_user_exc_parent_fn = sp_user_exc_parent;\n");
1482+ if (g_needs_class_machinery)
1483+ buf_puts(b, " sp_user_exc_parent_fn = sp_user_exc_parent;\n");
14831484 /* Replace the runtime's hook with the superset that also marks this
14841485 program's heap-typed globals/constants/class-ivars (it chains to
14851486 sp_re_mark_globals itself). */
@@ -1858,6 +1859,38 @@
18581859
18591860 /* ---- top level ---- */
18601861
1862+/* Conservative pre-scan: does the program use the class-introspection helper
1863+ bank (sp_class_to_s / sp_class_superclass / sp_class_is_ancestor /
1864+ sp_class_ancestors / sp_poly_is_a / sp_user_exc_parent / ...)? Any user
1865+ class/module forces it on (the struct/scan emission and dispatch may touch
1866+ it). With no user classes, only explicit builtin introspection needs it: a
1867+ `.class` / `is_a?` / `kind_of?` / `instance_of?` / `ancestors` /
1868+ `superclass` / `===` call, or a builtin class constant used as a value
1869+ (e.g. `puts Integer`, `Integer < Numeric`). Over-approximating is safe (it
1870+ only emits dead helpers); under-approximating would be a hard link error, so
1871+ the set is deliberately broad. */
1872+static int program_needs_class_machinery(Compiler *c) {
1873+ if (c->nclasses > 0) return 1;
1874+ const NodeTable *nt = c->nt;
1875+ for (int i = 0; i < nt->count; i++) {
1876+ const char *ty = nt_type(nt, i);
1877+ if (!ty) continue;
1878+ if (!strcmp(ty, "CallNode")) {
1879+ const char *nm = nt_str(nt, i, "name");
1880+ if (nm && (!strcmp(nm, "class") || !strcmp(nm, "is_a?") ||
1881+ !strcmp(nm, "kind_of?") || !strcmp(nm, "instance_of?") ||
1882+ !strcmp(nm, "ancestors") || !strcmp(nm, "superclass") ||
1883+ !strcmp(nm, "===")))
1884+ return 1;
1885+ }
1886+ else if (!strcmp(ty, "ConstantReadNode") || !strcmp(ty, "ConstantPathNode")) {
1887+ const char *nm = nt_str(nt, i, "name");
1888+ if (nm && is_builtin_class_name(nm)) return 1;
1889+ }
1890+ }
1891+ return 0;
1892+}
1893+
18611894 char *codegen_program(const NodeTable *nt) {
18621895 Compiler *c = comp_new(nt);
18631896 analyze_program(c);
@@ -1969,6 +2002,9 @@
19692002 "if(sp_ndyn<8192){sp_dyn_syms[sp_ndyn]=sp_str_dup_external(s);return (sp_sym)(%d+sp_ndyn++);}"
19702003 "return (sp_sym)0;}\n\n", ns, ns > 0 ? "sp_sym_names[i]" : "\"\"", ns, ns);
19712004 }
2005+ /* sp_class_to_s is referenced by the runtime itself (sp_poly_puts /
2006+ sp_poly_to_s SP_TAG_CLASS arms), so it is always emitted, independent of
2007+ the gated introspection bank below. */
19722008 {
19732009 buf_puts(&b, "static const char *sp_class_to_s(sp_Class c){switch(c.cls_id){");
19742010 for (int i = 0; i < c->nclasses; i++) {
@@ -1997,6 +2033,8 @@
19972033 buf_puts(&b, "case -130:return SPL(\"Math\");case -131:return SPL(\"Complex\");");
19982034 buf_puts(&b, "default:return \"\";} }\n\n");
19992035 }
2036+ g_needs_class_machinery = program_needs_class_machinery(c);
2037+ if (g_needs_class_machinery) {
20002038 /* sp_cls_is_module[i]: 1 if user class i was defined as a module, 0 if class */
20012039 if (c->nclasses > 0) {
20022040 buf_printf(&b, "static const int sp_cls_is_module[%d] = {", c->nclasses);
@@ -2246,6 +2284,7 @@
22462284 }
22472285 buf_puts(&b, " return 0;\n}\n");
22482286 }
2287+ } /* if (g_needs_class_machinery) */
22492288
22502289 /* class structs + GC scan functions. Forward-declare every typedef first so
22512290 a class struct may embed a pointer to a class defined later. */