Skip to content

Commit a1deca9

Browse files
authored
Merge pull request #9743 from dotty-staging/cache-annotated-types
Cache AnnotatedTypes
2 parents 9f80d16 + 0656438 commit a1deca9

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ trait Hashable {
9090
protected final def doHash(bs: Binders, x1: Any, tp2: Type): Int =
9191
finishHash(bs, hashing.mix(hashSeed, x1.hashCode), 1, tp2)
9292

93+
protected final def doHash(bs: Binders, x1: Int, tp2: Type): Int =
94+
finishHash(bs, hashing.mix(hashSeed, x1), 1, tp2)
95+
9396
protected final def doHash(bs: Binders, tp1: Type, tp2: Type): Int =
9497
finishHash(bs, hashSeed, 0, tp1, tp2)
9598

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

Lines changed: 21 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,28 @@ 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 =
4702+
doHash(bs, System.identityHashCode(annot), parent)
4703+
override def hashIsStable: Boolean =
4704+
parent.hashIsStable
4705+
4706+
override def eql(that: Type): Boolean = that match
4707+
case that: AnnotatedType => (parent eq that.parent) && (annot eq that.annot)
47044708
case _ => false
4705-
}
4706-
}
47074709

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

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

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

0 commit comments

Comments
 (0)