Skip to content

Commit

Permalink
Merge pull request #2262 from dotty-staging/fix/sbt-warm-compile
Browse files Browse the repository at this point in the history
Make warm compilation speed with sbt 2x faster
  • Loading branch information
DarkDimius authored Apr 19, 2017
2 parents 2efb6dd + 934ab5b commit 4623e12
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion sbt-bridge/src/xsbt/CompilerClassLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -70,7 +80,11 @@ 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 = synchronized {
fixedLoaderCache.getOrElseUpdate(bridgeLoader, computeFixedLoader(bridgeLoader))
}

private[this] def computeFixedLoader(bridgeLoader: ClassLoader) = bridgeLoader match {
case bridgeLoader: URLClassLoader =>
val dualLoader = bridgeLoader.getParent
val dualLoaderClass = dualLoader.getClass
Expand Down

0 comments on commit 4623e12

Please sign in to comment.