Commit 5eb2c5c0

5eb2c5c05f60Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 6 files · +2 added · ~4 modified

Description

Compile Conway's Game of Life (life.rb): 20x faster than CRuby

Key fixes enabling life.rb compilation:

  • Cross-function param type inference: register caller body locals (not just params) before inferring argument types, enabling cell_get(cells, x+ix, y+iy) where ix/iy are local variables
  • Multi-pass function type resolution (4 iterations) for deep call chains (main → next_gen → neighbors → cell_get)
  • var_declare: don't widen specific types (ARRAY) to VALUE when conflicting with unresolved function return types
  • sp_IntArray_set for cells[idx] = val on IntArray
  • IntArray []= generates sp_IntArray_set instead of C array syntax

Benchmark: Spinel 0.03s vs CRuby 0.61s (200 generations, 40x80 grid)

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

Details

Commit
5eb2c5c05f60
Parent
92db18276631

examples/bm_inline.rbaddedView file

1+# Benchmark: method inlining effect
2+# Small methods called in tight loops
3+
4+class Vec2
5+ attr_reader :x, :y
6+ def initialize(x, y)
7+ @x = x
8+ @y = y
9+ end
10+ def length_sq
11+ @x * @x + @y * @y
12+ end
13+ def dot(other)
14+ @x * other.x + @y * other.y
15+ end
16+end
17+
18+# Use enough ivars to force pointer type (avoid value type pass-by-copy issue)
19+class Counter
20+ attr_reader :value, :step, :name, :flag
21+ def initialize
22+ @value = 0
23+ @step = 1
24+ @name = "counter"
25+ @flag = 0
26+ end
27+ def increment
28+ @value = @value + @step
29+ end
30+ def double_value
31+ @value * 2
32+ end
33+end
34+
35+n = 10000000
36+
37+# Benchmark 1: length_sq in loop (single-expression method, no args)
38+sum = 0
39+v = Vec2.new(3, 4)
40+i = 0
41+while i < n
42+ sum = sum + v.length_sq
43+ i = i + 1
44+end
45+puts sum # 25 * 10000000 = 250000000
46+
47+# Benchmark 2: dot product (single-expression, one arg)
48+sum2 = 0
49+v1 = Vec2.new(1, 2)
50+v2 = Vec2.new(3, 4)
51+i = 0
52+while i < n
53+ sum2 = sum2 + v1.dot(v2)
54+ i = i + 1
55+end
56+puts sum2 # 11 * 10000000 = 110000000
57+
58+# Benchmark 3: counter increment + getter
59+c = Counter.new
60+i = 0
61+while i < n
62+ c.increment
63+ i = i + 1
64+end
65+puts c.value # 10000000
66+puts c.double_value # 20000000
67+
68+puts "done"