From 1701d6cee89b960c390065b3c9f58382933c32c9 Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Tue, 7 Jul 2020 14:56:54 +0200 Subject: [PATCH] use java collections for cache implementation --- .../scalafix/internal/sbt/BlockingCache.scala | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/scala/scalafix/internal/sbt/BlockingCache.scala b/src/main/scala/scalafix/internal/sbt/BlockingCache.scala index 91a08c4c..7d570344 100644 --- a/src/main/scala/scalafix/internal/sbt/BlockingCache.scala +++ b/src/main/scala/scalafix/internal/sbt/BlockingCache.scala @@ -1,21 +1,17 @@ package scalafix.internal.sbt -import scala.collection.mutable +import java.{util => ju} /** A basic thread-safe cache without any eviction. */ class BlockingCache[K, V] { - private val underlying = new mutable.HashMap[K, V] + + // Number of keys is expected to be very small so the global lock should not be a bottleneck + private val underlying = ju.Collections.synchronizedMap(new ju.HashMap[K, V]) /** * @param value By-name parameter evaluated when the key if missing. Value computation is guaranteed * to be called only once per key across all invocations. */ - def getOrElseUpdate(key: K, value: => V): V = { - // ConcurrentHashMap does not guarantee that there is only one evaluation of the value, so - // we use our own (global) locking, which is OK as the number of keys is expected to be - // very small (bound by the number of projects in the sbt build). - underlying.synchronized { - underlying.getOrElseUpdate(key, value) - } - } + def getOrElseUpdate(key: K, value: => V): V = + underlying.computeIfAbsent(key, (_: K) => value) }