Recursive call stack depth

603 views Asked by At

I have a recursive function that works for input where the call stack depth is up to 1000, but fails for bigger inputs. I converted the function to be tail recursive and that allowed it to get to about 1350.

What are the limits and is there any way to increase that limit?

I am working with pure functions and would like to avoid having to use operations. I have a solution that breaks up the problem into a composition of steps, each of which has a smaller stack depth, but it is rather contrived since its only purpose is to avoid the issue and it is more complex.

4

There are 4 answers

1
Nick Battle On BEST ANSWER

This is my mistake again... the setting for the Java stack is -Xss (the -Xms setting is the starting heap size), sorry. So if you use the JVM Arguments section in the Debugger tab of the launcher, and set something like -Xss5m, you should get further.

In a simple experiment with a recursive function, the default stack allowed me a depth of 227 calls. Using -Xss5m gave me 4020 calls, and -Xss10m gave me 8050 calls. Note that these stack sizes are somewhat less that the Gb sizes you were trying - 5Mb of stack is a lot of calls!

0
Nick Battle On

Overture does not impose a stack limit over the underlying Java stack limit, so it will simply respect the -Xms JVM argument. I think the regular execution stack for the interpreter comes from the Overture.ini file (top level), where you see the -Xmx argument to set the maximum heap. Can you try adding (say) -Xms128m, or a size of your choice, and see whether that gets you further?

2
Lausdahl On

It sounds like you are asking about how to increase the Java Stack Limit in the Overture debugger and not in the Overture IDE (overture.ini).

To change pass additional arguments to the Overture debugger you need to add them to the launch configuration:

  1. Open the launch configuration
  2. Select the "Debugger" tab
  3. The add your arguments to the box shown next to "Arguments:" in the top

Overture Launch configuration

1
Paul On

I have tried with -Xms and -Xmx both set up to 2048m but without any impact. I have also tried on Overture 2.3.0 on both Mac OSX and Windows 10 with the same result.

To take my project out of the loop, I created a new project with one very simple function:

  countdown(n:nat) res:nat
  == if n=0 then n else countdown(n-1)

On both Windows and Mac I can call this with value 807 and be successful, while with 808 it fails with error:

internal error

Main 206: Error evaluating code
Detailed Message: internal error