jdk: use parallel old gc and disable compact strings

When switching to JDK9 we regressed on java build performance by about ~30%.
We found that when using parallel gc instead of G1 and disabling compact
strings for JavaBuilder, the java build performance is "only" 10% slower.

We additionally found JDK9 to have a significantly higher startup time.
This impacts the performance of tools like javac and tubine and we
believe that this accounts for most of the remaining overhead that can't
be explained by disabling G1 and compact strings.

java8 -version: 80ms
java9 -version: 140ms
java10 -version: 110ms

Additionally, we found that the number of modules shipped with the JDK
have a direct effect on the startup time. When building Java 10 with only
the 9 modules required by Bazel we find that the startup time reduces to
80ms (from 110ms) which is on par with Java 8.

We thus expect the regression to be fixed by a future migration to Java 10,
which should be done in one of the next Bazel releases.

== Some benchmark results ==
https://github.com/google/protobuf
$ bazel build :protobuf_java
Bazel 0.15.2: 4.2s
Bazel 0.16.0-rc2: 5.2s
This Change: 4.2s

https://github.com/jin/android-projects/tree/master/java_only
$ bazel build :module0
Bazel 0.15.2: 8.2s
Bazel 0.16.0-rc2: 11.5s
This Change: 9.1s

RELNOTES: None
PiperOrigin-RevId: 205647957
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 09760fc..a99931b 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -420,8 +420,12 @@
 
   // TODO(b/109998449): only assume JDK >= 9 for embedded JDKs
   if (!globals->options->GetEmbeddedJavabase().empty()) {
-    // quiet warnings from com.google.protobuf.UnsafeUtil,
+    // In JDK9 we have seen a slow down when using the default G1 collector
+    // and thus switch back to parallel gc.
+    result.push_back("-XX:+UseParallelOldGC");
     // see: https://github.com/google/protobuf/issues/3781
+
+    // quiet warnings from com.google.protobuf.UnsafeUtil,
     result.push_back("--add-opens=java.base/java.nio=ALL-UNNAMED");
   }
 
diff --git a/tools/jdk/default_java_toolchain.bzl b/tools/jdk/default_java_toolchain.bzl
index f4dc035..c518d026 100644
--- a/tools/jdk/default_java_toolchain.bzl
+++ b/tools/jdk/default_java_toolchain.bzl
@@ -19,6 +19,10 @@
 ]
 
 JDK9_JVM_OPTS = [
+    # In JDK9 we have seen a ~30% slow down in JavaBuilder performance when using
+    # G1 collector and having compact strings enabled.
+    "-XX:+UseParallelOldGC",
+    "-XX:-CompactStrings",
     # Allow JavaBuilder to access internal javac APIs.
     "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
     "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",