Commit 15e86b2a
15e86b2a6b2bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: bigint nogc alloc to young heap (not old) for prompt GC collection
sp_gc_alloc_nogc was adding objects to sp_gc_old_heap, which is only swept during full GC (every 8 cycles). Intermediate bigint objects accumulated for 7 cycles before collection.
Fix: add to sp_gc_heap (young gen) so minor GC at safe points collects dead intermediates immediately.
pidigits n=10000: 6.0GB → 244MB memory, 10.0s → 6.3s (Ruby: 139MB, 11.6s)
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 15e86b2a6b2b
- Parent
- 3a2a7afb1d5b
spinel_codegen.rbmodifiedView file
@@ -6789,7 +6789,7 @@
67896789
gc_linkage = @needs_bigint == 1 ? "" : "static "67906790
emit_raw(gc_linkage + "void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}}size_t need=sizeof(sp_gc_hdr)+sz;int b=sp_gc_bucket(need);sp_gc_hdr*h=NULL;if(sp_gc_buckets[b]&&sp_gc_buckets[b]->size==need){h=sp_gc_buckets[b];sp_gc_buckets[b]=h->next;}if(!h){h=(sp_gc_hdr*)calloc(1,need);}h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}")67916791
if @needs_bigint == 16792-
emit_raw("void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_old_heap;sp_gc_old_heap=h;sp_gc_old_bytes+=need;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}")6792+
emit_raw("void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}")67936793
end67946794
emit_raw("")67956795
end