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:

  1. Add @needs_bigint → @needs_gc in recalc_needs_gc
  2. Add SP_GC_SAVE() at main function start when GC is needed
  3. 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 1
21752175 end
2176- if t == "fiber"
2176+ if t == "fiber" || t == "bigint"
21772177 return 1
21782178 end
21792179 if is_obj_type(t) == 1
@@ -7716,6 +7716,9 @@
77167716 if @needs_rb_value == 1 || @needs_lambda == 1 || @needs_fiber == 1
77177717 @needs_gc = 1
77187718 end
7719+ if @needs_bigint == 1
7720+ @needs_gc = 1
7721+ end
77197722 end
77207723 end
77217724
@@ -9275,6 +9278,9 @@
92759278 @in_main = 1
92769279 @indent = 1
92779280 push_scope
9281+ if @needs_gc == 1
9282+ emit(" SP_GC_SAVE();")
9283+ end
92789284
92799285 # Pre-declare main locals
92809286 lnames = "".split(",")
@@ -13133,6 +13139,8 @@
1313313139 else
1313413140 emit(" " + vref + " = sp_bigint_new_int(" + val + ");")
1313513141 end
13142+ # 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 return
1313713145 end
1313813146 if vt == "poly"
@@ -13182,6 +13190,7 @@
1318213190 if op == "/"
1318313191 emit(" " + vref + " = sp_bigint_div(" + vref + ", " + barg + ");")
1318413192 end
13193+ 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 return
1318613195 end
1318713196 if op == "+"