I have written a tiny recursive bit of F# code to see how many levels of recursion I can fit onto the stack under .NET/Mono. It just prints the recursion depth whenever it is an exact power of 2, so I find out the maximum depth to within a factor 2.
I start the code in a thread with a defined amount of stack space using System.Threading.Thread (ThreadStart, int)
. Under .Net it seems to take approx 100 bytes per level of recursion, and I can get about 16 million levels on a 2G stack. The memory usage is broadly similar under Mono, however I can only get about 30 thousand levels. Increasing the stack size value passed to Thread
past over about 600000
does not increase the recursion depth.
ulimit
reports the stack size limit is 1G.
An obvious explanation is that Mono will not obey the second argument of Thread
if it is too large. Does anybody please know how to convince Mono to allocate a large stack?
The code is trivial, but it's below just in case someone cares:
let rec f i =
if popcount i = 1 then // population count is one on exact powers of 2
printf "Got up to %d\n" i
stdout.Flush ()
if i = 1000000000 then 0 else 1 + f (i+1)
Option 1: Change Mono Stack Size
You are correct that Mono will limit the stack size, even if you pass in a large value. For example, on my Cent OS 64-bit test machine, the maximum stack size that Mono will allocate is 2 megabytes. The Mono C# source file Thread.cs shows us what happens when you create a Mono thread:
The code above puts a hard limit on the stack size.
You could in theory change code in one or both of the above functions (bold lines) so that a larger stack size is allocated. Once you did this you would have to build the Mono runtime and then run your function to see if the change makes a difference.
I should stress that I do not know enough about Mono to understand if allocating a larger stack will help in your specific case. I would only do this as a last resort (if neither of my other answers work).
Option 3: Make it Iterative
One option is to rewrite your method so that it is not recursive, but iterative. That is, you change the method so that it is a loop that calculates the results:
For input value 1:
The output:
For input value 999,999,998:
The output:
The code above uses two variables to keep track of progress:
acc
- the accumulater, which stores the result of the calculationcounter
- is simply the number of iterative calls (number of loops)Since we are using a loop to calculate the result, there is not any additional stack allocated.
This code gets around the mono limit. It is useful for dfs in competitive programming.
Option 2: F# Tail calls
One option is to rewrite your method so that you use recursive tail calls. From the prior (Wikipedia) link:
There is a caveat - Mono does not fully support tail recursive calls - what you should do is to test first in the .Net runtime, and then see if the code will run in Mono.
Here is your sample function re-written using a tail recursive call - it does work in both .NET (using Visual Studio 2010) and Mono (Mono version 3.2.0, F# 3.0):
For input value 1:
The output:
For input value 999,999,998:
The output:
The code above uses two variables to keep track of progress:
acc
- the accumulator, which stores the result of the calculationcounter
- is simply the number of recursive callsWhy is your sample code not tail recursive?
Paraphrasing the How To Write Tail-Recursive Functions section of the Wikipedia article, we can rewrite this line of your code:
as
The very definition of tail recursion says that there cannot be additional operations after the recursive call. As we can see in the rewritten version of your code, there are additional operations required to produce the result.
Resources