Skip to content

Commit 69c16f8

Browse files
committed
Cache signature in SingleDenotation for matchDegree; reduce denot calls in widens
1 parent 65a53b5 commit 69c16f8

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,30 @@ object Denotations {
626626
throw ex
627627
case _ => Signature.NotAMethod
628628

629+
private var myCurrentJavaSig: Signature = uninitialized
630+
private var myCurrentJavaSigRunId: RunId = NoRunId
631+
private var myCurrentScala2Sig: Signature = uninitialized
632+
private var myCurrentScala2SigRunId: RunId = NoRunId
633+
private var myCurrentSig: Signature = uninitialized
634+
private var myCurrentSigRunId: RunId = NoRunId
635+
636+
def currentSignature(sourceLanguage: SourceLanguage)(using Context): Signature = sourceLanguage match
637+
case SourceLanguage.Java =>
638+
if myCurrentJavaSigRunId != ctx.runId then
639+
myCurrentJavaSig = signature(sourceLanguage)
640+
myCurrentJavaSigRunId = ctx.runId
641+
myCurrentJavaSig
642+
case SourceLanguage.Scala2 =>
643+
if myCurrentScala2SigRunId != ctx.runId then
644+
myCurrentScala2Sig = signature(sourceLanguage)
645+
myCurrentScala2SigRunId = ctx.runId
646+
myCurrentScala2Sig
647+
case SourceLanguage.Scala3 =>
648+
if myCurrentSigRunId != ctx.runId then
649+
myCurrentSig = signature(sourceLanguage)
650+
myCurrentSigRunId = ctx.runId
651+
myCurrentSig
652+
629653
def derivedSingleDenotation(symbol: Symbol, info: Type, pre: Type = this.prefix, isRefinedMethod: Boolean = this.isRefinedMethod)(using Context): SingleDenotation =
630654
if ((symbol eq this.symbol) && (info eq this.info) && (pre eq this.prefix) && (isRefinedMethod == this.isRefinedMethod)) this
631655
else newLikeThis(symbol, info, pre, isRefinedMethod)
@@ -1033,8 +1057,8 @@ object Denotations {
10331057
val thisLanguage = SourceLanguage(symbol)
10341058
val otherLanguage = SourceLanguage(other.symbol)
10351059
val commonLanguage = SourceLanguage.commonLanguage(thisLanguage, otherLanguage)
1036-
val sig = signature(commonLanguage)
1037-
val otherSig = other.signature(commonLanguage)
1060+
val sig = currentSignature(commonLanguage)
1061+
val otherSig = other.currentSignature(commonLanguage)
10381062
sig.matchDegree(otherSig) match
10391063
case FullMatch =>
10401064
!alwaysCompareTypes || info.matches(other.info)

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,8 @@ object Types extends TypeUtils {
13111311
final def widen(using Context): Type = this match
13121312
case _: TypeRef | _: MethodOrPoly => this // fast path for most frequent cases
13131313
case tp: TermRef => // fast path for next most frequent case
1314-
if tp.isOverloaded then tp else tp.underlying.widen
1314+
val denot = tp.denot
1315+
if denot.isOverloaded then tp else denot.info.widen
13151316
case tp: SingletonType => tp.underlying.widen
13161317
case tp: ExprType => tp.resultType.widen
13171318
case tp =>
@@ -1325,15 +1326,20 @@ object Types extends TypeUtils {
13251326
* base type by applying one or more `underlying` dereferences.
13261327
*/
13271328
final def widenSingleton(using Context): Type = stripped match {
1328-
case tp: SingletonType if !tp.isOverloaded => tp.underlying.widenSingleton
1329+
case tp: TermRef =>
1330+
val denot = tp.denot
1331+
if denot.isOverloaded then this else denot.info.widenSingleton
1332+
case tp: SingletonType => tp.underlying.widenSingleton
13291333
case _ => this
13301334
}
13311335

13321336
/** Widen from TermRef to its underlying non-termref
13331337
* base type, while also skipping Expr types.
13341338
*/
13351339
final def widenTermRefExpr(using Context): Type = stripTypeVar match {
1336-
case tp: TermRef if !tp.isOverloaded => tp.underlying.widenExpr.widenTermRefExpr
1340+
case tp: TermRef =>
1341+
val denot = tp.denot
1342+
if denot.isOverloaded then this else denot.info.widenExpr.widenTermRefExpr
13371343
case _ => this
13381344
}
13391345

0 commit comments

Comments
 (0)