4 ms·
Maybe worth noting: when I tested a few years ago at least gcc (don't remember whether I tested clang) only applied TCO to small subprograms. As soon as the ite
by pflanze 8y ago
Maybe worth noting: when I tested a few years ago at least gcc (don't remember whether I tested clang) only applied TCO to small subprograms. As soon as the iterations were multiply nested and/or across multiple top-level functions (even if all of them in the same compilation unit) it would leave parts of the code still eating stack.
- Jach 8y ago'Peaker pointed out that the GCC optimization involved in the factorial example is actually quite brittle: https://news.ycombinator.com/item?id=15698924 https://news.ycombinator.com/item?id=15698924
- pflanze 8y agoBut the optimization there is rewriting the algorithm (which is 'brittle' because it depends on the operation used being commutative in that example). Just optimizing tail calls as written in the programmer's code (which is what is normally meant with "TCO") don't need such rewriting, and would be trivial to implement via a jump if the back-end supports that, except C also needs to take into consideration structures on the stack, and perhaps other details, hence apparently gcc's handling isn't straight-forward even for plain TCO and fails to be applied in all applicable cases (or at least that's what happened ~4 years ago).