Skip to content

Commit

Permalink
Move macro classloader cache to top level object.
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Oct 15, 2018
1 parent 24124d5 commit 5175550
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/compiler/scala/reflect/macros/runtime/MacroRuntimes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ trait MacroRuntimes extends JavaReflectionRuntimes {
* a classloader mapped to that virtual directory.
*/
private lazy val defaultMacroClassloaderCache = {
def attemptClose(loader: ClassLoader): Unit = loader match {
case u: URLClassLoader => debuglog("Closing macro runtime classloader"); u.close()
case afcl: AbstractFileClassLoader => attemptClose(afcl.getParent)
case _ => ???
def attemptClose(loader: ClassLoader): Unit = {
if (!scala.tools.nsc.typechecker.Macros.macroClassLoadersCache.owns(loader)) {
loader match {
case u: URLClassLoader => debuglog("Closing macro runtime classloader"); u.close()
case afcl: AbstractFileClassLoader => attemptClose(afcl.getParent)
case _ => ???
}
}
}
perRunCaches.newGeneric(findMacroClassLoader, attemptClose _)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ final class FileBasedCache[T] {
import java.nio.file.Path
private case class Stamp(lastModified: FileTime, fileKey: Object)
private val cache = collection.mutable.Map.empty[Seq[Path], (Seq[Stamp], T)]
def owns(t: T): Boolean = cache.valuesIterator.exists(_._2.asInstanceOf[AnyRef] eq t.asInstanceOf[AnyRef])

def getOrCreate(paths: Seq[Path], create: () => T): T = cache.synchronized {
val stamps = paths.map { path =>
Expand Down
11 changes: 7 additions & 4 deletions src/compiler/scala/tools/nsc/typechecker/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ trait Macros extends MacroRuntimes with Traces with Helpers {

def globalSettings = global.settings

private final val macroClassLoadersCache =
new scala.tools.nsc.classpath.FileBasedCache[ScalaClassLoader]()

/** Obtains a `ClassLoader` instance used for macro expansion.
*
* By default a new `ScalaClassLoader` is created using the classpath
Expand Down Expand Up @@ -109,7 +106,7 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
macroLogVerbose(s"macro classloader: caching is disabled because the following paths are not supported: ${nonJarZips.mkString(",")}.")
newLoader()
} else {
macroClassLoadersCache.getOrCreate(locations.map(_.jfile.toPath()), newLoader)
Macros.macroClassLoadersCache.getOrCreate(locations.map(_.jfile.toPath()), newLoader)
}
}
}
Expand Down Expand Up @@ -944,6 +941,12 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
}.transform(expandee)
}

object Macros {
final val macroClassLoadersCache =
new scala.tools.nsc.classpath.FileBasedCache[ScalaClassLoader]()

}

trait MacrosStats {
self: TypesStats with Statistics =>
val macroExpandCount = newCounter ("#macro expansions", "typer")
Expand Down

0 comments on commit 5175550

Please sign in to comment.