Commit b41e490b
b41e490b83ecMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · +1 added
Description
Add sudoku solver benchmark: 40x faster than CRuby
Adapted from yjit-bench. Uses flattened 2D arrays (1D with index math) and encoded multi-return values. Pure integer computation — ideal for Spinel's monomorphic AOT compilation.
Spinel: 0.21s vs CRuby: 8.3s (10000 iterations × 1 puzzle)
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- b41e490b83ec
- Parent
- abdb8047f43e
examples/sudoku.rbaddedView file
1+
# Sudoku solver benchmark (from yjit-bench)2+
# Adapted for Spinel AOT compilation3+
# Multi-return encoded: forward returns min * 1000 + min_c4+
5+
def sd_genmat_mr6+
mr = Array.new(324 * 9, 0)7+
mr_len = Array.new(324, 0)8+
mc = Array.new(729 * 4, 0)9+
r = 010+
i = 011+
while i < 912+
j = 013+
while j < 914+
k = 015+
while k < 916+
mc[r * 4] = 9 * i + j17+
mc[r * 4 + 1] = (i / 3 * 3 + j / 3) * 9 + k + 8118+
mc[r * 4 + 2] = 9 * i + k + 16219+
mc[r * 4 + 3] = 9 * j + k + 24320+
r = r + 121+
k = k + 122+
end23+
j = j + 124+
end25+
i = i + 126+
end27+
r2 = 028+
while r2 < 72929+
c2 = 030+
while c2 < 431+
idx = mc[r2 * 4 + c2]32+
mr[idx * 9 + mr_len[idx]] = r233+
mr_len[idx] = mr_len[idx] + 134+
c2 = c2 + 135+
end36+
r2 = r2 + 137+
end38+
mr39+
end40+
41+
def sd_genmat_mc42+
mc = Array.new(729 * 4, 0)43+
r = 044+
i = 045+
while i < 946+
j = 047+
while j < 948+
k = 049+
while k < 950+
mc[r * 4] = 9 * i + j51+
mc[r * 4 + 1] = (i / 3 * 3 + j / 3) * 9 + k + 8152+
mc[r * 4 + 2] = 9 * i + k + 16253+
mc[r * 4 + 3] = 9 * j + k + 24354+
r = r + 155+
k = k + 156+
end57+
j = j + 158+
end59+
i = i + 160+
end61+
mc62+
end63+
64+
# Returns min * 1000 + min_c (encoded pair)65+
def sd_update_forward(mr, mc, sr, sc, r)66+
min = 1067+
min_c = 068+
c2 = 069+
while c2 < 470+
idx = mc[r * 4 + c2]71+
sc[idx] = sc[idx] + 12872+
c2 = c2 + 173+
end74+
c2 = 075+
while c2 < 476+
base = mc[r * 4 + c2] * 977+
r2 = 078+
while r2 < 979+
rr = mr[base + r2]80+
sr[rr] = sr[rr] + 181+
if sr[rr] == 182+
cc2 = 083+
while cc2 < 484+
cc = mc[rr * 4 + cc2]85+
sc[cc] = sc[cc] - 186+
if sc[cc] < min87+
min = sc[cc]88+
min_c = cc89+
end90+
cc2 = cc2 + 191+
end92+
end93+
r2 = r2 + 194+
end95+
c2 = c2 + 196+
end97+
min * 1000 + min_c98+
end99+
100+
def sd_update_reverse(mr, mc, sr, sc, r)101+
c2 = 0102+
while c2 < 4103+
idx = mc[r * 4 + c2]104+
sc[idx] = sc[idx] - 128105+
c2 = c2 + 1106+
end107+
c2 = 0108+
while c2 < 4109+
base = mc[r * 4 + c2] * 9110+
r2 = 0111+
while r2 < 9112+
rr = mr[base + r2]113+
sr[rr] = sr[rr] - 1114+
if sr[rr] == 0115+
sc[mc[rr * 4]] = sc[mc[rr * 4]] + 1116+
sc[mc[rr * 4 + 1]] = sc[mc[rr * 4 + 1]] + 1117+
sc[mc[rr * 4 + 2]] = sc[mc[rr * 4 + 2]] + 1118+
sc[mc[rr * 4 + 3]] = sc[mc[rr * 4 + 3]] + 1119+
end120+
r2 = r2 + 1121+
end122+
c2 = c2 + 1123+
end124+
end125+
126+
def sd_solve(mr, mc, s)127+
sr = Array.new(729, 0)128+
sc = Array.new(324, 9)129+
hints = 0130+
i = 0131+
while i < 81132+
a = s[i] - 49133+
if a >= 0 && a < 9134+
sd_update_forward(mr, mc, sr, sc, i * 9 + a)135+
hints = hints + 1136+
end137+
i = i + 1138+
end139+
140+
cr = Array.new(81, -1)141+
cc = Array.new(81, 0)142+
i = 0143+
min = 10144+
dir = 1145+
count = 0146+
147+
while true148+
while i >= 0 && i < 81 - hints149+
if dir == 1150+
if min > 1151+
c = 0152+
while c < 324153+
if sc[c] < min154+
min = sc[c]155+
cc[i] = c156+
if min < 2157+
c = 324158+
end159+
end160+
c = c + 1161+
end162+
end163+
if min == 0 || min == 10164+
cr[i] = -1165+
dir = -1166+
i = i - 1167+
next168+
end169+
end170+
171+
c = cc[i]172+
if dir == -1 && cr[i] >= 0173+
sd_update_reverse(mr, mc, sr, sc, mr[c * 9 + cr[i]])174+
end175+
176+
r2 = cr[i] + 1177+
while r2 < 9 && sr[mr[c * 9 + r2]] != 0178+
r2 = r2 + 1179+
end180+
181+
if r2 < 9182+
encoded = sd_update_forward(mr, mc, sr, sc, mr[c * 9 + r2])183+
min = encoded / 1000184+
cc[i + 1] = encoded - min * 1000185+
cr[i] = r2186+
dir = 1187+
i = i + 1188+
else189+
cr[i] = -1190+
dir = -1191+
i = i - 1192+
end193+
end194+
195+
if i < 0196+
break197+
end198+
count = count + 1199+
i = i - 1200+
dir = -1201+
end202+
count203+
end204+
205+
mr = sd_genmat_mr206+
mc = sd_genmat_mc207+
208+
puzzle = [46,46,46,46,46,46,46,46,46,46,46,46,46,46,51,46,56,53,46,46,49,46,50,46,46,46,46,46,46,46,53,46,55,46,46,46,46,46,52,46,46,46,49,46,46,46,57,46,46,46,46,46,46,46,53,46,46,46,46,46,46,55,51,46,46,50,46,49,46,46,46,46,46,46,46,46,52,46,46,46,57]209+
210+
total = 0211+
n = 0212+
while n < 100213+
total = total + sd_solve(mr, mc, puzzle)214+
n = n + 1215+
end216+
puts total217+
puts "done"