From 97d81712ff46f42e077b717fc1723b13c678f215 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 22 Nov 2023 15:55:21 +0100 Subject: [PATCH] Cache stable TASTyVersion Co-authored-by: Nicolas Stucki Co-authored-by: Jamie Thompson --- .../src/dotty/tools/tasty/TastyVersion.scala | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tasty/src/dotty/tools/tasty/TastyVersion.scala b/tasty/src/dotty/tools/tasty/TastyVersion.scala index 0d2f1b8c76d8..b6474f7c7934 100644 --- a/tasty/src/dotty/tools/tasty/TastyVersion.scala +++ b/tasty/src/dotty/tools/tasty/TastyVersion.scala @@ -1,6 +1,8 @@ package dotty.tools.tasty -case class TastyVersion(major: Int, minor: Int, experimental: Int) { +import scala.annotation.internal.sharable + +case class TastyVersion private(major: Int, minor: Int, experimental: Int) { def isExperimental: Boolean = experimental > 0 def nextStable: TastyVersion = copy(experimental = 0) @@ -21,4 +23,17 @@ case class TastyVersion(major: Int, minor: Int, experimental: Int) { val extra = Option.when(experimental > 0)(this) s"stable TASTy from ${min.show} to ${max.show}${extra.fold("")(e => s", or exactly ${e.show}")}" } -} \ No newline at end of file +} + +object TastyVersion { + + @sharable + private val cache: java.util.concurrent.ConcurrentHashMap[TastyVersion, TastyVersion] = + new java.util.concurrent.ConcurrentHashMap() + + def apply(major: Int, minor: Int, experimental: Int): TastyVersion = { + val version = new TastyVersion(major, minor, experimental) + val cachedVersion = cache.putIfAbsent(version, version) + if (cachedVersion == null) version else cachedVersion + } +}