How to increase stack size more than 2047

Godot Version

4.4

Question

I am implementing Wave Function Collapse on 2D square lattice. All is fine with the code, but here is the issue. I use recursion A LOT and I ended up finishing the stack of 1024 bites. So even a 40x40 map does not work.
The code is right. In fact, I managed to increase stack size from the properties menu up to 2047 and 40x40 now works.
But not 50x50.

Properties does not allow me more than 2047.

I tried adding the line:
ProjectSettings.set_setting(“debug/settings/gdscript/max_call_stack”,4096)

in the _ready function.

It does not give rise to an exception, but also it does not do anything.

Now please everybody don’t lecture me on computer science.
I know recursion has its drawbacks wrt iteration.
I know you can turn any recursion into iteration, and vice versa.

To be honest, I already turned one of the recursions into an iteration and now I run 100x100 maps fine, and maybe even more (still have to try). Now I do recursion only in the func used to update entropies and not on the main one.
Fine. Problem solved.

But… NO!!!

No way! Dammit guys, I WANT to be able to set the stack size the the value I want, anytime! I can’t believe to not be able to do it!!!

So how can I do it?!

It’s not possible to go bigger than 2047. It’s a hardcoded value to avoid stack overflow:

In the VM if the depth is bigger than that constant it will just return early the default value:

If this is strongly important to you, my suggestion would be:

  1. see if Godot supports tail recursion (assuming you can make your recursive functions tail calls…)
  2. if not, consider proposing making gdscript properly tail recursive

Thank you all for your replies!

I think that, instead of propising making gdscript properly tail recursive, one might simply propose to remove the hardcode maximum of 2047. I’m perfectly fine with the default value of 1024, I don’t get why imposing this further constraint. A constraint that is actually close to the default value, which means even less sense to me.

Practically speaking, I suspect you’ll find the devs unwilling to contemplate removing the hard limit on the stack; something needs to prevent stack overflow, and there may well be other parts of the code that do not behave well (or indeed, may behave extremely badly) if the stack overruns. You might be able to convince them to up the hard limit, but I expect removing it entirely will be a difficult thing to sell.

On the other hand, you’ve got source for Godot, and @mrcdk has pointed you at the relevant bits of it; you could always set MAX_CALL_DEPTH yourself and build from source.

2 Likes