Skip to content

Commit 9349867

Browse files
noti0na1WojciechMazur
authored andcommitted
Cache signature in SingleDenotation for matchDegree; reduce denot calls in widens
[Cherry-picked 69c16f8]
1 parent 81ff618 commit 9349867

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
@@ -621,6 +621,30 @@ object Denotations {
621621
throw ex
622622
case _ => Signature.NotAMethod
623623

624+
private var myCurrentJavaSig: Signature = uninitialized
625+
private var myCurrentJavaSigRunId: RunId = NoRunId
626+
private var myCurrentScala2Sig: Signature = uninitialized
627+
private var myCurrentScala2SigRunId: RunId = NoRunId
628+
private var myCurrentSig: Signature = uninitialized
629+
private var myCurrentSigRunId: RunId = NoRunId
630+
631+
def currentSignature(sourceLanguage: SourceLanguage)(using Context): Signature = sourceLanguage match
632+
case SourceLanguage.Java =>
633+
if myCurrentJavaSigRunId != ctx.runId then
634+
myCurrentJavaSig = signature(sourceLanguage)
635+
myCurrentJavaSigRunId = ctx.runId
636+
myCurrentJavaSig
637+
case SourceLanguage.Scala2 =>
638+
if myCurrentScala2SigRunId != ctx.runId then
639+
myCurrentScala2Sig = signature(sourceLanguage)
640+
myCurrentScala2SigRunId = ctx.runId
641+
myCurrentScala2Sig
642+
case SourceLanguage.Scala3 =>
643+
if myCurrentSigRunId != ctx.runId then
644+
myCurrentSig = signature(sourceLanguage)
645+
myCurrentSigRunId = ctx.runId
646+
myCurrentSig
647+
624648
def derivedSingleDenotation(symbol: Symbol, info: Type, pre: Type = this.prefix, isRefinedMethod: Boolean = this.isRefinedMethod)(using Context): SingleDenotation =
625649
if ((symbol eq this.symbol) && (info eq this.info) && (pre eq this.prefix) && (isRefinedMethod == this.isRefinedMethod)) this
626650
else newLikeThis(symbol, info, pre, isRefinedMethod)
@@ -1023,8 +1047,8 @@ object Denotations {
10231047
val thisLanguage = SourceLanguage(symbol)
10241048
val otherLanguage = SourceLanguage(other.symbol)
10251049
val commonLanguage = SourceLanguage.commonLanguage(thisLanguage, otherLanguage)
1026-
val sig = signature(commonLanguage)
1027-
val otherSig = other.signature(commonLanguage)
1050+
val sig = currentSignature(commonLanguage)
1051+
val otherSig = other.currentSignature(commonLanguage)
10281052
sig.matchDegree(otherSig) match
10291053
case FullMatch =>
10301054
!alwaysCompareTypes || info.matches(other.info)

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,8 @@ object Types extends TypeUtils {
12621262
final def widen(using Context): Type = this match
12631263
case _: TypeRef | _: MethodOrPoly => this // fast path for most frequent cases
12641264
case tp: TermRef => // fast path for next most frequent case
1265-
if tp.isOverloaded then tp else tp.underlying.widen
1265+
val denot = tp.denot
1266+
if denot.isOverloaded then tp else denot.info.widen
12661267
case tp: SingletonType => tp.underlying.widen
12671268
case tp: ExprType => tp.resultType.widen
12681269
case tp =>
@@ -1276,15 +1277,20 @@ object Types extends TypeUtils {
12761277
* base type by applying one or more `underlying` dereferences.
12771278
*/
12781279
final def widenSingleton(using Context): Type = stripped match {
1279-
case tp: SingletonType if !tp.isOverloaded => tp.underlying.widenSingleton
1280+
case tp: TermRef =>
1281+
val denot = tp.denot
1282+
if denot.isOverloaded then this else denot.info.widenSingleton
1283+
case tp: SingletonType => tp.underlying.widenSingleton
12801284
case _ => this
12811285
}
12821286

12831287
/** Widen from TermRef to its underlying non-termref
12841288
* base type, while also skipping Expr types.
12851289
*/
12861290
final def widenTermRefExpr(using Context): Type = stripTypeVar match {
1287-
case tp: TermRef if !tp.isOverloaded => tp.underlying.widenExpr.widenTermRefExpr
1291+
case tp: TermRef =>
1292+
val denot = tp.denot
1293+
if denot.isOverloaded then this else denot.info.widenExpr.widenTermRefExpr
12881294
case _ => this
12891295
}
12901296

0 commit comments

Comments
 (0)