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 effect2+
# Small methods called in tight loops3+
4+
class Vec25+
attr_reader :x, :y6+
def initialize(x, y)7+
@x = x8+
@y = y9+
end10+
def length_sq11+
@x * @x + @y * @y12+
end13+
def dot(other)14+
@x * other.x + @y * other.y15+
end16+
end17+
18+
# Use enough ivars to force pointer type (avoid value type pass-by-copy issue)19+
class Counter20+
attr_reader :value, :step, :name, :flag21+
def initialize22+
@value = 023+
@step = 124+
@name = "counter"25+
@flag = 026+
end27+
def increment28+
@value = @value + @step29+
end30+
def double_value31+
@value * 232+
end33+
end34+
35+
n = 1000000036+
37+
# Benchmark 1: length_sq in loop (single-expression method, no args)38+
sum = 039+
v = Vec2.new(3, 4)40+
i = 041+
while i < n42+
sum = sum + v.length_sq43+
i = i + 144+
end45+
puts sum # 25 * 10000000 = 25000000046+
47+
# Benchmark 2: dot product (single-expression, one arg)48+
sum2 = 049+
v1 = Vec2.new(1, 2)50+
v2 = Vec2.new(3, 4)51+
i = 052+
while i < n53+
sum2 = sum2 + v1.dot(v2)54+
i = i + 155+
end56+
puts sum2 # 11 * 10000000 = 11000000057+
58+
# Benchmark 3: counter increment + getter59+
c = Counter.new60+
i = 061+
while i < n62+
c.increment63+
i = i + 164+
end65+
puts c.value # 1000000066+
puts c.double_value # 2000000067+
68+
puts "done"examples/life.rbaddedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/emit.cmodifiedView file
Loading diff…
src/expr.cmodifiedView file
Loading diff…
src/type.cmodifiedView file
Loading diff…