Commit 8f49d73a
8f49d73a7e78Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 2 files · +1 added · ~1 modified
Description
Add nil? on OBJECT types (ptr == NULL), port splay benchmark
nil? on pointer types now checks NULL at runtime instead of returning constant FALSE. Enables splay tree and other nil-initialized ivar patterns.
splay: Spinel 0.01s vs CRuby 0.23s (23x) 77/77 tests pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 8f49d73a7e78
- Parent
- c4912767cd8a
examples/bm_splay.rbaddedView file
1+
# Splay tree benchmark (from yjit-bench / V8 Octane)2+
3+
class SplayNode4+
attr_accessor :key, :value, :left, :right5+
def initialize(key, value)6+
@key = key7+
@value = value8+
@left = nil9+
@right = nil10+
@tag = "splay"11+
end12+
end13+
14+
class SplayTree15+
def initialize16+
@root = nil17+
@size = 018+
@tag = "tree"19+
end20+
21+
def empty_p22+
@root.nil?23+
end24+
25+
def insert(key, value)26+
if empty_p27+
@root = SplayNode.new(key, value)28+
else29+
splay(key)30+
if @root.key != key31+
node = SplayNode.new(key, value)32+
if key > @root.key33+
node.left = @root34+
node.right = @root.right35+
@root.right = nil36+
else37+
node.right = @root38+
node.left = @root.left39+
@root.left = nil40+
end41+
@root = node42+
end43+
end44+
@size = @size + 145+
end46+
47+
def find(key)48+
if empty_p49+
-150+
else51+
splay(key)52+
@root.key53+
end54+
end55+
56+
def splay(key)57+
dummy = SplayNode.new(0, 0)58+
sl = dummy59+
sr = dummy60+
t = @root61+
while true62+
if key < t.key63+
if t.left.nil?64+
break65+
end66+
if key < t.left.key67+
y = t.left68+
t.left = y.right69+
y.right = t70+
t = y71+
if t.left.nil?72+
break73+
end74+
end75+
sr.left = t76+
sr = t77+
t = t.left78+
elsif key > t.key79+
if t.right.nil?80+
break81+
end82+
if key > t.right.key83+
y = t.right84+
t.right = y.left85+
y.left = t86+
t = y87+
if t.right.nil?88+
break89+
end90+
end91+
sl.right = t92+
sl = t93+
t = t.right94+
else95+
break96+
end97+
end98+
sl.right = t.left99+
sr.left = t.right100+
t.left = dummy.right101+
t.right = dummy.left102+
@root = t103+
end104+
end105+
106+
# Deterministic: insert keys 0..24999 in scrambled order107+
tree = SplayTree.new108+
idx = 0109+
while idx < 25000110+
key = (idx * 7919) % 100000111+
tree.insert(key, idx)112+
idx = idx + 1113+
end114+
115+
# Find known keys116+
found = 0117+
idx = 0118+
while idx < 25000119+
key = (idx * 7919) % 100000120+
result = tree.find(key)121+
if result == key122+
found = found + 1123+
end124+
idx = idx + 1125+
end126+
127+
puts found128+
puts "done"src/expr.cmodifiedView file
Loading diff…