8000 Refactor: Introduce common caching utilities. by sjrd · Pull Request #5098 · scala-js/scala-js · GitHub
[go: up one dir, main page]

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 lo 10000 ad files.
Loading
Diff view
Diff view
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
0