Commit 395236a0

395236a0923dMerged

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

Description

Pool heap-object allocations (fixes ao_render 2x, gcbench 1.5x)

spinelc allocated every X.new via sp_gc_alloc (malloc per object), so an allocation-heavy workload churned malloc/free: ao_render made ~5M allocations where legacy made ~5K, gcbench ~15M vs ~1.2M. Legacy pools each heap class -- sp_gc_collect recycles unmarked instances onto a bounded per-class free-list (via the gc-header recycle hook) and sp_X_new reuses them. The SP_POOL_* runtime machinery already exists; spinelc just wasn't emitting it.

Emit SP_POOL_DEFINE(Cls) + SP_POOL_NEW(Cls, scan) for each pooled heap class (every user/struct class except value types and exception subclasses). A recycled slot is not zeroed, so memset it before initialize to preserve the previous calloc-zero semantics.

bm_ao_render 2.0x -> 1.1x and bm_gcbench 1.5x -> 1.05x vs legacy. make test 845, make bench 57, optcarrot checksum 59662 (fps unchanged/slightly better -- it allocates many short-lived PPU/CPU objects too).

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

Details

Commit
395236a0923d
Parent
aee79e2bad98

src/codegen.cmodifiedView file

@@ -1140,6 +1140,7 @@
11401140 int cid = comp_class_index(c, ci->name);
11411141 if (ci->is_struct) {
11421142 /* Struct constructor: one parameter per member, set the backing ivars. */
1143+ buf_printf(b, "SP_POOL_DEFINE(%s)\n", ci->name);
11431144 buf_printf(b, "static sp_%s *sp_%s_new(", ci->name, ci->name);
11441145 for (int i = 0; i < ci->nivars; i++) {
11451146 if (i) buf_puts(b, ", ");
@@ -1147,10 +1148,11 @@
11471148 buf_printf(b, " a%d", i);
11481149 }
11491150 if (ci->nivars == 0) buf_puts(b, "void");
1150- buf_printf(b, ") {\n sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, %s%s%s);\n",
1151- ci->name, ci->name, ci->name,
1151+ buf_printf(b, ") {\n sp_%s *self = SP_POOL_NEW(%s, %s%s%s);\n",
1152+ ci->name, ci->name,
11521153 class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL",
11531154 class_needs_scan(ci) ? "_scan" : "");
1155+ buf_puts(b, " memset(self, 0, sizeof(*self));\n"); /* recycled slots are not zeroed */
11541156 buf_puts(b, " SP_GC_ROOT(self);\n");
11551157 buf_printf(b, " self->cls_id = %d;\n", cid);
11561158 for (int i = 0; i < ci->nivars; i++)
@@ -1184,6 +1186,11 @@
11841186 buf_puts(b, " return self;\n}\n");
11851187 return;
11861188 }
1189+ /* per-class free-list pool: sp_gc_collect recycles unmarked instances onto
1190+ the pool instead of free()ing them, and sp_X_new reuses them -- this
1191+ removes the malloc/free churn of allocation-heavy workloads. Exception
1192+ subclasses use sp_exc_new_sub storage, so they are not pooled. */
1193+ if (!class_is_exc_subclass(c, cid)) buf_printf(b, "SP_POOL_DEFINE(%s)\n", ci->name);
11871194 buf_printf(b, "static sp_%s *sp_%s_new(", ci->name, ci->name);
11881195 if (init >= 0 && c->scopes[init].nparams > 0) {
11891196 Scope *s = &c->scopes[init];
@@ -1208,10 +1215,11 @@
12081215 buf_printf(b, " SP_GC_ROOT(self);\n");
12091216 }
12101217 else {
1211- buf_printf(b, ") {\n sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, %s%s%s);\n",
1212- ci->name, ci->name, ci->name,
1218+ buf_printf(b, ") {\n sp_%s *self = SP_POOL_NEW(%s, %s%s%s);\n",
1219+ ci->name, ci->name,
12131220 class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL",
12141221 class_needs_scan(ci) ? "_scan" : "");
1222+ buf_puts(b, " memset(self, 0, sizeof(*self));\n"); /* recycled slots are not zeroed */
12151223 buf_printf(b, " SP_GC_ROOT(self);\n");
12161224 buf_printf(b, " self->cls_id = %d;\n", cid);
12171225 /* calloc zero-inits fields; a poly (boxed) ivar's zero pattern is not nil,