I tried to print the max recursion depth:
fn recursion(n: Int):
print(n)
recursion(n + 1)
fn main():
recursion(0)
Such code would end at 1000 loops in Python, but It never ends in Mojo.
I’ve seen that Mojo has some magical built-in optimazations, I thought the code might have been optimized. To prevent any optimization, I introduced random values:
from random import random_si64
fn recursion(n: Int):
print(n)
var rand = Int(random_si64(1, 10))
recursion(n + rand)
fn main():
recursion(0)
But nothing changes.
So my question is:
- There isn’t a max recursion depth limitation, is this correct? Or am I testing in a wrong way?
- Why the runtime memory cost doesn’t change? It’s expected to increase due to more and more call stacks?
Updated
The program crashes if there are two recursion(n+1)
, not sure why:
fn recursion(n: Int):
print(n)
recursion(n + 1)
recursion(n + 1)
fn main():
recursion(0)