Skip to content

Commit d239bb2

Browse files
committed
Move macro classloader cache to top level object.
1 parent b853ede commit d239bb2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/compiler/scala/reflect/macros/runtime/MacroRuntimes.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ trait MacroRuntimes extends JavaReflectionRuntimes {
6060
* a classloader mapped to that virtual directory.
6161
*/
6262
private lazy val defaultMacroClassloaderCache = {
63-
def attemptClose(loader: ClassLoader): Unit = loader match {
64-
case u: URLClassLoader => debuglog("Closing macro runtime classloader"); u.close()
65-
case afcl: AbstractFileClassLoader => attemptClose(afcl.getParent)
66-
case _ => ???
63+
def attemptClose(loader: ClassLoader): Unit = {
64+
if (!scala.tools.nsc.typechecker.Macros.macroClassLoadersCache.owns(loader)) {
65+
loader match {
66+
case u: URLClassLoader => debuglog("Closing macro runtime classloader"); u.close()
67+
case afcl: AbstractFileClassLoader => attemptClose(afcl.getParent)
68+
case _ => ???
69+
}
70+
}
6771
}
6872
perRunCaches.newGeneric(findMacroClassLoader, attemptClose _)
6973
}

src/compiler/scala/tools/nsc/classpath/ZipAndJarFileLookupFactory.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ final class FileBasedCache[T] {
190190
import java.nio.file.Path
191191
private case class Stamp(lastModified: FileTime, fileKey: Object)
192192
private val cache = collection.mutable.Map.empty[Seq[Path], (Seq[Stamp], T)]
193+
def owns(t: T): Boolean = cache.valuesIterator.exists(_._2.asInstanceOf[AnyRef] eq t.asInstanceOf[AnyRef])
193194

194195
def getOrCreate(paths: Seq[Path], create: () => T): T = cache.synchronized {
195196
val stamps = paths.map { path =>

src/compiler/scala/tools/nsc/typechecker/Macros.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
6767

6868
def globalSettings = global.settings
6969

70-
private final val macroClassLoadersCache =
71-
new scala.tools.nsc.classpath.FileBasedCache[ScalaClassLoader]()
72-
7370
/** Obtains a `ClassLoader` instance used for macro expansion.
7471
*
7572
* By default a new `ScalaClassLoader` is created using the classpath
@@ -109,7 +106,7 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
109106
macroLogVerbose(s"macro classloader: caching is disabled because the following paths are not supported: ${nonJarZips.mkString(",")}.")
110107
newLoader()
111108
} else {
112-
macroClassLoadersCache.getOrCreate(locations.map(_.jfile.toPath()), newLoader)
109+
Macros.macroClassLoadersCache.getOrCreate(locations.map(_.jfile.toPath()), newLoader)
113110
}
114111
}
115112
}
@@ -944,6 +941,12 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
944941
}.transform(expandee)
945942
}
946943

944+
object Macros {
945+
final val macroClassLoadersCache =
946+
new scala.tools.nsc.classpath.FileBasedCache[ScalaClassLoader]()
947+
948+
}
949+
947950
trait MacrosStats {
948951
self: TypesStats with Statistics =>
949952
val macroExpandCount = newCounter ("#macro expansions", "typer")

0 commit comments

Comments
 (0)