Skip to content

Commit 2cd9998

Browse files
committed
Cache AnnotatedTypes
This leads to an increase of cachable applied types. For typer/*.scala: cached applied types uncached applied types now 32.3K 0.4K was 30k 12.4K # Conflicts: # compiler/src/dotty/tools/dotc/core/Types.scala
1 parent 8c94870 commit 2cd9998

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ object Types {
147147
final def exists: Boolean = this.ne(NoType)
148148

149149
/** This type, if it exists, otherwise `that` type */
150-
inline def orElse(inline that: => Type): Type = if (exists) this else that
150+
inline def orElse(inline that: Type): Type = if (exists) this else that
151151

152152
/** Is this type a value type? */
153153
final def isValueType: Boolean = this.isInstanceOf[ValueType]
@@ -4672,12 +4672,11 @@ object Types {
46724672
// ----- Annotated and Import types -----------------------------------------------
46734673

46744674
/** An annotated type tpe @ annot */
4675-
case class AnnotatedType(parent: Type, annot: Annotation) extends UncachedProxyType with ValueType {
4676-
// todo: cache them? but this makes only sense if annotations and trees are also cached.
4675+
abstract case class AnnotatedType(parent: Type, annot: Annotation) extends CachedProxyType with ValueType {
46774676

46784677
override def underlying(using Context): Type = parent
46794678

4680-
def derivedAnnotatedType(parent: Type, annot: Annotation): AnnotatedType =
4679+
def derivedAnnotatedType(parent: Type, annot: Annotation)(using Context): AnnotatedType =
46814680
if ((parent eq this.parent) && (annot eq this.annot)) this
46824681
else AnnotatedType(parent, annot)
46834682

@@ -4699,17 +4698,26 @@ object Types {
46994698

47004699
// equals comes from case class; no matching override is needed
47014700

4702-
override def iso(that: Any, bs: BinderPairs): Boolean = that match {
4703-
case that: AnnotatedType => parent.equals(that.parent, bs) && (annot `eq` that.annot)
4701+
override def computeHash(bs: Binders): Int = doHash(bs, annot, parent)
4702+
override def hashIsStable: Boolean = parent.hashIsStable
4703+
4704+
override def eql(that: Type): Boolean = that match
4705+
case that: AnnotatedType => (parent eq that.parent) && (annot eq that.annot)
47044706
case _ => false
4705-
}
4706-
}
47074707

4708-
object AnnotatedType {
4709-
def make(underlying: Type, annots: List[Annotation]): Type =
4710-
annots.foldLeft(underlying)(AnnotatedType(_, _))
4708+
override def iso(that: Any, bs: BinderPairs): Boolean = that match
4709+
case that: AnnotatedType => parent.equals(that.parent, bs) && (annot eq that.annot)
4710+
case _ => false
47114711
}
47124712

4713+
class CachedAnnotatedType(parent: Type, annot: Annotation) extends AnnotatedType(parent, annot)
4714+
4715+
object AnnotatedType:
4716+
def make(underlying: Type, annots: List[Annotation])(using Context): Type =
4717+
annots.foldLeft(underlying)(apply(_, _))
4718+
def apply(parent: Type, annot: Annotation)(using Context): AnnotatedType =
4719+
unique(CachedAnnotatedType(parent, annot))
4720+
47134721
// Special type objects and classes -----------------------------------------------------
47144722

47154723
/** The type of an erased array */

0 commit comments

Comments
 (0)