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
int cid = comp_class_index(c, ci->name); if (ci->is_struct) { /* Struct constructor: one parameter per member, set the backing ivars. */ buf_printf(b, "SP_POOL_DEFINE(%s)\n", ci->name); buf_printf(b, "static sp_%s *sp_%s_new(", ci->name, ci->name); for (int i = 0; i < ci->nivars; i++) { if (i) buf_puts(b, ", "); buf_printf(b, " a%d", i); } if (ci->nivars == 0) buf_puts(b, "void"); buf_printf(b, ") {\n sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, %s%s%s);\n", ci->name, ci->name, ci->name, buf_printf(b, ") {\n sp_%s *self = SP_POOL_NEW(%s, %s%s%s);\n", ci->name, ci->name, class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL", class_needs_scan(ci) ? "_scan" : ""); buf_puts(b, " memset(self, 0, sizeof(*self));\n"); /* recycled slots are not zeroed */ buf_puts(b, " SP_GC_ROOT(self);\n"); buf_printf(b, " self->cls_id = %d;\n", cid); for (int i = 0; i < ci->nivars; i++) buf_puts(b, " return self;\n}\n"); return; } /* per-class free-list pool: sp_gc_collect recycles unmarked instances onto the pool instead of free()ing them, and sp_X_new reuses them -- this removes the malloc/free churn of allocation-heavy workloads. Exception subclasses use sp_exc_new_sub storage, so they are not pooled. */ if (!class_is_exc_subclass(c, cid)) buf_printf(b, "SP_POOL_DEFINE(%s)\n", ci->name); buf_printf(b, "static sp_%s *sp_%s_new(", ci->name, ci->name); if (init >= 0 && c->scopes[init].nparams > 0) { Scope *s = &c->scopes[init]; buf_printf(b, " SP_GC_ROOT(self);\n"); } else { buf_printf(b, ") {\n sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, %s%s%s);\n", ci->name, ci->name, ci->name, buf_printf(b, ") {\n sp_%s *self = SP_POOL_NEW(%s, %s%s%s);\n", ci->name, ci->name, class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL", class_needs_scan(ci) ? "_scan" : ""); buf_puts(b, " memset(self, 0, sizeof(*self));\n"); /* recycled slots are not zeroed */ buf_printf(b, " SP_GC_ROOT(self);\n"); buf_printf(b, " self->cls_id = %d;\n", cid); /* calloc zero-inits fields; a poly (boxed) ivar's zero pattern is not nil,