Commit b2dc028b
b2dc028b2595Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Make GC.start collect; root interpolation and concat-chain operands
Comparing the gc_*.rb C output against the legacy backend surfaced that
spinelc emitted GC.start as a no-op (0LL) while legacy emits
(sp_gc_collect(), 0). The rooting tests therefore never forced a
collection at their GC.start points — they only collected incidentally
inside churn() — which masked two real rooting holes in string-building
expressions:
-
String interpolation
"#{a}#{b}"lowered tosp_sprintf(fmt, conv_a, conv_b)with the conversions inline. A conversion that allocates (sp_poly_to_s of an int/float, *_inspect, sp_float_to_s, …) left its fresh string in an unrooted temporary while a sibling conversion triggered a GC, freeing it before sp_sprintf read it (gc_root_heap_param's"#{gc_then}-#{body}"). Hoist each %s arg into a rooted temp inside a self-contained statement-expression (({ const char *_t = conv; SP_GC_ROOT(_t); … sp_sprintf(…); })), so it works in any expression position without polluting g_pre. Int (%lld) args are plain values and stay inline. -
Chained
a + b + clowered to nested sp_str_concat with operands evaluated inline; a fresh operand could be swept while a sibling operand allocated or forced a GC (concat_chain_operand_gc_root). When either operand may allocate, evaluate both into rooted temps first.
Then make GC.start (and GC.compact) actually call sp_gc_collect, matching Ruby/legacy semantics and making the gc_*.rb tests genuinely exercise a forced collection.
All seven gc_*.rb tests now pass under ASAN with SPINEL_GC_STRESS=1 and a forced GC.start. make test (845, normal and stress+verify) and make bench (57) stay green.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- b2dc028b2595
- Parent
- 75ab7d970562
src/codegen_call.cmodifiedView file
/* GC module methods */ if (recv >= 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode") && nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "GC")) { if (!strcmp(name, "start") && argc == 0) { buf_puts(b, "0LL"); return; } if (!strcmp(name, "compact") && argc == 0) { buf_puts(b, "0LL"); return; } if (!strcmp(name, "start") && argc == 0) { buf_puts(b, "(sp_gc_collect(), (mrb_int)0)"); return; } if (!strcmp(name, "compact") && argc == 0) { buf_puts(b, "(sp_gc_collect(), (mrb_int)0)"); return; } if (!strcmp(name, "stat") && argc == 0) { buf_puts(b, "sp_gc_stat()"); return; } } if (recv >= 0 && argc == 1 && int_arith_fn(name) && !ty_is_object(rt) && !ty_is_array(rt)) { if (rt == TY_STRING && !strcmp(name, "+")) { buf_puts(b, "sp_str_concat("); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")"); /* Root both operands when either may allocate: `a + b` evaluates both, and a fresh heap string from one can be swept while the other allocates or forces a GC (chained `a + b + c` with side-effecting operands — concat_chain_operand_gc_root). Recurses naturally: a chain's left operand is itself a `+` and gets its own rooted block. Pure literal / bare-read operands need no rooting. */ if (subtree_may_allocate(nt, recv) || subtree_may_allocate(nt, argv[0])) { int ta = ++g_tmp, tb = ++g_tmp; buf_printf(b, "({ const char *_t%d = ", ta); emit_expr(c, recv, b); buf_printf(b, "; SP_GC_ROOT(_t%d); const char *_t%d = ", ta, tb); emit_expr(c, argv[0], b); buf_printf(b, "; SP_GC_ROOT(_t%d); sp_str_concat(_t%d, _t%d); })", tb, ta, tb); } else { buf_puts(b, "sp_str_concat("); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")"); } return; } if (rt == TY_STRING && !strcmp(name, "*")) {src/codegen_expr.cmodifiedView file
Loading diff…