8000 Introduce common caching utilities. · scala-js/scala-js@3f76772 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f76772

Browse files
committed
Introduce common caching utilities.
Previously, we had several reimplementations of the same basic caching mechanisms. In particular, `cleanAfterRun()`-based removal of caches not used in a given run. In this commit, we introduce common caching utilities. The provide common implementations of the various idioms that we use. This simplifies all the use sites, which can now focus on their core logic, instead of mixing it with caching mechanisms. The abstraction is not zero-cost everywhere. It may introduce some constant overhead.
1 parent 8782649 commit 3f76772

15 files changed

+530
-218
lines changed

linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.scalajs.linker.standard.ModuleSet.ModuleID
2828

2929
import org.scalajs.linker.backend.emitter.Emitter
3030
import org.scalajs.linker.backend.javascript.{ByteArrayWriter, Printers, SourceMapWriter, Trees => js}
31+
import org.scalajs.linker.caching._
3132

3233
/** The basic backend for the Scala.js linker.
3334
*
@@ -187,7 +188,8 @@ private object BasicLinkerBackend {
187188
private var _footerBytesCache: Array[Byte] = null
188189
private var _headerNewLineCountCache: Int = 0
189190

190-
private val modules = new java.util.concurrent.ConcurrentHashMap[ModuleID, PrintedModuleCache]
191+
private val modules: ConcurrentCacheMap[ModuleID, PrintedModuleCache] =
192+
key => new PrintedModuleCache
191193

192194
def updateGlobal(header: String, footer: String): Boolean = {
193195
if (header == lastHeader && footer == lastFooter) {
@@ -206,33 +208,17 @@ private object BasicLinkerBackend {
206208
def footerBytes: Array[Byte] = _footerBytesCache
207209
def headerNewLineCount: Int = _headerNewLineCountCache
208210

209-
def getModuleCache(moduleID: ModuleID): PrintedModuleCache = {
210-
val result = modules.computeIfAbsent(moduleID, _ => new PrintedModuleCache)
211-
result.startRun()
212-
result
213-
}
211+
def getModuleCache(moduleID: ModuleID): PrintedModuleCache =
212+
modules.get(moduleID)
214213

215-
def cleanAfterRun(): Unit = {
216-
val iter = modules.entrySet().iterator()
217-
while (iter.hasNext()) {
218-
val moduleCache = iter.next().getValue()
219-
if (!moduleCache.cleanAfterRun()) {
220-
iter.remove()
221-
}
222-
}
223-
}
214+
def cleanAfterRun(): Unit =
215+
modules.cleanAfterRun()
224216
}
225217

226-
private sealed class PrintedModuleCache {
227-
private var cacheUsed = false
228-
218+
private final class PrintedModuleCache extends Cache {
229219
private var previousFinalJSFileSize: Int = 0
230220
private var previousFinalSourceMapSize: Int = 0
231221

232-
def startRun(): Unit = {
233-
cacheUsed = true
234-
}
235-
236222
def getPreviousFinalJSFileSize(): Int = previousFinalJSFileSize
237223

238224
def getPreviousFinalSourceMapSize(): Int = previousFinalSourceMapSize
@@ -241,11 +227,5 @@ private object BasicLinkerBackend {
241227
previousFinalJSFileSize = finalJSFileSize
242228
previousFinalSourceMapSize = finalSourceMapSize
243229
}
244-
245-
def cleanAfterRun(): Boolean = {
246-
val wasUsed = cacheUsed
247-
cacheUsed = false
248-
wasUsed
249-
}
250230
}
251231
}

0 commit comments

Comments
 (0)
0