Why GPars runForkJoin is slow even when there are no forkOffChild()?

220 Views Asked by At

This is the sequential version:

void f(long n) {
   for (int i=1; i<n-1; i++) {
      // do nothing     
   }
}

List result = []
(1..99999).each {
   f(it)
   result << it
}

It takes a few seconds to run the code.

void f(long n) {
   for (int i=1; i<n-1; i++) {
      // do nothing
   }
}

withPool {
   runForkJoin(1,99999) { a, b ->
      List result = []
      (a..b).each {
         f(it)
         result << it
      }
      return result
   }
}

The code above takes several minutes to finish. I haven't call any forkOffChild() or childrenResults() yet. I'm running this code in Windows and single core CPU with Intel Hyperthreading (2 logical CPU). Java Runtime.runtime.availableProcessors() returns 2.

I don't understand why the code that uses runForkJoin much slower than the sequential code (minutes versus seconds).

1

There are 1 best solutions below

1
On BEST ANSWER

The runForJoin() method has no impact on performance in the code snippet. It is the withPool() method, that causes the slowdown. This is because the withPool() method adds several xxxParallel() methods to the Groovy Object's dynamic scope, which then slows down method resolution.

Annotating the f() method as @CompileStatic will give you the expected performance.