Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tailcall elimination in Mono

Tags:

.net

mono

I have a .NET 4.0 application that makes heavy use of tail recursion (programmed in F#). It runs fine on the .NET VM, but it runs out of stack on Mono-3.0.1.

I've tried running with mono --optimize=tailc but that doesn't seem to change anything.

Is there some way to force mono to eliminate tail calls? Is there some way to increase the maximum stack size in mono?

like image 255
Nikhil Swamy Avatar asked Nov 29 '12 00:11

Nikhil Swamy


People also ask

Why should we try to eliminate tail recursion?

Since each function call consumes both additional space and additional time, the removal of tail recursion by the optimizer increases the performance of a function significantly — and it eliminates the possibility that the recursive function could overflow memory as it tries to remember variable values in each ...

Does F# support tail call optimization?

F# is a functional-first language and its compiler is designed to provide tail-call optimization if possible. The most efficient way is to turn the recursive function into a function with a loop.

Does C# do tail recursion?

Unfortunately, the C# compiler doesn't support tail recursion, which is a pity, since the CLR supports it.


1 Answers

There is a very similar more recent question with helpful answers: Stack size under Mono

As Robert Harvey already pointed out in the comments, Mono has broken/incomplete tail call support for F# (bug 476785: Tail call support in F#). Fix for this problem is on Mono's runtime ongoing projects list.

Stack size can be increased either using System.Threading.Thread constructor or in the PE header. The System.Threading.Thread way does not work in Mono, though. For changing the PE, you'll probably need MS Visual Studio, since there may be no alternative to its editbin.exe /stack.

like image 60
Palec Avatar answered Oct 07 '22 18:10

Palec