From 882a10a89ff127518fabb0a2e643ba691d1824e7 Mon Sep 17 00:00:00 2001 From: Matt Dziuban Date: Sat, 7 Jun 2025 23:39:22 -0400 Subject: [PATCH 1/2] Ensure caching uses weak references for both keys and values --- .../lmcoursier/internal/SbtUpdateReport.scala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala b/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala index e7fdf9c86..d97492911 100644 --- a/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala +++ b/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala @@ -1,8 +1,8 @@ package lmcoursier.internal import java.io.File -import java.util.GregorianCalendar -import java.util.concurrent.ConcurrentHashMap +import java.lang.ref.WeakReference +import java.util.{ Collections, GregorianCalendar, WeakHashMap } import coursier.cache.CacheUrl import coursier.{ Attributes, Dependency, Module, Project, Resolution } import coursier.core.{ Classifier, Configuration, Extension, Info, Publication, Type } @@ -18,16 +18,16 @@ private[internal] object SbtUpdateReport { private def caching[K, V](f: K => V): K => V = { - val cache = new ConcurrentHashMap[K, V] + val cache = Collections.synchronizedMap(new WeakHashMap[K, WeakReference[V]]) key => val previousValueOpt = Option(cache.get(key)) - previousValueOpt.getOrElse { + previousValueOpt.fold { val value = f(key) - val concurrentValueOpt = Option(cache.putIfAbsent(key, value)) - concurrentValueOpt.getOrElse(value) - } + val concurrentValueOpt = Option(cache.putIfAbsent(key, new WeakReference(value))) + concurrentValueOpt.fold(value)(_.get()) + }(_.get()) } private def infoProperties(project: Project): Seq[(String, String)] = From 8b0bcc6fcf5b5d3dcab08a27b4a0b627aa1afea1 Mon Sep 17 00:00:00 2001 From: Matt Dziuban Date: Sat, 7 Jun 2025 23:44:29 -0400 Subject: [PATCH 2/2] Revert use of WeakReferences for SbtUpdateReport cached values --- .../scala/lmcoursier/internal/SbtUpdateReport.scala | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala b/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala index d97492911..649e24da0 100644 --- a/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala +++ b/lm-coursier/src/main/scala/lmcoursier/internal/SbtUpdateReport.scala @@ -1,7 +1,6 @@ package lmcoursier.internal import java.io.File -import java.lang.ref.WeakReference import java.util.{ Collections, GregorianCalendar, WeakHashMap } import coursier.cache.CacheUrl import coursier.{ Attributes, Dependency, Module, Project, Resolution } @@ -18,16 +17,16 @@ private[internal] object SbtUpdateReport { private def caching[K, V](f: K => V): K => V = { - val cache = Collections.synchronizedMap(new WeakHashMap[K, WeakReference[V]]) + val cache = Collections.synchronizedMap(new WeakHashMap[K, V]) key => val previousValueOpt = Option(cache.get(key)) - previousValueOpt.fold { + previousValueOpt.getOrElse { val value = f(key) - val concurrentValueOpt = Option(cache.putIfAbsent(key, new WeakReference(value))) - concurrentValueOpt.fold(value)(_.get()) - }(_.get()) + val concurrentValueOpt = Option(cache.putIfAbsent(key, value)) + concurrentValueOpt.getOrElse(value) + } } private def infoProperties(project: Project): Seq[(String, String)] =