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 Dog
4+ def speak; "Woof!"; end
5+ def name; "Dog"; end
6+end
7+
8+class Cat
9+ def speak; "Meow!"; end
10+ def name; "Cat"; end
11+end
12+
13+class Bird
14+ def speak; "Tweet!"; end
15+ def name; "Bird"; end
16+end
17+
18+def make_noise(animal)
19+ puts animal.speak
20+end
21+
22+make_noise(Dog.new) # Woof!
23+make_noise(Cat.new) # Meow!
24+make_noise(Bird.new) # Tweet!
25+
26+# Also test with name method
27+def identify(animal)
28+ puts animal.name
29+end
30+
31+identify(Dog.new) # Dog
32+identify(Cat.new) # Cat
33+identify(Bird.new) # Bird
34+
35+puts "done"