Friday, August 30, 2019

Which, if any, C++ compilers do tail-recursion optimization?



It seems to me that it would work perfectly well to do tail-recursion optimization in both C and C++, yet while debugging I never seem to see a frame stack that indicates this optimization. That is kind of good, because the stack tells me how deep the recursion is. However, the optimization would be kind of nice as well.



Do any C++ compilers do this optimization? Why? Why not?




How do I go about telling the compiler to do it?




  • For MSVC: /O2 or /Ox

  • For GCC: -O2 or -O3



How about checking if the compiler has done this in a certain case?





  • For MSVC, enable PDB output to be able to trace the code, then inspect the code

  • For GCC..?



I'd still take suggestions for how to determine if a certain function is optimized like this by the compiler (even though I find it reassuring that Konrad tells me to assume it)



It is always possible to check if the compiler does this at all by making an infinite recursion and checking if it results in an infinite loop or a (I did this with GCC and found out that -O2 is sufficient), but I want to be able to check a certain function that I know will terminate anyway. I'd love to have an easy way of checking this :)







After some testing, I discovered that destructors ruin the possibility of making this optimization. It can sometimes be worth it to change the scoping of certain variables and temporaries to make sure they go out of scope before the return-statement starts.



If any destructor needs to be run after the tail-call, the tail-call optimization can not be done.


Answer



All current mainstream compilers perform tail call optimisation fairly well (and have done for more than a decade), even for mutually recursive calls such as:



int bar(int, int);

int foo(int n, int acc) {

return (n == 0) ? acc : bar(n - 1, acc + 2);
}

int bar(int n, int acc) {
return (n == 0) ? acc : foo(n - 1, acc + 1);
}


Letting the compiler do the optimisation is straightforward: Just switch on optimisation for speed:





  • For MSVC, use /O2 or /Ox.

  • For GCC, Clang and ICC, use -O3



An easy way to check if the compiler did the optimisation is to perform a call that would otherwise result in a — or looking at the assembly output.



As an interesting historical note, tail call optimisation for C was added to the GCC in the course of a diploma thesis by Mark Probst. The thesis describes some interesting caveats in the implementation. It's worth reading.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...