Skip to content

Commit 44b98ce

Browse files
committed
Better caching of prototypes
- Make IgnoredProto a cached type - Convert IgnroedProtos to WildcardTypes in wildApprox - Use `eql` instead of `equals` when hash-consing prototypes
1 parent 8c94870 commit 44b98ce

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,22 @@ object ProtoTypes {
120120
}
121121

122122
/** A class marking ignored prototypes that can be revealed by `deepenProto` */
123-
case class IgnoredProto(ignored: Type) extends UncachedGroundType with MatchAlways:
123+
abstract case class IgnoredProto(ignored: Type) extends CachedGroundType with MatchAlways:
124124
override def revealIgnored = ignored
125125
override def deepenProto(using Context): Type = ignored
126126

127+
override def computeHash(bs: Hashable.Binders): Int = doHash(bs, ignored)
128+
129+
override def eql(that: Type): Boolean = that match
130+
case that: IgnoredProto => ignored eq that.ignored
131+
case _ => false
132+
133+
final class CachedIgnoredProto(ignored: Type) extends IgnoredProto(ignored)
134+
127135
object IgnoredProto:
128136
def apply(ignored: Type): IgnoredProto = ignored match
129137
case ignored: IgnoredProto => ignored
130-
case _ => new IgnoredProto(ignored)
138+
case _ => CachedIgnoredProto(ignored)
131139

132140
/** A prototype for expressions [] that are part of a selection operation:
133141
*
@@ -185,13 +193,6 @@ object ProtoTypes {
185193
if ((name eq this.name) && (memberProto eq this.memberProto) && (compat eq this.compat)) this
186194
else SelectionProto(name, memberProto, compat, privateOK)
187195

188-
override def equals(that: Any): Boolean = that match {
189-
case that: SelectionProto =>
190-
(name eq that.name) && (memberProto == that.memberProto) && (compat eq that.compat) && (privateOK == that.privateOK)
191-
case _ =>
192-
false
193-
}
194-
195196
def map(tm: TypeMap)(using Context): SelectionProto = derivedSelectionProto(name, tm(memberProto), compat)
196197
def fold[T](x: T, ta: TypeAccumulator[T])(using Context): T = ta(x, memberProto)
197198

@@ -201,6 +202,13 @@ object ProtoTypes {
201202
val delta = (if (compat eq NoViewsAllowed) 1 else 0) | (if (privateOK) 2 else 0)
202203
addDelta(doHash(bs, name, memberProto), delta)
203204
}
205+
206+
override def eql(that: Type): Boolean = that match {
207+
case that: SelectionProto =>
208+
(name eq that.name) && (memberProto eq that.memberProto) && (compat eq that.compat) && (privateOK == that.privateOK)
209+
case _ =>
210+
false
211+
}
204212
}
205213

206214
class CachedSelectionProto(name: Name, memberProto: Type, compat: Compatibility, privateOK: Boolean)
@@ -450,6 +458,9 @@ object ProtoTypes {
450458

451459
class CachedViewProto(argType: Type, resultType: Type) extends ViewProto(argType, resultType) {
452460
override def computeHash(bs: Hashable.Binders): Int = doHash(bs, argType, resultType)
461+
override def eql(that: Type): Boolean = that match
462+
case that: ViewProto => (argType eq that.argType) && (resType eq that.resType)
463+
case _ => false
453464
}
454465

455466
object ViewProto {
@@ -680,6 +691,8 @@ object ProtoTypes {
680691
tp.derivedViewProto(
681692
wildApprox(tp.argType, theMap, seen, internal),
682693
wildApprox(tp.resultType, theMap, seen, internal))
694+
case tp: IgnoredProto =>
695+
WildcardType
683696
case _: ThisType | _: BoundType => // default case, inlined for speed
684697
tp
685698
case tl: TypeLambda =>

0 commit comments

Comments
 (0)