4 ms·
Zig appears to have a proposed solution to this similar to your option 1, move recursion to the heap. https://github.com/ziglang/zig/issues/1639 https://github
by ximeng 5y ago
Zig appears to have a proposed solution to this similar to your option 1, move recursion to the heap.
https://github.com/ziglang/zig/issues/1639 https://github.com/ziglang/zig/issues/1639
They're also looking at static analysis of the maximum stack depth which might help in some cases.
- ziml77 5y agoBeing able to make recursive functions use the heap would be great. I assume it would need to be opt-in since you'll change performance characteristics, but I almost always rearrange recursive algorithms to use a loop and a heap allocated stack structure instead. There's no reason the compiler couldn't help do that for me.
- pcwalton 5y agoI'm highly skeptical that static analysis of maximum stack depth will work, because of higher-order functions.
- klyrs 5y agoIn rust, unsafe operations can be marked such to silence the compiler when you know better; one could imagine something similar for a stack depth analyzer in zig. Or, for example, zig could use a finite stack depth when it can prove that's feasible, and emit code that will spill to the heap for cases it can't.