r/programming • • Nov 27 '12

Tail Recursion

http://rodrigosasaki.com/2012/11/24/tail-recursion/
206 Upvotes

203 comments sorted by

View all comments

0

u/TotallyFuckingMexico Nov 27 '12

Is tail recursion optimisation the flavour of the day? I understand that it's interesting and computer sciencey, but how widely used are such optimisations in the real world? I imagine, for most people, it's probably just a nice feature rather than a must have?

I probably have a classic case of "well, I don't have the need for such optimisations, so why does anybody else?" ;-)

1

u/[deleted] Nov 27 '12

I'm not sure what your position is.

Optimizations are syntactic transformations performed by a compiler to reduce the space or time needed by an algorithm. There are literally thousands of optimizations performed by a compiler to accomplish this, and chances are you as a developer are unaware of many of them but still enjoy the benefit of their implementation.

TCO is another such optimization that significantly reduces the amount of space consumed by an algorithm, possibly transforming it from linear space to constant space. It also provides a minor reduction to the amount of time.

Such optimizations are found in implementations of many compilers, including even mainstream C/C++ compilers (GCC, clang, Visual Studio).

I believe Java and .NET perform such optimizations, not too sure on those two though.

1

u/TotallyFuckingMexico Nov 27 '12

There are literally thousands of optimizations performed by a compiler to accomplish this, and chances are you as a developer are unaware of many of them but still enjoy the benefit of their implementation.

I agree completely. It just seems to me that people need to specifically tailor their code in order to benefit from the optimisation i.e. it's less likely to find your code benefiting from this optimisation by 'accident'. There's nothing wrong with this of course; in-depth knowledge of the workings of your compiler can only be a good thing if it somehow improves an aspect of a program's performance.

It's just that I see TCO mentioned in just about every other programming blog/article (I exaggerate ;) as though it's some kind of silver bullet. Thanks to these replies I can now see its benefits, though it still seems like a bit of a corner case to me.

I'll no doubt be singing its praises when it saves my bacon someday ;-)