Skip to content

Commit e2e7f30

Browse files
committed
Matt Bittarelli is a genius
1 parent 6241e57 commit e2e7f30

File tree

2 files changed

+7
-26
lines changed

2 files changed

+7
-26
lines changed

test/test_helpers/stack_busters/factorial_stack_buster.rb

+3-22
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,13 @@ def stack_depth_remaining
2525
def stack_overflow_threshold
2626
# Use a frame size that's larger than the expected frame size to ensure
2727
# that limit is less than the point of overflow
28-
limit = last_good_limit = stack_depth_limit_for_frame_size(LARGEST_VM_STACK_SIZE * 2)
29-
last_overflow_limit = nil
30-
# Determine an upper-bound for binary search
28+
limit = stack_depth_limit_for_frame_size(LARGEST_VM_STACK_SIZE * 2)
3129
loop do
3230
begin
3331
unoptimized_factorial(limit)
34-
last_good_limit = limit
3532
limit *= 2
36-
rescue SystemStackError
37-
last_overflow_limit = limit
38-
break
39-
end
40-
end
41-
42-
# Reset for binary search for point of stack overflow
43-
limit = last_good_limit
44-
loop do
45-
return last_overflow_limit if last_overflow_limit == last_good_limit + 1
46-
begin
47-
half_the_distance_to_overflow = (last_overflow_limit - limit) / 2
48-
limit += half_the_distance_to_overflow
49-
unoptimized_factorial(limit)
50-
last_good_limit = limit
51-
rescue SystemStackError
52-
last_overflow_limit = limit
53-
limit = last_good_limit
33+
rescue SystemStackError => e
34+
return e.backtrace.length
5435
end
5536
end
5637
end

test/test_helpers/stack_busters/vanilla_stack_buster.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def stack_depth_remaining
1414

1515
private
1616

17-
def stack_overflow_threshold(depth = 1)
18-
stack_overflow_threshold(depth + 1)
19-
rescue SystemStackError
20-
depth
17+
def stack_overflow_threshold
18+
stack_overflow_threshold
19+
rescue SystemStackError => e
20+
e.backtrace.length
2121
end
2222
end
2323
end

0 commit comments

Comments
 (0)