Skip to content

Commit 372a55f

Browse files
Add number of call frame check
1 parent a81358b commit 372a55f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

benchmarks/vm_deep_call.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This script checks the max number of call frames under WebAssembly restriction
2+
#
3+
# Example runs
4+
# $ ruby vm_deep_call.rb
5+
# $ RUBY_EXE="wasmtime run --mapdir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
6+
# $ RUBY_EXE="wasmtime run --env RUBY_FIBER_MACHINE_STACK_SIZE=20971520 --mapdir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
7+
8+
def vm_rec n
9+
vm_rec n - 1 if n > 0
10+
end
11+
12+
def vm_c_rec n
13+
1.times do
14+
vm_c_rec n - 1 if n > 0
15+
end
16+
end
17+
18+
def vm_rec_fiber n
19+
Fiber.new { vm_rec n }.resume
20+
end
21+
22+
def vm_c_rec_fiber n
23+
Fiber.new { vm_c_rec n }.resume
24+
end
25+
26+
def check(ruby_exe, target, n)
27+
cmd = %Q(#{ruby_exe} -r #{File.expand_path(__FILE__)} -e "#{target}(#{n})")
28+
Kernel.system(cmd, err: File::NULL)
29+
end
30+
31+
def bisect(ruby_exe, target)
32+
min = 0
33+
max = 15000
34+
while min < max
35+
mid = (min + max) / 2
36+
ok = check(ruby_exe, target, mid)
37+
if ok
38+
min = mid + 1
39+
else
40+
max = mid
41+
end
42+
end
43+
min
44+
end
45+
46+
def main
47+
ruby_exe = ENV['RUBY_EXE'] || 'ruby'
48+
puts "How deep the call stack can be"
49+
puts " with only VM calls: " + bisect(ruby_exe, "vm_rec").to_s
50+
puts " with only VM calls in Fiber: " + bisect(ruby_exe, "vm_rec_fiber").to_s
51+
puts " with VM and C calls: " + bisect(ruby_exe, "vm_c_rec").to_s
52+
puts " with VM and C calls in Fiber: " + bisect(ruby_exe, "vm_c_rec_fiber").to_s
53+
end
54+
55+
main if $0 == __FILE__

0 commit comments

Comments
 (0)