Commit 695bdcc4
695bdcc4ba56Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 4 files · +2 added · ~2 modified
Description
Add megamorphic dispatch (3+ types) and POLY arithmetic
Megamorphic dispatch:
- 3+ class types → per-method dispatch function (switch on class tag)
- sp_dispatch_(sp_RbValue obj) auto-generated at file scope
- 2 types remains bimorphic (inline if/else) — no change
- 1 type remains monomorphic (direct call) — no change
POLY arithmetic:
- sp_poly_add/sub/mul/div: Int+Int, Float+Float (promotion), String+String
- sp_poly_gt/lt/ge/le/eq/neq: comparison returning mrb_bool
- to_s on POLY → sp_poly_to_s
3-tier dispatch now fully implemented: mono (1 type) → direct C call bimorphic (2 types) → call-site inline if/else megamorphic (3+ types) → dispatch function
Tests: bm_mega.rb (Dog/Cat/Bird), bm_poly_arith.rb (calc/double/bigger?). All regressions pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 695bdcc4ba56
- Parent
- ac474542cb8d
examples/bm_mega.rbaddedView file
1+
# Test megamorphic dispatch (3+ types)2+
3+
class Dog4+
def speak; "Woof!"; end5+
def name; "Dog"; end6+
end7+
8+
class Cat9+
def speak; "Meow!"; end10+
def name; "Cat"; end11+
end12+
13+
class Bird14+
def speak; "Tweet!"; end15+
def name; "Bird"; end16+
end17+
18+
def make_noise(animal)19+
puts animal.speak20+
end21+
22+
make_noise(Dog.new) # Woof!23+
make_noise(Cat.new) # Meow!24+
make_noise(Bird.new) # Tweet!25+
26+
# Also test with name method27+
def identify(animal)28+
puts animal.name29+
end30+
31+
identify(Dog.new) # Dog32+
identify(Cat.new) # Cat33+
identify(Bird.new) # Bird34+
35+
puts "done"examples/bm_poly_arith.rbaddedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…