@@ -30,7 +30,7 @@ import ScalaJSPackedClasspath.packOrderLine
30
30
class ScalaJSOptimizer {
31
31
import ScalaJSOptimizer ._
32
32
33
- private [this ] var logger : Logger = _
33
+ private [this ] var persistentState : PersistentState = new PersistentState
34
34
35
35
/** Applies Scala.js-specific optimizations to a Scala.js classpath.
36
36
* See [[ScalaJSOptimizer.Inputs ]] for details about the required and
@@ -45,66 +45,34 @@ class ScalaJSOptimizer {
45
45
*/
46
46
def optimize (inputs : Inputs , outputConfig : OutputConfig ,
47
47
logger : Logger ): Unit = {
48
- this .logger = logger
49
- PersistentState .startRun()
48
+ persistentState.startRun()
50
49
try {
51
50
import inputs ._
52
- val analyzer = readClasspathAndCreateAnalyzer(classpath)
51
+ val analyzer = readClasspathAndCreateAnalyzer(classpath, logger )
53
52
analyzer.computeReachability(manuallyReachable, noWarnMissing)
54
53
writeDCEedOutput(inputs, outputConfig, analyzer)
55
54
} 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}" )
58
60
}
59
61
}
60
62
63
+ /** Resets all persistent state of this optimizer */
64
+ def clean (): Unit = {
65
+ persistentState = new PersistentState
66
+ }
67
+
61
68
private def readClasspathAndCreateAnalyzer (
62
- classpath : ScalaJSClasspath ): Analyzer = {
69
+ classpath : ScalaJSClasspath , logger : Logger ): Analyzer = {
63
70
val userInfo = classpath.irFiles map { irFile =>
64
- PersistentState .getPersistentIRFile(irFile).info
71
+ persistentState .getPersistentIRFile(irFile).info
65
72
}
66
73
new Analyzer (logger, CoreData .CoreClassesInfo ++ userInfo)
67
74
}
68
75
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
-
108
76
private def writeDCEedOutput (inputs : Inputs , outputConfig : OutputConfig ,
109
77
analyzer : Analyzer ): Unit = {
110
78
@@ -132,7 +100,7 @@ class ScalaJSOptimizer {
132
100
for {
133
101
classInfo <- analyzer.classInfos.values.toSeq.sortWith(compareClassInfo)
134
102
if classInfo.isNeededAtAll
135
- persistentFile <- PersistentState .encodedNameToPersistentFile.get(
103
+ persistentFile <- persistentState .encodedNameToPersistentFile.get(
136
104
classInfo.encodedName)
137
105
} {
138
106
import ir .Trees ._
@@ -141,7 +109,7 @@ class ScalaJSOptimizer {
141
109
142
110
val d = persistentFile.desugared
143
111
lazy val classDef = {
144
- PersistentState .statsTreesRead += 1
112
+ persistentState .statsTreesRead += 1
145
113
persistentFile.tree
146
114
}
147
115
@@ -256,6 +224,41 @@ object ScalaJSOptimizer {
256
224
257
225
// Private helpers -----------------------------------------------------------
258
226
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
+
259
262
private final class PersistentIRFile (val path : String ) {
260
263
import ir .Trees ._
261
264
0 commit comments