Skip to content
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

Merged
merged 2 commits into from
Apr 19, 2017

Conversation

smarter
Copy link
Member

@smarter smarter commented Apr 14, 2017

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).

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`).
Copy link
Contributor

@DarkDimius DarkDimius left a 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]
Copy link
Contributor

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.

Copy link
Member Author

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."

Copy link
Contributor

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))
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Member Author

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.
@DarkDimius DarkDimius merged commit 4623e12 into scala:master Apr 19, 2017
@allanrenucci allanrenucci deleted the fix/sbt-warm-compile branch December 14, 2017 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants