Commit 53101062

531010622676Merged

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

Description

Skip the global-mark hook override when there are no user globals

sp_re_init unconditionally set sp_gc_mark_globals_hook to the generated sp_mark_user_globals, and that function was always emitted. But a constructor (sp_gc_install_tu_hooks) already installs sp_re_mark_globals as the default before main, and sp_mark_user_globals only chains sp_re_mark_globals plus marks for the program's heap-typed globals/constants/class-ivars. When there are none -- a trivial program like p 42 -- the generated marker is byte-for- byte the runtime default, so emitting it and overriding the hook is pure redundancy.

Collect the user-global mark lines into a temp buffer; if empty, skip both the function and the sp_re_init assignment (g_has_user_global_marks gates the override). Building the gate from the actual emission keeps it drift-proof -- it can never disagree with what would have been marked, so a program that does have heap globals still emits and installs the marker.

964 test (gcc + clang) / 57 bench / optcarrot 59662; GC-stress confirms heap globals/consts/class-ivars still survive collection.

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

Details

Commit
531010622676
Parent
96875f620fbf

src/codegen.cmodifiedView file

@@ -1737,8 +1737,10 @@
17371737 buf_puts(b, " sp_user_exc_parent_fn = sp_user_exc_parent;\n");
17381738 /* Replace the runtime's hook with the superset that also marks this
17391739 program's heap-typed globals/constants/class-ivars (it chains to
1740- sp_re_mark_globals itself). */
1741- buf_puts(b, " sp_gc_mark_globals_hook = sp_mark_user_globals;\n");
1740+ sp_re_mark_globals itself). Skipped when there are none -- the marker would
1741+ equal the constructor-installed default, so it isn't emitted either. */
1742+ if (g_has_user_global_marks)
1743+ buf_puts(b, " sp_gc_mark_globals_hook = sp_mark_user_globals;\n");
17421744 if (g_re_count > 0) {
17431745 buf_puts(b, " sp_re_set_error_handler(sp_re_startup_error_handler);\n");
17441746 for (int i = 0; i < g_re_count; i++) {
@@ -2705,35 +2707,46 @@
27052707 swept (RAND = Rand.new lost its PRNG mid-render). Chained ahead of
27062708 the runtime's own sp_re_mark_globals via the hook in sp_re_init. */
27072709 {
2708- buf_puts(&b, "static void sp_mark_user_globals(void) {\n");
2709- buf_puts(&b, " sp_re_mark_globals();\n");
2710+ /* Collect the user-global mark lines into a temp buffer first. If none are
2711+ emitted, the marker would be identical to the runtime default
2712+ (sp_re_mark_globals, installed by a constructor before main), so skip it
2713+ and the sp_re_init hook override entirely -- a trivial program carries
2714+ neither. g_has_user_global_marks gates the override (see emit_regex_section). */
2715+ Buf mk; memset(&mk, 0, sizeof mk);
27102716 for (int i = 0; i < c->ngvars; i++) {
27112717 LocalVar *lv = &c->gvars[i];
27122718 if (!is_scalar_ret(lv->type)) continue;
2713- if (lv->type == TY_STRING) buf_printf(&b, " sp_mark_string(gv_%s);\n", lv->name);
2714- else if (lv->type == TY_POLY) buf_printf(&b, " sp_mark_rbval(gv_%s);\n", lv->name);
2715- else if (needs_root(lv->type)) buf_printf(&b, " if (gv_%s) sp_gc_mark((void *)gv_%s);\n", lv->name, lv->name);
2719+ if (lv->type == TY_STRING) buf_printf(&mk, " sp_mark_string(gv_%s);\n", lv->name);
2720+ else if (lv->type == TY_POLY) buf_printf(&mk, " sp_mark_rbval(gv_%s);\n", lv->name);
2721+ else if (needs_root(lv->type)) buf_printf(&mk, " if (gv_%s) sp_gc_mark((void *)gv_%s);\n", lv->name, lv->name);
27162722 }
27172723 for (int i = 0; i < c->nconsts; i++) {
27182724 LocalVar *lv = &c->consts[i];
27192725 if (!is_scalar_ret(lv->type)) continue;
2720- if (lv->type == TY_STRING) buf_printf(&b, " sp_mark_string(cst_%s);\n", lv->name);
2721- else if (lv->type == TY_POLY) buf_printf(&b, " sp_mark_rbval(cst_%s);\n", lv->name);
2722- else if (needs_root(lv->type)) buf_printf(&b, " if (cst_%s) sp_gc_mark((void *)cst_%s);\n", lv->name, lv->name);
2726+ if (lv->type == TY_STRING) buf_printf(&mk, " sp_mark_string(cst_%s);\n", lv->name);
2727+ else if (lv->type == TY_POLY) buf_printf(&mk, " sp_mark_rbval(cst_%s);\n", lv->name);
2728+ else if (needs_root(lv->type)) buf_printf(&mk, " if (cst_%s) sp_gc_mark((void *)cst_%s);\n", lv->name, lv->name);
27232729 }
27242730 for (int i = 0; i < c->nclasses; i++) {
27252731 ClassInfo *ci = &c->classes[i];
27262732 for (int j = 0; j < ci->nivars; j++) {
27272733 TyKind t = ci->ivar_types[j] == TY_UNKNOWN ? TY_INT : ci->ivar_types[j];
27282734 const char *iv = ci->ivars[j] + 1;
2729- if (t == TY_STRING) buf_printf(&b, " sp_mark_string(civ_%s_%s);\n", ci->name, iv);
2730- else if (t == TY_POLY) buf_printf(&b, " sp_mark_rbval(civ_%s_%s);\n", ci->name, iv);
2731- else if (needs_root(t)) buf_printf(&b, " if (civ_%s_%s) sp_gc_mark((void *)civ_%s_%s);\n", ci->name, iv, ci->name, iv);
2735+ if (t == TY_STRING) buf_printf(&mk, " sp_mark_string(civ_%s_%s);\n", ci->name, iv);
2736+ else if (t == TY_POLY) buf_printf(&mk, " sp_mark_rbval(civ_%s_%s);\n", ci->name, iv);
2737+ else if (needs_root(t)) buf_printf(&mk, " if (civ_%s_%s) sp_gc_mark((void *)civ_%s_%s);\n", ci->name, iv, ci->name, iv);
27322738 }
27332739 for (int j = 0; j < ci->nsg_readers; j++)
2734- buf_printf(&b, " sp_mark_rbval(sg_%s_%s);\n", ci->name, ci->sg_readers[j]);
2740+ buf_printf(&mk, " sp_mark_rbval(sg_%s_%s);\n", ci->name, ci->sg_readers[j]);
27352741 }
2736- buf_puts(&b, "}\n\n");
2742+ g_has_user_global_marks = (mk.p && mk.len > 0);
2743+ if (g_has_user_global_marks) {
2744+ buf_puts(&b, "static void sp_mark_user_globals(void) {\n");
2745+ buf_puts(&b, " sp_re_mark_globals();\n");
2746+ buf_puts(&b, mk.p);
2747+ buf_puts(&b, "}\n\n");
2748+ }
2749+ free(mk.p);
27372750 }
27382751
27392752 /* Constructor defs, method defs, and main go into a separate buffer. Any