Skip to content

Commit

Permalink
Take advantage of SBT 0.13.13's compiler interface classloader cache
Browse files Browse the repository at this point in the history
As SBT itself does:

  sbt/sbt#2754

Fixes typesafehub#105
  • Loading branch information
retronym committed Feb 17, 2017
1 parent e6e2a99 commit ca475f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/scala/com/typesafe/zinc/ClassLoaderCacheAccess.java
@@ -0,0 +1,20 @@
package com.typesafe.zinc;

import sbt.classpath.ClassLoaderCache;
import sbt.compiler.AnalyzingCompiler;

import java.net.URL;
import java.net.URLClassLoader;

// Subvert the private[sbt] on sbt.classpath.ClassLoaderCache by using javac.
final class ClassLoaderCacheAccess {
private ClassLoaderCacheAccess() {
}

static AnalyzingCompiler withClassLoaderCache(AnalyzingCompiler compiler, Object cache) {
return compiler.withClassLoaderCache((ClassLoaderCache) cache);
}
static Object newClassLoaderCache() {
return new ClassLoaderCache(new URLClassLoader(new URL[]{}));
}
}
8 changes: 7 additions & 1 deletion src/main/scala/com/typesafe/zinc/Compiler.scala
Expand Up @@ -33,6 +33,8 @@ object Compiler {
*/
val analysisCache = Cache[FileFPrint, Option[(Analysis, CompileSetup)]](Setup.Defaults.analysisCacheLimit)

private val classLoaderCache: /*ClassLoaderCache*/ Any = ClassLoaderCacheAccess.newClassLoaderCache()

/**
* Get or create a zinc compiler based on compiler setup.
*/
Expand Down Expand Up @@ -60,7 +62,11 @@ object Compiler {
* Create a new scala compiler.
*/
def newScalaCompiler(instance: ScalaInstance, interfaceJar: File, log: Logger): AnalyzingCompiler = {
IC.newScalaCompiler(instance, interfaceJar, ClasspathOptions.boot, log)
val compiler = IC.newScalaCompiler(instance, interfaceJar, ClasspathOptions.boot, log)
// This cache, new in SBT 0.13.13, avoids repeatedly classloading the compiler interface's CallbackGlobal,
// which was bad for performance. The layer of compiler caching here in Zinc probably mitigated that ill effect,
// so long as the caller needed fewer than `Setup.Defaults.compilerCacheLimit` compilers at a time.
ClassLoaderCacheAccess.withClassLoaderCache(compiler, classLoaderCache)
}

/**
Expand Down

0 comments on commit ca475f4

Please sign in to comment.