Skip to content

Commit 223aa2a

Browse files
authored
Merge pull request #3263 from dotty-staging/try-optimize-7
Micro-optimization: Symbol#denot
2 parents caa2bc5 + d0c377b commit 223aa2a

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,10 @@ object Denotations {
668668
private[this] var myValidFor: Period = Nowhere
669669

670670
def validFor = myValidFor
671-
def validFor_=(p: Period) =
671+
def validFor_=(p: Period) = {
672672
myValidFor = p
673+
symbol.invalidateDenotCache()
674+
}
673675

674676
/** The next SingleDenotation in this run, with wrap-around from last to first.
675677
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ object SymDenotations {
18511851
override def owner: Symbol = throw new AssertionError("NoDenotation.owner")
18521852
override def computeAsSeenFrom(pre: Type)(implicit ctx: Context): SingleDenotation = this
18531853
override def mapInfo(f: Type => Type)(implicit ctx: Context): SingleDenotation = this
1854-
validFor = Period.allInRun(NoRunId) // will be brought forward automatically
1854+
validFor = Period.allInRun(NoRunId)
18551855
}
18561856

18571857
@sharable val NoDenotation = new NoDenotation

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,34 @@ object Symbols {
398398

399399
/** The last denotation of this symbol */
400400
private[this] var lastDenot: SymDenotation = _
401+
private[this] var checkedPeriod: Period = Nowhere
402+
403+
private[core] def invalidateDenotCache() = { checkedPeriod = Nowhere }
401404

402405
/** Set the denotation of this symbol */
403-
private[core] def denot_=(d: SymDenotation) =
406+
private[core] def denot_=(d: SymDenotation) = {
404407
lastDenot = d
408+
checkedPeriod = Nowhere
409+
}
405410

406411
/** The current denotation of this symbol */
407412
final def denot(implicit ctx: Context): SymDenotation = {
408-
var denot = lastDenot
409-
if (!(denot.validFor contains ctx.period)) {
410-
denot = denot.current.asInstanceOf[SymDenotation]
411-
lastDenot = denot
412-
}
413-
denot
413+
val lastd = lastDenot
414+
if (checkedPeriod == ctx.period) lastd
415+
else computeDenot(lastd)
416+
}
417+
418+
private def computeDenot(lastd: SymDenotation)(implicit ctx: Context): SymDenotation = {
419+
val now = ctx.period
420+
checkedPeriod = now
421+
if (lastd.validFor contains now) lastd else recomputeDenot(lastd)
422+
}
423+
424+
/** Overridden in NoSymbol */
425+
protected def recomputeDenot(lastd: SymDenotation)(implicit ctx: Context) = {
426+
val newd = lastd.current.asInstanceOf[SymDenotation]
427+
lastDenot = newd
428+
newd
414429
}
415430

416431
/** The initial denotation of this symbol, without going through `current` */
@@ -631,8 +646,8 @@ object Symbols {
631646

632647
@sharable object NoSymbol extends Symbol(NoCoord, 0) {
633648
denot = NoDenotation
634-
635649
override def associatedFile(implicit ctx: Context): AbstractFile = NoSource.file
650+
override def recomputeDenot(lastd: SymDenotation)(implicit ctx: Context): SymDenotation = NoDenotation
636651
}
637652

638653
implicit class Copier[N <: Name](sym: Symbol { type ThisName = N })(implicit ctx: Context) {

0 commit comments

Comments
 (0)