Skip to content

Commit c81ac64

Browse files
authored
Use SoftReference instead of WeakReference for caches (com-lihaoyi#1966)
Pull request: com-lihaoyi#1966
1 parent 1e025cd commit c81ac64

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

scalajslib/worker/0.6/src/ScalaJSWorkerImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.scalajs.jsenv._
2222
import org.scalajs.testadapter.TestAdapter
2323

2424
import scala.collection.mutable
25-
import scala.ref.WeakReference
25+
import scala.ref.SoftReference
2626

2727
class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
2828
private case class LinkerInput(
@@ -32,12 +32,12 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
3232
esFeatures: ESFeatures
3333
)
3434
private object ScalaJSLinker {
35-
private val cache = mutable.Map.empty[LinkerInput, WeakReference[ClearableLinker]]
35+
private val cache = mutable.Map.empty[LinkerInput, SoftReference[ClearableLinker]]
3636
def reuseOrCreate(input: LinkerInput): ClearableLinker = cache.get(input) match {
37-
case Some(WeakReference(linker)) => linker
37+
case Some(SoftReference(linker)) => linker
3838
case _ =>
3939
val newLinker = createLinker(input)
40-
cache.update(input, WeakReference(newLinker))
40+
cache.update(input, SoftReference(newLinker))
4141
newLinker
4242
}
4343
private def createLinker(input: LinkerInput): ClearableLinker = {

scalajslib/worker/1/src/ScalaJSWorkerImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import org.scalajs.testing.adapter.TestAdapter
2727
import org.scalajs.testing.adapter.{TestAdapterInitializer => TAI}
2828

2929
import scala.collection.mutable
30-
import scala.ref.WeakReference
30+
import scala.ref.SoftReference
3131

3232
class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
3333
private case class LinkerInput(
@@ -43,12 +43,12 @@ class ScalaJSWorkerImpl extends ScalaJSWorkerApi {
4343
case _ => true
4444
}
4545
private object ScalaJSLinker {
46-
private val cache = mutable.Map.empty[LinkerInput, WeakReference[Linker]]
46+
private val cache = mutable.Map.empty[LinkerInput, SoftReference[Linker]]
4747
def reuseOrCreate(input: LinkerInput): Linker = cache.get(input) match {
48-
case Some(WeakReference(linker)) => linker
48+
case Some(SoftReference(linker)) => linker
4949
case _ =>
5050
val newLinker = createLinker(input)
51-
cache.update(input, WeakReference(newLinker))
51+
cache.update(input, SoftReference(newLinker))
5252
newLinker
5353
}
5454
private def createLinker(input: LinkerInput): Linker = {

scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.io.File
44
import java.util.Optional
55
import java.util.concurrent.ConcurrentHashMap
66

7-
import scala.ref.WeakReference
7+
import scala.ref.SoftReference
88
import scala.util.Properties.isWin
99

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

334334
def getCachedClassLoader(compilersSig: Long, combinedCompilerJars: Array[java.io.File])(implicit
335335
ctx: ZincWorkerApi.Ctx
336336
) = {
337337
classloaderCache.synchronized {
338338
classloaderCache.get(compilersSig) match {
339-
case Some(WeakReference(cl)) => cl
339+
case Some(SoftReference(cl)) => cl
340340
case _ =>
341341
// the Scala compiler must load the `xsbti.*` classes from the same loader than `ZincWorkerImpl`
342342
val sharedPrefixes = Seq("xsbti")
@@ -346,7 +346,7 @@ class ZincWorkerImpl(
346346
sharedLoader = getClass.getClassLoader,
347347
sharedPrefixes
348348
)
349-
classloaderCache.update(compilersSig, WeakReference(cl))
349+
classloaderCache.update(compilersSig, SoftReference(cl))
350350
cl
351351
}
352352
}

0 commit comments

Comments
 (0)