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

Skip to content

Commit 784a85d

Browse files
committed
Refactor: 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 76f7be7 commit 784a85d

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
*
@@ -185,7 +186,8 @@ private object BasicLinkerBackend {
185186
private var _footerBytesCache: Array[Byte] = null
186187
private var _headerNewLineCountCache: Int = 0
187188

188-
private val modules = new java.util.concurrent.ConcurrentHashMap[ModuleID, PrintedModuleCache]
189+
private val modules: ConcurrentCacheMap[ModuleID, PrintedModuleCache] =
190+
key => new PrintedModuleCache
189191

190192
def updateGlobal(header: String, footer: String): Boolean = {
191193
if (header == lastHeader && footer == lastFooter) {
@@ -204,33 +206,17 @@ private object BasicLinkerBackend {
204206
def footerBytes: Array[Byte] = _footerBytesCache
205207
def headerNewLineCount: Int = _headerNewLineCountCache
206208

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

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

224-
private sealed class PrintedModuleCache {
225-
private var cacheUsed = false
226-
216+
private final class PrintedModuleCache extends Cache {
227217
private var previousFinalJSFileSize: Int = 0
228218
private var previousFinalSourceMapSize: Int = 0
229219

230-
def startRun(): Unit = {
231-
cacheUsed = true
232-
}
233-
234220
def getPreviousFinalJSFileSize(): Int = previousFinalJSFileSize
235221

236222
def getPreviousFinalSourceMapSize(): Int = previousFinalSourceMapSize
@@ -239,11 +225,5 @@ private object BasicLinkerBackend {
239225
previousFinalJSFileSize = finalJSFileSize
240226
previousFinalSourceMapSize = finalSourceMapSize
241227
}
242-
243-
def cleanAfterRun(): Boolean = {
244-
val wasUsed = cacheUsed
245-
cacheUsed = false
246-
wasUsed
247-
}
248228
}
249229
}

0 commit comments

Comments
 (0)
0