-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package xsbt | |
|
||
import java.net.{URL, URLClassLoader} | ||
|
||
import scala.collection.mutable | ||
|
||
/** A classloader to run the compiler | ||
* | ||
* A CompilerClassLoader is constructed from a list of `urls` that need to be on | ||
|
@@ -42,6 +44,14 @@ class CompilerClassLoader(urls: Array[URL], sbtLoader: ClassLoader) | |
} | ||
|
||
object CompilerClassLoader { | ||
/** Cache the result of `fixBridgeLoader`. | ||
* | ||
* Reusing ClassLoaders is important for warm performance since otherwise the | ||
* 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] | ||
|
||
/** Fix the compiler bridge ClassLoader | ||
* | ||
* Soundtrack: https://www.youtube.com/watch?v=imamcajBEJs | ||
|
@@ -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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. You're right, this method should be synchronized. I don't think |
||
|
||
private[this] def computeFixedLoader(bridgeLoader: ClassLoader) = bridgeLoader match { | ||
case bridgeLoader: URLClassLoader => | ||
val dualLoader = bridgeLoader.getParent | ||
val dualLoaderClass = dualLoader.getClass | ||
|
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.