Skip to content

Commit

Permalink
Use classpath PathRefs hashCode as cache key for Scala.js worker (#…
Browse files Browse the repository at this point in the history
…2183)

This makes the `bridge` method take 15ms instead of 600ms (in my
machine) since it is not needed anymore to perform file system
operations. But we use the already existing `Agg[PathRef]` as the key to
cache Scala.js workers.

Pull request: #2183
  • Loading branch information
lolgab authored Dec 10, 2022
1 parent d17f020 commit 5234477
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions scalajslib/src/mill/scalajslib/ScalaJSModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
case Left(err) => Result.Failure(err)
case Right(_) =>
ScalaJSWorkerExternalModule.scalaJSWorker().run(
scalaJSToolsClasspath().map(_.path),
scalaJSToolsClasspath(),
jsEnvConfig(),
fastLinkJS()
)
Expand Down Expand Up @@ -173,7 +173,7 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
.filter(_.ext == "sjsir")
val libraries = classpath.filter(_.ext == "jar")
worker.link(
toolsClasspath.map(_.path),
toolsClasspath,
sjsirFiles,
libraries,
outputPath.toIO,
Expand Down Expand Up @@ -288,7 +288,7 @@ trait TestScalaJSModule extends ScalaJSModule with TestModule {
): Task[(String, Seq[TestRunner.Result])] = T.task {

val (close, framework) = ScalaJSWorkerExternalModule.scalaJSWorker().getFramework(
scalaJSToolsClasspath().map(_.path),
scalaJSToolsClasspath(),
jsEnvConfig(),
testFramework(),
fastLinkJSTest()
Expand Down
13 changes: 6 additions & 7 deletions scalajslib/src/mill/scalajslib/worker/ScalaJSWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import mill.{Agg, T}
private[scalajslib] class ScalaJSWorker extends AutoCloseable {
private var scalaJSWorkerInstanceCache = Option.empty[(Long, workerApi.ScalaJSWorkerApi)]

private def bridge(toolsClasspath: Agg[os.Path])(implicit ctx: Ctx.Home) = {
val classloaderSig =
toolsClasspath.map(p => p.toString().hashCode + os.mtime(p)).sum
private def bridge(toolsClasspath: Agg[mill.PathRef])(implicit ctx: Ctx.Home) = {
val classloaderSig = toolsClasspath.hashCode
scalaJSWorkerInstanceCache match {
case Some((sig, bridge)) if sig == classloaderSig => bridge
case _ =>
val cl = mill.api.ClassLoader.create(
toolsClasspath.map(_.toIO.toURI.toURL).toVector,
toolsClasspath.map(_.path.toIO.toURI.toURL).toVector,
getClass.getClassLoader
)
val bridge = cl
Expand Down Expand Up @@ -129,7 +128,7 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
}

def link(
toolsClasspath: Agg[os.Path],
toolsClasspath: Agg[mill.PathRef],
sources: Agg[os.Path],
libraries: Agg[os.Path],
dest: File,
Expand Down Expand Up @@ -160,15 +159,15 @@ private[scalajslib] class ScalaJSWorker extends AutoCloseable {
}
}

def run(toolsClasspath: Agg[os.Path], config: api.JsEnvConfig, report: api.Report)(
def run(toolsClasspath: Agg[mill.PathRef], config: api.JsEnvConfig, report: api.Report)(
implicit ctx: Ctx.Home
): Unit = {
val dest =
bridge(toolsClasspath).run(toWorkerApi(config), toWorkerApi(report))
}

def getFramework(
toolsClasspath: Agg[os.Path],
toolsClasspath: Agg[mill.PathRef],
config: api.JsEnvConfig,
frameworkName: String,
report: api.Report
Expand Down

0 comments on commit 5234477

Please sign in to comment.