Hacker News new | past | comments | ask | show | jobs | submit login

If the optimization is not performed, you blow the stack because your stack usage is now O(n) instead of O(1).

That's why it's important to get the optimization in debug mode.




Are you saying that the achievement outlined in the article is making sure that this optimization is turned on in debug mode?

Because I thought the entire point of the article is that they have made a marked speed up in production mode, not in debug mode. And I've yet to see (what must be obvious to everyone else) why the compiler is missing this obvious optimization in production mode without the explicit markup.

And if the compiler is not missing this optimization in production mode, why is the article saying they've sped up their code by explicitly marking it for optimization?


The C programming language does not provide any guarantees that tail calls will be optimized out in this way. Compilers are free to do it if they want to and it doesn't violate the "as-if" rule, but they are neither required to do it, nor required not to do it.

However, if you can statically GUARANTEE that this "optimization" happens (which is what this new clang feature does), it opens up a new kind of programming pattern that was simply not possible in C before, it was only really possible in assembly. The reason you couldn't do it in C before is that if the compiler decides (which it is free to do) not to optimize the tail call, it adds to the stack, and you will get stack overflow pretty much instantly.

This MUSTTAIL thing is basically an extension to the C programming language itself: if you can guarantee that the tail call happens, you can structure your code differently because there's no risk of blowing the stack in this case. Another language that actually does this is Scheme, in which the language guarantees that tail calls are eliminated, for the very same reason. If a scheme interpreter doesn't do this, it's not a valid Scheme interpreter.

"Debug mode" vs. "Release mode" doesn't really enter into it: if this optimization doesn't happen, the program is no longer correct, regardless of what optimization level you've selected. Asking "why is the compiler missing this obvious optimization in production mode" is missing the point: you have to guarantee that it always happens in all compilation modes, otherwise the code doesn't work at all.


That makes a lot of sense, thank you.


The code has to be structured entirely differently for this optimisation to be possible. If the code was restructured without marking the optimisation as mandatory the code would break in debug mode (as it would quickly blow the stack)


Yes, without the attribute the only option is to use macros to generate different code in debug mode, which is possible but far from ideal.


What's the argument for building in debug mode vs specifying -g to the compiler to build with debug symbols?

I've previously encountered bugs that stubbornly refused to reproduce in a non-optimized build.

One thing that comes to mind is `#ifdef DEBUG`-style macros, although I'm curious if there's anything else I'm missing. Omitted stack frames, e.g. from inlining, perhaps?


> What's the argument for building in debug mode

Sometimes optimized binaries are just a nightmare to debug. Symbols do not help.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: