I have one FunSuite of unit tests testing a highly recursive (non-tail) Scala function. If I add the line below to my pom.xml Surefire <configuration>
it runs twice as fast.
<argLine>-Xss1024k</argLine>
It doesn't matter what value I specify, except that if I specify a really low value like -Xss256k I get the expected StackOverflowException. Otherwise, I can set it anywhere from 512k up to 512m and the execution time is all the same. But then if I delete the line entirely from pom.xml the execution time doubles.
Why could that be?
JVM specification states,
Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.
This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java Virtual Machine stacks are of a fixed size, the size of each Java Virtual Machine stack may be chosen independently when that stack is created.
I think the jvm You are using supports dynamically expanding stacks. When chain of calls gets bigger than the default initial stack size, stack frames will be allocated from the heap. This will cause the execution to be slowed down.
And the case You specify the stack size with parameter, stack maybe work on fixed size mode. In this case all the space will be allocated before hand and no new allocations will be required. Also when You give the low stack size it does not expand and throws StackOverflow, this also shows it is working on fixed size mode.
Since You didn't mention JVM implementation You are using, this is just my assumption based on the info from the JVM specs and Your use case.
https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.2