Commit f5371fa7

f5371fa7b865Merged

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

Description

perf: early-exit iterative type inference when fixed point reached

The iterative loop that refines return / ivar / param types ran a fixed 4 iterations. In practice most programs converge after 1-2. Capture a string fingerprint of the three refined arrays each iteration and break as soon as it stops changing.

Self-host compile of spinel_codegen.rb: 78 s -> 67 s (-14%). Full bootstrap: 5:59 -> 5:14 (-13%). Cumulative since baseline 6:19: -17%.

74 tests / 55 benchmarks pass. gen2.c == gen3.c.

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

Details

Commit
f5371fa7b865
Parent
22cb8b0286a5

spinel_codegen.rbmodifiedView file

@@ -7132,18 +7132,32 @@
71327132 end
71337133 end
71347134
7135+ # Build a string fingerprint of the arrays that iterative type inference
7136+ # refines. Identical fingerprints between successive iterations means a
7137+ # fixed point has been reached and further iterations are wasted work.
7138+ def inference_signature
7139+ @meth_return_types.join("|") + "/" + @cls_ivar_types.join("|") + "/" + @meth_param_types.join("|")
7140+ end
7141+
71357142 def compile
71367143 collect_all
71377144 infer_main_call_types
71387145 infer_function_body_call_types
71397146 infer_class_body_call_types
71407147 detect_poly_locals
7141- # Iterative type inference: converge param types, return types, ivar types
7148+ # Iterative type inference: converge param types, return types, ivar types.
7149+ # Stop early when the signature of these three arrays stops changing.
71427150 iter = 0
7151+ prev_sig = inference_signature
71437152 while iter < 4
71447153 infer_all_returns
71457154 infer_ivar_types_from_writers
71467155 detect_poly_params
7156+ cur_sig = inference_signature
7157+ if cur_sig == prev_sig
7158+ break
7159+ end
7160+ prev_sig = cur_sig
71477161 iter = iter + 1
71487162 end
71497163 # Fix nil/poly-typed ivars with attr_writer to nullable self type