Skip to content

Commit b5a923c

Browse files
committed
Streamline some hot compuations
Make it cheaper to compute whether a Period is Nowhere, and also make the symbol and denot computations on NamedType as small as possible.
1 parent 19dab9d commit b5a923c

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ object Periods {
118118
apply(rid, 0, PhaseMask)
119119
}
120120

121-
final val Nowhere: Period = new Period(0)
121+
inline val NowhereCode = 0
122+
123+
final val Nowhere: Period = new Period(NowhereCode)
124+
125+
extension (p: Period)
126+
inline def exists: Boolean = p.code != NowhereCode
122127

123128
final val InitialPeriod: Period = Period(InitialRunId, FirstPhaseId)
124129

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,15 +2263,17 @@ object Types {
22632263
final def symbol(using Context): Symbol =
22642264
// We can rely on checkedPeriod (unlike in the definition of `denot` below)
22652265
// because SymDenotation#installAfter never changes the symbol
2266-
if (checkedPeriod == ctx.period) lastSymbol.nn else computeSymbol
2266+
if (checkedPeriod == ctx.period) lastSymbol.asInstanceOf[Symbol]
2267+
else computeSymbol
22672268

22682269
private def computeSymbol(using Context): Symbol =
2269-
designator match {
2270+
val result = designator match
22702271
case sym: Symbol =>
22712272
if (sym.isValidInCurrentRun) sym else denot.symbol
22722273
case name =>
2273-
(if (denotationIsCurrent) lastDenotation.nn else denot).symbol
2274-
}
2274+
(if (denotationIsCurrent) lastDenotation.asInstanceOf[Denotation] else denot).symbol
2275+
if checkedPeriod.exists then checkedPeriod = ctx.period
2276+
result
22752277

22762278
/** There is a denotation computed which is valid (somewhere in) the
22772279
* current run.
@@ -2303,18 +2305,16 @@ object Types {
23032305

23042306
def info(using Context): Type = denot.info
23052307

2306-
/** The denotation currently denoted by this type */
2307-
final def denot(using Context): Denotation = {
2308+
/** The denotation currently denoted by this type. Extremely hot. Carefully optimized
2309+
* to be as small as possible.
2310+
*/
2311+
final def denot(using Context): Denotation =
23082312
util.Stats.record("NamedType.denot")
2309-
val now = ctx.period
2313+
val lastd = lastDenotation.asInstanceOf[Denotation]
23102314
// Even if checkedPeriod == now we still need to recheck lastDenotation.validFor
23112315
// as it may have been mutated by SymDenotation#installAfter
2312-
if (checkedPeriod != Nowhere && lastDenotation.nn.validFor.contains(now)) {
2313-
checkedPeriod = now
2314-
lastDenotation.nn
2315-
}
2316+
if checkedPeriod.code != NowhereCode && lastd.validFor.contains(ctx.period) then lastd
23162317
else computeDenot
2317-
}
23182318

23192319
private def computeDenot(using Context): Denotation = {
23202320
util.Stats.record("NamedType.computeDenot")
@@ -2350,11 +2350,11 @@ object Types {
23502350
lastDenotation match {
23512351
case lastd0: SingleDenotation =>
23522352
val lastd = lastd0.skipRemoved
2353-
if lastd.validFor.runId == ctx.runId && checkedPeriod != Nowhere then
2353+
if lastd.validFor.runId == ctx.runId && checkedPeriod.exists then
23542354
finish(lastd.current)
23552355
else lastd match {
23562356
case lastd: SymDenotation =>
2357-
if (stillValid(lastd) && (checkedPeriod != Nowhere)) finish(lastd.current)
2357+
if stillValid(lastd) && checkedPeriod.exists then finish(lastd.current)
23582358
else finish(memberDenot(lastd.initial.name, allowPrivate = false))
23592359
case _ =>
23602360
fromDesignator

0 commit comments

Comments
 (0)