I recently had a test failing due to stack overflow. The culprit turned out to be a recursive function which consisted of nothing but a big match expression with about 20 different branches. This function's stack frame was so big that it could only recurse about 10 times before overflowing the stack. However I found that commenting out individual match branches would reduce the size of the stack frame, so it seems that each individual branch in a match gets its own stack space rather than all sharing the same space.
Is there a reason for this? Is it just an optimization which (like most optimizations) only gets applied in release mode? Or does it help with debugging somehow? If it's just a missed optimization is there a way to turn on this specific optimization in debug mode?
For now I've refactored my code to split the branches into separate functions and reduce the sizes of some local variables. This has made the problem go away for now but I'm worried it could rear its head again since the stack frame is still pretty large and there's plenty of other large match expressions in the code which could end up causing similar problems.