So I’ve been wondering about something. From time to time, it is very convenient in the compiler to use recursive algorithms – trees tend to lend themselves to that, after all. Usually it’s better to structure such algorithms with an explicit queue or stack, but that can sometimes be quite awkward, particularly since the borrow checker generally favors real recursion. For those scenarios, I’ve been thinking that perhaps we ought to integrate support for something like the stacker crate, by @alexcrichton:
stacker basically lets you insert manual stack growth checks and switch over to a larger stack if you would exceed the one you have. An example where this would be useful is MIR construction, which currently uses recursion to track scoping. This leads to stack overflow on very large examples like #29466.
The downsides of using stacker that I can see are:
- Currently, if you have switched to another stack segment, and you panic, it aborts, rather than shutting down cleanly. This can be fixed with
- We’d have to port this bit of assembler hackery over to new platforms in order to run the compiler. Seems doable.
- It can be less memory efficient to use the program stack in this fashion, since you have to keep around the whole stack frame, whereas if you use an explicit stack you’ll often pop up just the data you really need. Using stacker might encourage us down this road.