Skip to content

Commit

Permalink
Use SoftReference instead of WeakReference for caches (#1966)
Browse files Browse the repository at this point in the history
Pull request: #1966
  • Loading branch information
lolgab authored Jul 21, 2022
1 parent 1e025cd commit c81ac64
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions scalajslib/worker/0.6/src/ScalaJSWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.scalajs.jsenv._
import org.scalajs.testadapter.TestAdapter

import scala.collection.mutable
import scala.ref.WeakReference
import scala.ref.SoftReference

class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
private case class LinkerInput(
Expand All @@ -32,12 +32,12 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
esFeatures: ESFeatures
)
private object ScalaJSLinker {
private val cache = mutable.Map.empty[LinkerInput, WeakReference[ClearableLinker]]
private val cache = mutable.Map.empty[LinkerInput, SoftReference[ClearableLinker]]
def reuseOrCreate(input: LinkerInput): ClearableLinker = cache.get(input) match {
case Some(WeakReference(linker)) => linker
case Some(SoftReference(linker)) => linker
case _ =>
val newLinker = createLinker(input)
cache.update(input, WeakReference(newLinker))
cache.update(input, SoftReference(newLinker))
newLinker
}
private def createLinker(input: LinkerInput): ClearableLinker = {
Expand Down
8 changes: 4 additions & 4 deletions scalajslib/worker/1/src/ScalaJSWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.scalajs.testing.adapter.TestAdapter
import org.scalajs.testing.adapter.{TestAdapterInitializer => TAI}

import scala.collection.mutable
import scala.ref.WeakReference
import scala.ref.SoftReference

class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
private case class LinkerInput(
Expand All @@ -43,12 +43,12 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
case _ => true
}
private object ScalaJSLinker {
private val cache = mutable.Map.empty[LinkerInput, WeakReference[Linker]]
private val cache = mutable.Map.empty[LinkerInput, SoftReference[Linker]]
def reuseOrCreate(input: LinkerInput): Linker = cache.get(input) match {
case Some(WeakReference(linker)) => linker
case Some(SoftReference(linker)) => linker
case _ =>
val newLinker = createLinker(input)
cache.update(input, WeakReference(newLinker))
cache.update(input, SoftReference(newLinker))
newLinker
}
private def createLinker(input: LinkerInput): Linker = {
Expand Down
8 changes: 4 additions & 4 deletions scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.File
import java.util.Optional
import java.util.concurrent.ConcurrentHashMap

import scala.ref.WeakReference
import scala.ref.SoftReference
import scala.util.Properties.isWin

import mill.api.Loose.Agg
Expand Down Expand Up @@ -329,14 +329,14 @@ class ZincWorkerImpl(
// for now this just grows unbounded; YOLO
// But at least we do not prevent unloading/garbage collecting of classloaders
private[this] val classloaderCache =
collection.mutable.LinkedHashMap.empty[Long, WeakReference[ClassLoader]]
collection.mutable.LinkedHashMap.empty[Long, SoftReference[ClassLoader]]

def getCachedClassLoader(compilersSig: Long, combinedCompilerJars: Array[java.io.File])(implicit
ctx: ZincWorkerApi.Ctx
) = {
classloaderCache.synchronized {
classloaderCache.get(compilersSig) match {
case Some(WeakReference(cl)) => cl
case Some(SoftReference(cl)) => cl
case _ =>
// the Scala compiler must load the `xsbti.*` classes from the same loader than `ZincWorkerImpl`
val sharedPrefixes = Seq("xsbti")
Expand All @@ -346,7 +346,7 @@ class ZincWorkerImpl(
sharedLoader = getClass.getClassLoader,
sharedPrefixes
)
classloaderCache.update(compilersSig, WeakReference(cl))
classloaderCache.update(compilersSig, SoftReference(cl))
cl
}
}
Expand Down

0 comments on commit c81ac64

Please sign in to comment.