Commit 3a2a7afb
3a2a7afb1d5bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: bigint GC correctness — add SP_GC_SAVE/ROOT in main, fix type_is_pointer
Three fixes for bigint GC:
- Add @needs_bigint → @needs_gc in recalc_needs_gc
- Add SP_GC_SAVE() at main function start when GC is needed
- Add "bigint" to type_is_pointer (was only in is_nullable_pointer_type) This ensures bigint variables get SP_GC_ROOT in main
Also add GC safe points after bigint assignment statements — triggers GC collection between statements (not during nested expressions).
pidigits n=1000: correct, 0.05s vs Ruby 0.18s (3.6x faster) pidigits n=10000: correct, 10s vs Ruby 11.6s (1.2x faster), 6GB memory
68 tests pass, bootstrap clean.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 3a2a7afb1d5b
- Parent
- 822ba282a8f9
spinel_codegen.rbmodifiedView file
@@ -2173,7 +2173,7 @@
21732173
if t == "mutable_str"21742174
return 121752175
end2176-
if t == "fiber"2176+
if t == "fiber" || t == "bigint"21772177
return 121782178
end21792179
if is_obj_type(t) == 1@@ -7716,6 +7716,9 @@
77167716
if @needs_rb_value == 1 || @needs_lambda == 1 || @needs_fiber == 177177717
@needs_gc = 177187718
end7719+
if @needs_bigint == 17720+
@needs_gc = 17721+
end77197722
end77207723
end77217724
@@ -9275,6 +9278,9 @@
92759278
@in_main = 192769279
@indent = 192779280
push_scope9281+
if @needs_gc == 19282+
emit(" SP_GC_SAVE();")9283+
end92789284
92799285
# Pre-declare main locals92809286
lnames = "".split(",")@@ -13133,6 +13139,8 @@
1313313139
else1313413140
emit(" " + vref + " = sp_bigint_new_int(" + val + ");")1313513141
end13142+
# Trigger GC after bigint statement (safe point - all results stored in vars)13143+
emit(" if(sp_gc_bytes>sp_gc_threshold){size_t _b=sp_gc_bytes;sp_gc_collect();size_t _f=_b-sp_gc_bytes;if(_f<_b/4)sp_gc_threshold=_b*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;}")1313613144
return1313713145
end1313813146
if vt == "poly"@@ -13182,6 +13190,7 @@
1318213190
if op == "/"1318313191
emit(" " + vref + " = sp_bigint_div(" + vref + ", " + barg + ");")1318413192
end13193+
emit(" if(sp_gc_bytes>sp_gc_threshold){size_t _b=sp_gc_bytes;sp_gc_collect();size_t _f=_b-sp_gc_bytes;if(_f<_b/4)sp_gc_threshold=_b*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;}")1318513194
return1318613195
end1318713196
if op == "+"