Skip to content

Refactor: Introduce common caching utilities. #5098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
sjrd committed Apr 22, 2025
commit 61122d86a26cd132075602c116f78e910d4211a9
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.scalajs.linker.standard.ModuleSet.ModuleID

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

/** The basic backend for the Scala.js linker.
*
Expand Down Expand Up @@ -185,7 +186,8 @@ private object BasicLinkerBackend {
private var _footerBytesCache: Array[Byte] = null
private var _headerNewLineCountCache: Int = 0

private val modules = new java.util.concurrent.ConcurrentHashMap[ModuleID, PrintedModuleCache]
private val modules: ConcurrentCacheMap[ModuleID, PrintedModuleCache] =
key => new PrintedModuleCache

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

def getModuleCache(moduleID: ModuleID): PrintedModuleCache = {
val result = modules.computeIfAbsent(moduleID, _ => new PrintedModuleCache)
result.startRun()
result
}
def getModuleCache(moduleID: ModuleID): PrintedModuleCache =
modules.get(moduleID)

def cleanAfterRun(): Unit = {
val iter = modules.entrySet().iterator()
while (iter.hasNext()) {
val moduleCache = iter.next().getValue()
if (!moduleCache.cleanAfterRun()) {
iter.remove()
}
}
}
def cleanAfterRun(): Unit =
modules.cleanAfterRun()
}

private sealed class PrintedModuleCache {
private var cacheUsed = false

private final class PrintedModuleCache extends Cache {
private var previousFinalJSFileSize: Int = 0
private var previousFinalSourceMapSize: Int = 0

def startRun(): Unit = {
cacheUsed = true
}

def getPreviousFinalJSFileSize(): Int = previousFinalJSFileSize

def getPreviousFinalSourceMapSize(): Int = previousFinalSourceMapSize
Expand All @@ -239,11 +225,5 @@ private object BasicLinkerBackend {
previousFinalJSFileSize = finalJSFileSize
previousFinalSourceMapSize = finalSourceMapSize
}

def cleanAfterRun(): Boolean = {
val wasUsed = cacheUsed
cacheUsed = false
wasUsed
}
}
}
Loading