News for hackers. Stuff that matters.
Re:Misconceptions about loops in C by suprjami on Thursday June 27, 2024 @05:54PM (#40815583)
In my experience, modern optimising compilers can turn many naive loops into tail recursive loops.

Actually learning tail recursion and writing loops for it is a lot less necessary today than it was in the 1980s. It isn't necessary to teach it to beginners anymore because the compiler largely does the heavy lifting for us.

These days, tail recursion is a very advanced performance tuning trick, only used when you've positively identified your compiler version's implementation of your loop as the cause of slowness and you're willing to incur the increased code complexity for the increased performance.

Re:Misconceptions about loops in C by guntars on Thursday June 27, 2024 @07:45PM (#40816411)
How can tail recursion have better performance if they both compile to the same machine instruction - a conditional jump?
Re:Misconceptions about loops in C by suprjami on Thursday June 27, 2024 @11:24PM (#40817406)
Because it results in a tail call and you don't have to build a new stack frame.
Re:Misconceptions about loops in C by guntars on Friday June 28, 2024 @01:36AM (#40818043)
A loop didn’t have to build a stack frame in the first place. Once the optimizer has done it’s job, both the TCO optimized tail call and a basic loop will have the same instructions, hence the performance will be the same.
Re:Misconceptions about loops in C by suprjami on Sunday June 30, 2024 @05:48PM (#40840657)
Yes, that's exactly the point I am making.

Modern compilers optimise this for you, exactly as you said.

Back in the 80s compilers didn't do this. There really was a difference between the machine code emitted for a naive loop and a tail call loop.

A lot of advice to rewrite naive loops to do tail calls is based on that 1980s compiler behaviour, not on the optimising 2020s compiler behaviour.