Skip to content

Commit 7e42f3b

Browse files
committed
Merge pull request scala-js#601 from gzm0/clean-optimizer
Fix scala-js#541: Make incremental optimizer honor clean
2 parents 405576b + c0e556e commit 7e42f3b

File tree

2 files changed

+58
-50
lines changed

2 files changed

+58
-50
lines changed

sbt-plugin/src/main/scala/scala/scalajs/sbtplugin/ScalaJSPlugin.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,12 @@ object ScalaJSPlugin extends Plugin with impl.DependencyBuilders {
486486
// add all the webjars your jsDependencies depend upon
487487
libraryDependencies ++= jsDependencies.value.collect {
488488
case JarJSModuleID(module, _) => module
489-
}
489+
},
490+
// have clean reset incremental optimizer state
491+
clean <<= clean.dependsOn(Def.task {
492+
scalaJSOptimizer.in(Compile, fastOptJS).value.clean()
493+
scalaJSOptimizer.in(Test, fastOptJS).value.clean()
494+
})
490495
)
491496

492497
val scalaJSProjectBaseSettings = Seq(

tools/src/main/scala/scala/scalajs/tools/optimizer/ScalaJSOptimizer.scala

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import ScalaJSPackedClasspath.packOrderLine
3030
class ScalaJSOptimizer {
3131
import ScalaJSOptimizer._
3232

33-
private[this] var logger: Logger = _
33+
private[this] var persistentState: PersistentState = new PersistentState
3434

3535
/** Applies Scala.js-specific optimizations to a Scala.js classpath.
3636
* See [[ScalaJSOptimizer.Inputs]] for details about the required and
@@ -45,66 +45,34 @@ class ScalaJSOptimizer {
4545
*/
4646
def optimize(inputs: Inputs, outputConfig: OutputConfig,
4747
logger: Logger): Unit = {
48-
this.logger = logger
49-
PersistentState.startRun()
48+
persistentState.startRun()
5049
try {
5150
import inputs._
52-
val analyzer = readClasspathAndCreateAnalyzer(classpath)
51+
val analyzer = readClasspathAndCreateAnalyzer(classpath, logger)
5352
analyzer.computeReachability(manuallyReachable, noWarnMissing)
5453
writeDCEedOutput(inputs, outputConfig, analyzer)
5554
} finally {
56-
PersistentState.endRun()
57-
this.logger = null
55+
persistentState.endRun()
56+
logger.debug(
57+
s"Inc. opt stats: reused: ${persistentState.statsReused} -- "+
58+
s"invalidated: ${persistentState.statsInvalidated} -- "+
59+
s"trees read: ${persistentState.statsTreesRead}")
5860
}
5961
}
6062

63+
/** Resets all persistent state of this optimizer */
64+
def clean(): Unit = {
65+
persistentState = new PersistentState
66+
}
67+
6168
private def readClasspathAndCreateAnalyzer(
62-
classpath: ScalaJSClasspath): Analyzer = {
69+
classpath: ScalaJSClasspath, logger: Logger): Analyzer = {
6370
val userInfo = classpath.irFiles map { irFile =>
64-
PersistentState.getPersistentIRFile(irFile).info
71+
persistentState.getPersistentIRFile(irFile).info
6572
}
6673
new Analyzer(logger, CoreData.CoreClassesInfo ++ userInfo)
6774
}
6875

69-
private[this] object PersistentState {
70-
71-
val files = mutable.Map.empty[String, PersistentIRFile]
72-
val encodedNameToPersistentFile =
73-
mutable.Map.empty[String, PersistentIRFile]
74-
75-
var statsReused: Int = 0
76-
var statsInvalidated: Int = 0
77-
var statsTreesRead: Int = 0
78-
79-
def startRun(): Unit = {
80-
statsReused = 0
81-
statsInvalidated = 0
82-
statsTreesRead = 0
83-
for (file <- files.values)
84-
file.startRun()
85-
}
86-
87-
def getPersistentIRFile(irFile: VirtualScalaJSIRFile): PersistentIRFile = {
88-
val file = files.getOrElseUpdate(irFile.path,
89-
new PersistentIRFile(irFile.path))
90-
if (file.updateFile(irFile))
91-
statsReused += 1
92-
else
93-
statsInvalidated += 1
94-
encodedNameToPersistentFile += ((file.info.encodedName, file))
95-
file
96-
}
97-
98-
def endRun(): Unit = {
99-
// "Garbage-collect" persisted versions of files that have disappeared
100-
files.retain((_, f) => f.cleanAfterRun())
101-
encodedNameToPersistentFile.clear()
102-
logger.debug(
103-
s"Inc. opt stats: reused: $statsReused -- "+
104-
s"invalidated: $statsInvalidated -- trees read: $statsTreesRead")
105-
}
106-
}
107-
10876
private def writeDCEedOutput(inputs: Inputs, outputConfig: OutputConfig,
10977
analyzer: Analyzer): Unit = {
11078

@@ -132,7 +100,7 @@ class ScalaJSOptimizer {
132100
for {
133101
classInfo <- analyzer.classInfos.values.toSeq.sortWith(compareClassInfo)
134102
if classInfo.isNeededAtAll
135-
persistentFile <- PersistentState.encodedNameToPersistentFile.get(
103+
persistentFile <- persistentState.encodedNameToPersistentFile.get(
136104
classInfo.encodedName)
137105
} {
138106
import ir.Trees._
@@ -141,7 +109,7 @@ class ScalaJSOptimizer {
141109

142110
val d = persistentFile.desugared
143111
lazy val classDef = {
144-
PersistentState.statsTreesRead += 1
112+
persistentState.statsTreesRead += 1
145113
persistentFile.tree
146114
}
147115

@@ -256,6 +224,41 @@ object ScalaJSOptimizer {
256224

257225
// Private helpers -----------------------------------------------------------
258226

227+
private final class PersistentState {
228+
val files = mutable.Map.empty[String, PersistentIRFile]
229+
val encodedNameToPersistentFile =
230+
mutable.Map.empty[String, PersistentIRFile]
231+
232+
var statsReused: Int = 0
233+
var statsInvalidated: Int = 0
234+
var statsTreesRead: Int = 0
235+
236+
def startRun(): Unit = {
237+
statsReused = 0
238+
statsInvalidated = 0
239+
statsTreesRead = 0
240+
for (file <- files.values)
241+
file.startRun()
242+
}
243+
244+
def getPersistentIRFile(irFile: VirtualScalaJSIRFile): PersistentIRFile = {
245+
val file = files.getOrElseUpdate(irFile.path,
246+
new PersistentIRFile(irFile.path))
247+
if (file.updateFile(irFile))
248+
statsReused += 1
249+
else
250+
statsInvalidated += 1
251+
encodedNameToPersistentFile += ((file.info.encodedName, file))
252+
file
253+
}
254+
255+
def endRun(): Unit = {
256+
// "Garbage-collect" persisted versions of files that have disappeared
257+
files.retain((_, f) => f.cleanAfterRun())
258+
encodedNameToPersistentFile.clear()
259+
}
260+
}
261+
259262
private final class PersistentIRFile(val path: String) {
260263
import ir.Trees._
261264

0 commit comments

Comments
 (0)