Tail Recursion Optimisation in .NET 4

In C#, the tail recursion optimisation only seems to apply when you do a release build for x64.

If you create a console application, which does something like this:

static void Main(string[] args)
{
    Recur();
}
 
static void Recur()
{
    Recur();
}

…and run in debug, it’s doesn’t take very long until this happens:

Process is terminated due to StackOverflowException.

Change the platform target to x64, do a release build, and run it without debugging. It will run forever!

Why does this optimisation only get used under these conditions?

Advertisements