Most of the programming language has feature to increase limit of recursion depth (python => sys.setrecursionlimit, ruby => RUBY_THREAD_VM_STACK_SIZE). How to do this with Crystal?
Specifically, I want this code to be compiled and run without error.
def foobar(depth)
return true if depth == 5000000
foobar(depth + 1)
end
p foobar(0) # => true
There is no explicit recursion depth limit. Your code simply exceeds the stack size (which is 8MB) and there’s no more room on the stack for the program to continue further.
Changing the stack size is technically possible, but I don’t think it’s a good idea because it affects all fibers and thus heavily increases the overall memory use. It’s probably better to refactor the code to avoid such deep recursion.
I believe (plz someone correct mt if I’m wrong) this is set by the OS, Crystal runtime just catch a signal and translate into an exception, so this post can help you:
TL;DR; ulimit -s shows the stack default size ulimit -s 16384 set the stack size to 16M.