I know that multiple modules can be compiled using multiple threads where each thread compiles a single module but what if I have a single large module? Does Javac or the Eclipse Java Compiler support compiling single modules in parallel (using many threads)? Or is there any other Java compiler which supports it?
Update: I created a Java source file with ~50k simple methods (just for the purpose of this test) such as:
static int add1(int a, int b, int c) {
return 2 * a + 55 * b - c;
}
static int add2(int a, int b, int c) {
return 2 * a + 55 * b - c;
}
static int add3(int a, int b, int c) {
return 2 * a + 55 * b - c;
}
These methods do not depend on each other so compilation could be done in parallel (at least in theory). Compiling this file with Javac
on my 12 core + HT machine lead to an average 20% CPU usage with a really short spike of up to 50%. This leads me to believe that although there is some parralelization done inside Javac
, it is really minor.
The interesting thing is that if I create 2, 3 or 4 classes with the same number of methods and compile them at the same time with a single Javac
process, I cannot get a higher CPU usage. The compilation takes exactly 2x, 3x, 4x longer which shows that Javac doesn't compile these totally unrelated classes in parallel. But if I start separate Javac processes to compile these files separately, the CPU jumps to almost 100% when 4 files(=Javac
processes) are used and the compilation time is just 5-10% higher than compiling a single file (compared to this, a single Javac
process compiling all these 4 files, the compilation takes 400% longer).
So my opinion is that Javac
does compile files using multiple threads but it is kind of limited to ~4 threads, it cannot fully utilize a 12 cores machine. Also to me it seems that Javac
compiles multiple files in serial, it only uses cores/threads to compile a single file in parallel(I believe that when a single file is compiled, some parts can be done in parallel and this is what Javac does, but what about compiling multiple files in parallel? If I have 100 files which are independent I should be able to see my CPU jump to 100% which is not the case.)
maven
supports parallel build cwiki.apache.org/confluence/display/MAVEN/… – ConsubstantiateJavac
could do better, like it could compile files in multiple steps and during a very fast first step it could already know that the file is independent. – IsochromaticHashSet
still uses aHashMap
under the hood (I can make more examples like this). The idea is that such optimizations might not ever be a priority to the JDK team. Unless a VERY brave and smart enthusiast wants to help with a PR (which will never happen) – Intramundane