Statically compute upper bound for stack usage?

Previously, I thought that as long as we avoided recursive functions / mutually recursive group of functions, we can avoid blowing up the stack.

With this new realization of Drop handlers, if we even have recursive or mutually recursive Struct's/Enum's (via Rc/Box), it is enough, in degenerate cases, to blowup the stack.

Are there any design patterns around this? This seems severely limiting on wasm32 / small-stack platforms.

Yes, the link I provided shows a technique where you can implement Drop manually in a way that avoids the problem. I admit that this is a bit more cumbersome for DAGs than linked lists, but something similar should be possible.

I am not convinced it is possible. I think that technique requires being able to ask:

"if I were to drop this object, what other objects would get dropped"
"can I manually modify them so dropping them is non recursive"

this works when it's a linked list using Box to Self-type, but this doesn't work if we have Rc because in the latter case, the "drop procedure" is a black box, and we (afaik) can't do things like:

"if we were to drop this object, what else gets dropped?"

"can we assign this obj some new value so that it's drop is non recursive"

You can use Rc::try_unwrap before dropping it. If you post a small example struct, I can show how to do it.

1 Like

Actually, I'm wrong, you're right; I'm now convinced this can be done -- a bit messy, but do-able.