Permalink
Browse files

Matt Bittarelli is a genius

  • Loading branch information...
tdg5 committed Mar 18, 2015
1 parent 6241e57 commit e2e7f30314fd3d0e1b2d138328d7deeb31e7bd96
@@ -25,32 +25,13 @@ def stack_depth_remaining
def stack_overflow_threshold
# Use a frame size that's larger than the expected frame size to ensure
# that limit is less than the point of overflow
limit = last_good_limit = stack_depth_limit_for_frame_size(LARGEST_VM_STACK_SIZE * 2)
last_overflow_limit = nil
# Determine an upper-bound for binary search
limit = stack_depth_limit_for_frame_size(LARGEST_VM_STACK_SIZE * 2)
loop do
begin
unoptimized_factorial(limit)
last_good_limit = limit
limit *= 2
rescue SystemStackError
last_overflow_limit = limit
break
end
end
# Reset for binary search for point of stack overflow
limit = last_good_limit
loop do
return last_overflow_limit if last_overflow_limit == last_good_limit + 1
begin
half_the_distance_to_overflow = (last_overflow_limit - limit) / 2
limit += half_the_distance_to_overflow
unoptimized_factorial(limit)
last_good_limit = limit
rescue SystemStackError
last_overflow_limit = limit
limit = last_good_limit
rescue SystemStackError => e
return e.backtrace.length
end
end
end
@@ -14,10 +14,10 @@ def stack_depth_remaining
private
def stack_overflow_threshold(depth = 1)
stack_overflow_threshold(depth + 1)
rescue SystemStackError
depth
def stack_overflow_threshold
stack_overflow_threshold
rescue SystemStackError => e
e.backtrace.length
end
end
end

0 comments on commit e2e7f30

Please sign in to comment.