Switch Statements and Stack Overflows

Yeah, my original issue was primarily a Debug issue, Release builds were fine. But for this type of more-hobby-than-commercial project, Debug is often the primary target.

It seems like everyone here correctly understands the scenario, but if anyone wants any context around the issue that I was discussing in the original toot, it was the printName function in CwlDemangle, literally an implementation of Swift's NodePrinter::print. The C++ original is here:

and you can see the Swift function here:

The biggest insult here is that the C++ ran fine but Swift would overflow, even though both functions are roughly equivalent in terms of structure.

I briefly had an implementation that refactored the immediate printing into deferred printing via a heap-based queue (and iterated until the queue was drained):

but needing to do this made me feel sad – mostly because it's admitting defeat to C++. Instead, I got the stack usage down from 36kB to about 12kB by aggressively factoring all the case legs out into child functions and even though Xcode does most of the work for you (select code, refactor -> extract to method), it's still 750+/750- lines of churn.

And at the end of that, the function still takes 12kB (i.e. a lot) for a function where no path through the function is longer than 3 steps. But 12kB is "enough to pass my test case" so :person_shrugging:

Oh, and we'll all quietly ignore the fact that SE-0262 remains stuck in permanent limbo? Excellent :stuck_out_tongue:

3 Likes