-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make warm compilation speed with sbt 2x faster #2262
Conversation
Previously, every call to `compile` in sbt with dotty took about the same time because we created a new ClassLoader everytime and thus thrased the JIT code cache, by reusing ClassLoaders we can make `compile` about 2x faster. You can reproduce this by running: > dotty-compiler-bootstrapped/compile This takes ~50 seconds on my machine. Then clean using: > ;dotty-compiler-bootstrapped/clean;dotty-compiler-update And run `dotty-compiler-bootstrapped/compile` again, this takes ~25 seconds for me. I get very similar timings from scalac (replacing `dotty-compiler-bootstrapped` by `dotty-compiler`).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise LGTM
* JIT code cache for the compiler will be discarded between every call to | ||
* the sbt `compile` task. | ||
*/ | ||
private[this] val fixedLoaderCache = new mutable.WeakHashMap[ClassLoader, ClassLoader] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in weak hashMap only values are weak. You are keeping SBT classloaders(keys) from being GC-d.
I'd prefer to have one more level of indirection to also remove this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure? From https://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html: "Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right.
@@ -70,7 +80,10 @@ object CompilerClassLoader { | |||
* @param bridgeLoader The classloader that sbt uses to load the compiler bridge | |||
* @return A fixed classloader that works with dotty | |||
*/ | |||
def fixBridgeLoader(bridgeLoader: ClassLoader) = bridgeLoader match { | |||
def fixBridgeLoader(bridgeLoader: ClassLoader): ClassLoader = | |||
fixedLoaderCache.getOrElseUpdate(bridgeLoader, computeFixedLoader(bridgeLoader)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this method be synchronized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW: CompilerClassLoader.loadClass should definitely be synchronized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, this method should be synchronized. I don't think loadClass
needs to be however because I'm only calling super.loadClass
and resolveClass
which should both be thread-safe. In fact, I think that I should be able to call https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#registerAsParallelCapable-- to make my ClassLoader parallel.
This method could be called from multiple threads since sbt could run multiple `compile` task in parallel.
Previously, every call to
compile
in sbt with dotty took about thesame time because we created a new ClassLoader everytime and thus
thrased the JIT code cache, by reusing ClassLoaders we can make
compile
about 2x faster.You can reproduce this by running:
> dotty-compiler-bootstrapped/compile
This takes ~50 seconds on my machine. Then clean using:
And run
dotty-compiler-bootstrapped/compile
again, this takes ~25seconds for me. I get very similar timings from scalac (replacing
dotty-compiler-bootstrapped
bydotty-compiler
).