4

After participating in the debate in this fiascoquestion, I would like to posit the question before the entire community.

In what scenarios will tail-call optimization be applied to .Net-based code?

Please support your answers with reliable, up-to-date sources or reproducible experiments.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    What exactly are you looking for? The truth or the most popular answer? Only the latter is available here. – Hans Passant Feb 19 '10 at 01:03
  • His request for evidence ("reliable, up-to-date sources or reproducible experiments") would seem to imply that he's looking for "The truth". Regardless of how you feel about SO, his question is valid since the .Net compiler and CLR are, in fact, deterministic. Whether anyone will be able to uncover that truth is still up in the air. – jball Feb 19 '10 at 01:24
  • Seems very localized to me. Different versions of the compiler may see different The Truth. Certainly different compilers will. And I take issue with the title: nothing that is local to one compiler can every be "The Definitive Question" on anything. – dmckee --- ex-moderator kitten Feb 19 '10 at 01:30
  • @dmckee, it's tagged ".net" and titles always exist in conjunction with tags. It has to be this way, otherwise the titles would be a mess of keywords and the tags would be redundant and borderline useless. – jball Feb 19 '10 at 01:51
  • @dmckee well when it can be applied is independent of the compiler for when it's actually applied well that would deffinately depend on the compiler and there's quite a few of them in the .NET world and some do tail call optimization and some don't. – Rune FS Feb 19 '10 at 04:47
  • @Rune: That's part of the question. (At least for the mainstream compilers) – SLaks Feb 19 '10 at 13:59

1 Answers1

4

According to "Expert F#" written by Don Syme et al., F# does tail call optimization. I seem to remember reading on Eric Lippert's blog that the C# compiler (any version) does not. Correct me if I'm wrong, Eric. In all cases tail-call optimizations can be done when the last instruction is to call a method. This will often be a recursive call of the method itself but does not need to be. The optimization can be done since it's guaranteed that the current stack frame is no longer needed. However if just a simple operation has to be performed afterwards the optimization cannot be performed.

int Fib(int n)
{
  if(n < 2)
     return 1;

  return Fib(n-1) + Fib(n-2);
}

This cannot be tail call optimized because + cannot be evaluated before the last call to Fib returns. (Actually I think this is the example used in Expert F# as well but not sure on that one.)

int Fib(int n, int a, int b)
{
  if(n == 0)
    return a+b;
  return Fib(n-1,a+b,a);
}

This version can be tail call optimized since all the arguments are evaluated before the last call to Fib and no operations exist to be performed after the call so the current stack frame can be discarded.

Boann
  • 48,794
  • 16
  • 117
  • 146
Rune FS
  • 21,497
  • 7
  • 62
  • 96