Skip to content

Commit 458aec6

Browse files
authored
Merge pull request #3257 from dotty-staging/try-optimize-6
Some micro-optimizations around phases
2 parents 20a8ea7 + e5bbb25 commit 458aec6

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

compiler/src/dotty/tools/dotc/core/Phases.scala

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ object Phases {
7272
override def lastPhaseId(implicit ctx: Context) = id
7373
}
7474

75-
def phasePlan = this.phasesPlan
76-
def setPhasePlan(phasess: List[List[Phase]]) = this.phasesPlan = phasess
75+
final def phasePlan = this.phasesPlan
76+
final def setPhasePlan(phasess: List[List[Phase]]) = this.phasesPlan = phasess
7777

7878
/** Squash TreeTransform's beloning to same sublist to a single TreeTransformer
7979
* Each TreeTransform gets own period,
8080
* whereas a combined TreeTransformer gets period equal to union of periods of it's TreeTransforms
8181
*/
82-
def squashPhases(phasess: List[List[Phase]],
82+
final def squashPhases(phasess: List[List[Phase]],
8383
phasesToSkip: List[String], stopBeforePhases: List[String], stopAfterPhases: List[String], YCheckAfter: List[String]): List[Phase] = {
8484
val squashedPhases = ListBuffer[Phase]()
8585
var prevPhases: Set[Class[_ <: Phase]] = Set.empty
@@ -139,7 +139,7 @@ object Phases {
139139
* The list should never contain NoPhase.
140140
* if squashing is enabled, phases in same subgroup will be squashed to single phase.
141141
*/
142-
def usePhases(phasess: List[Phase], squash: Boolean = true) = {
142+
final def usePhases(phasess: List[Phase], squash: Boolean = true) = {
143143

144144
val flatPhases = collection.mutable.ListBuffer[Phase]()
145145

@@ -149,6 +149,7 @@ object Phases {
149149
})
150150

151151
phases = (NoPhase :: flatPhases.toList ::: new TerminalPhase :: Nil).toArray
152+
setSpecificPhases()
152153
var phasesAfter:Set[Class[_ <: Phase]] = Set.empty
153154
nextDenotTransformerId = new Array[Int](phases.length)
154155
denotTransformers = new Array[DenotTransformer](phases.length)
@@ -211,54 +212,53 @@ object Phases {
211212
config.println(s"nextDenotTransformerId = ${nextDenotTransformerId.deep}")
212213
}
213214

214-
def phaseOfClass(pclass: Class[_]) = phases.find(pclass.isInstance).getOrElse(NoPhase)
215-
216-
private val cachedPhases = collection.mutable.Set[PhaseCache]()
217-
private def cleanPhaseCache = cachedPhases.foreach(_.myPhase = NoPhase)
218-
219-
/** A cache to compute the phase with given name, which
220-
* stores the phase as soon as phaseNamed returns something
221-
* different from NoPhase.
222-
*/
223-
private class PhaseCache(pclass: Class[_ <: Phase]) {
224-
var myPhase: Phase = NoPhase
225-
def phase = {
226-
if (myPhase eq NoPhase) myPhase = phaseOfClass(pclass)
227-
myPhase
228-
}
229-
cachedPhases += this
215+
private[this] var myTyperPhase: Phase = _
216+
private[this] var myPicklerPhase: Phase = _
217+
private[this] var myRefChecksPhase: Phase = _
218+
private[this] var myPatmatPhase: Phase = _
219+
private[this] var myElimRepeatedPhase: Phase = _
220+
private[this] var myExtensionMethodsPhase: Phase = _
221+
private[this] var myExplicitOuterPhase: Phase = _
222+
private[this] var myGettersPhase: Phase = _
223+
private[this] var myErasurePhase: Phase = _
224+
private[this] var myElimErasedValueTypePhase: Phase = _
225+
private[this] var myLambdaLiftPhase: Phase = _
226+
private[this] var myFlattenPhase: Phase = _
227+
private[this] var myGenBCodePhase: Phase = _
228+
229+
final def typerPhase = myTyperPhase
230+
final def picklerPhase = myPicklerPhase
231+
final def refchecksPhase = myRefChecksPhase
232+
final def patmatPhase = myPatmatPhase
233+
final def elimRepeatedPhase = myElimRepeatedPhase
234+
final def extensionMethodsPhase = myExtensionMethodsPhase
235+
final def explicitOuterPhase = myExplicitOuterPhase
236+
final def gettersPhase = myGettersPhase
237+
final def erasurePhase = myErasurePhase
238+
final def elimErasedValueTypePhase = myElimErasedValueTypePhase
239+
final def lambdaLiftPhase = myLambdaLiftPhase
240+
final def flattenPhase = myFlattenPhase
241+
final def genBCodePhase = myGenBCodePhase
242+
243+
private def setSpecificPhases() = {
244+
def phaseOfClass(pclass: Class[_]) = phases.find(pclass.isInstance).getOrElse(NoPhase)
245+
246+
myTyperPhase = phaseOfClass(classOf[FrontEnd])
247+
myPicklerPhase = phaseOfClass(classOf[Pickler])
248+
myRefChecksPhase = phaseOfClass(classOf[RefChecks])
249+
myElimRepeatedPhase = phaseOfClass(classOf[ElimRepeated])
250+
myExtensionMethodsPhase = phaseOfClass(classOf[ExtensionMethods])
251+
myErasurePhase = phaseOfClass(classOf[Erasure])
252+
myElimErasedValueTypePhase = phaseOfClass(classOf[ElimErasedValueType])
253+
myPatmatPhase = phaseOfClass(classOf[PatternMatcher])
254+
myLambdaLiftPhase = phaseOfClass(classOf[LambdaLift])
255+
myFlattenPhase = phaseOfClass(classOf[Flatten])
256+
myExplicitOuterPhase = phaseOfClass(classOf[ExplicitOuter])
257+
myGettersPhase = phaseOfClass(classOf[Getters])
258+
myGenBCodePhase = phaseOfClass(classOf[GenBCode])
230259
}
231260

232-
private val typerCache = new PhaseCache(classOf[FrontEnd])
233-
private val picklerCache = new PhaseCache(classOf[Pickler])
234-
235-
private val refChecksCache = new PhaseCache(classOf[RefChecks])
236-
private val elimRepeatedCache = new PhaseCache(classOf[ElimRepeated])
237-
private val extensionMethodsCache = new PhaseCache(classOf[ExtensionMethods])
238-
private val erasureCache = new PhaseCache(classOf[Erasure])
239-
private val elimErasedValueTypeCache = new PhaseCache(classOf[ElimErasedValueType])
240-
private val patmatCache = new PhaseCache(classOf[PatternMatcher])
241-
private val lambdaLiftCache = new PhaseCache(classOf[LambdaLift])
242-
private val flattenCache = new PhaseCache(classOf[Flatten])
243-
private val explicitOuterCache = new PhaseCache(classOf[ExplicitOuter])
244-
private val gettersCache = new PhaseCache(classOf[Getters])
245-
private val genBCodeCache = new PhaseCache(classOf[GenBCode])
246-
247-
def typerPhase = typerCache.phase
248-
def picklerPhase = picklerCache.phase
249-
def refchecksPhase = refChecksCache.phase
250-
def elimRepeatedPhase = elimRepeatedCache.phase
251-
def extensionMethodsPhase = extensionMethodsCache.phase
252-
def erasurePhase = erasureCache.phase
253-
def elimErasedValueTypePhase = elimErasedValueTypeCache.phase
254-
def patmatPhase = patmatCache.phase
255-
def lambdaLiftPhase = lambdaLiftCache.phase
256-
def flattenPhase = flattenCache.phase
257-
def explicitOuterPhase = explicitOuterCache.phase
258-
def gettersPhase = gettersCache.phase
259-
def genBCodePhase = genBCodeCache.phase
260-
261-
def isAfterTyper(phase: Phase): Boolean = phase.id > typerPhase.id
261+
final def isAfterTyper(phase: Phase): Boolean = phase.id > typerPhase.id
262262
}
263263

264264
trait Phase extends DotClass {

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,13 +1241,13 @@ object SymDenotations {
12411241
*/
12421242
class ClassDenotation private[SymDenotations] (
12431243
symbol: Symbol,
1244-
ownerIfExists: Symbol,
1244+
maybeOwner: Symbol,
12451245
name: Name,
12461246
initFlags: FlagSet,
12471247
initInfo: Type,
12481248
initPrivateWithin: Symbol,
12491249
initRunId: RunId)
1250-
extends SymDenotation(symbol, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {
1250+
extends SymDenotation(symbol, maybeOwner, name, initFlags, initInfo, initPrivateWithin) {
12511251

12521252
import util.LRUCache
12531253

@@ -2011,6 +2011,7 @@ object SymDenotations {
20112011
protected def sameGroup(p1: Phase, p2: Phase): Boolean
20122012

20132013
private[this] var dependent: WeakHashMap[InheritedCache, Unit] = null
2014+
private[this] var checkedPeriod: Period = Nowhere
20142015

20152016
protected def invalidateDependents() = {
20162017
if (dependent != null) {
@@ -2026,9 +2027,11 @@ object SymDenotations {
20262027
}
20272028

20282029
def isValidAt(phase: Phase)(implicit ctx: Context) =
2029-
createdAt.runId == ctx.runId &&
2030-
createdAt.phaseId < ctx.phases.length &&
2031-
sameGroup(ctx.phases(createdAt.phaseId), phase)
2030+
checkedPeriod == ctx.period ||
2031+
createdAt.runId == ctx.runId &&
2032+
createdAt.phaseId < ctx.phases.length &&
2033+
sameGroup(ctx.phases(createdAt.phaseId), phase) &&
2034+
{ checkedPeriod = ctx.period; true }
20322035
}
20332036

20342037
private class InvalidCache extends InheritedCache {

0 commit comments

Comments
 (0)