Skip to content

Commit 0f5d088

Browse files
committed
Hash-cons all lambda types
1 parent 68b1d14 commit 0f5d088

File tree

1 file changed

+25
-51
lines changed

1 file changed

+25
-51
lines changed

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

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,11 +2356,6 @@ object Types {
23562356

23572357
override def computeHash(h: Hashing) = h.withBinder(this).doHash(getClass, parent)
23582358

2359-
override def eql(that: Type) = that match {
2360-
case that: RecType => parent.eq(that.parent)
2361-
case _ => false
2362-
}
2363-
23642359
override def iso(that: Any, e: StructEquality) = that match {
23652360
case that: RecType =>
23662361
e.withBinders(this, that).equals(parent, that.parent)
@@ -2684,37 +2679,51 @@ object Types {
26842679
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
26852680
}
26862681

2682+
/** Base class of HKTypeLambda. In the future could be base class of HKTermLambda
2683+
* (aka typelevel function type) if that is added.
2684+
*/
26872685
abstract class HKLambda extends CachedProxyType with LambdaType {
26882686
final override def underlying(implicit ctx: Context) = resType
26892687

26902688
final override def computeHash(h: Hashing) =
26912689
h.withBinder(this).doHash(getClass, paramNames, resType, paramInfos)
26922690

2693-
final override def eql(that: Type) = that match {
2691+
final override def iso(that: Any, e: StructEquality) = that match {
26942692
case that: HKLambda =>
2695-
paramNames.equals(that.paramNames) &&
2696-
paramInfos.equals(that.paramInfos) &&
2697-
resType.equals(that.resType) &&
2698-
companion.eq(that.companion)
2693+
paramNames.eqElements(that.paramNames) &&
2694+
companion.eq(that.companion) && {
2695+
val e1 = e.withBinders(this, that)
2696+
e1.equals(paramInfos, that.paramInfos) &&
2697+
e1.equals(resType, that.resType)
2698+
}
26992699
case _ =>
27002700
false
27012701
}
2702+
}
2703+
2704+
/** Common base class of MethodType and PolyType. It's methods are duplicated
2705+
* from HKLambda for efficiency. Joining the two methods in the common supertrait
2706+
* LambdaType could be slower because of trait dispatch and because the type test
2707+
* in `iso` would be to a trait instead of a class
2708+
*/
2709+
abstract class MethodOrPoly extends CachedGroundType with LambdaType with MethodicType {
2710+
2711+
final override def computeHash(h: Hashing) =
2712+
h.withBinder(this).doHash(getClass, paramNames, resType, paramInfos)
27022713

27032714
final override def iso(that: Any, e: StructEquality) = that match {
2704-
case that: HKLambda =>
2715+
case that: MethodOrPoly =>
27052716
paramNames.eqElements(that.paramNames) &&
27062717
companion.eq(that.companion) && {
27072718
val e1 = e.withBinders(this, that)
27082719
e1.equals(paramInfos, that.paramInfos) &&
27092720
e1.equals(resType, that.resType)
27102721
}
2711-
case _ =>
2722+
case _ =>
27122723
false
27132724
}
27142725
}
27152726

2716-
trait MethodOrPoly extends LambdaType with MethodicType // TODO: Make PolyTypes cached
2717-
27182727
trait TermLambda extends LambdaType { thisLambdaType =>
27192728
import DepStatus._
27202729
type ThisName = TermName
@@ -2830,7 +2839,7 @@ object Types {
28302839
abstract case class MethodType(paramNames: List[TermName])(
28312840
paramInfosExp: MethodType => List[Type],
28322841
resultTypeExp: MethodType => Type)
2833-
extends CachedGroundType with MethodOrPoly with TermLambda with NarrowCached { thisMethodType =>
2842+
extends MethodOrPoly with TermLambda with NarrowCached { thisMethodType =>
28342843
import MethodType._
28352844

28362845
type This = MethodType
@@ -2847,29 +2856,6 @@ object Types {
28472856
def computeSignature(implicit ctx: Context): Signature =
28482857
resultSignature.prepend(paramInfos, isJavaMethod)
28492858

2850-
final override def computeHash(h: Hashing) = h.doHash(getClass, paramNames, resType, paramInfos)
2851-
2852-
final override def eql(that: Type) = that match {
2853-
case that: MethodType =>
2854-
paramNames.eqElements(that.paramNames) &&
2855-
paramInfos.eqElements(that.paramInfos) &&
2856-
resType.eq(that.resType) &&
2857-
companion.eq(that.companion)
2858-
case _ =>
2859-
false
2860-
}
2861-
2862-
final override def iso(that: Any, e: StructEquality) = that match {
2863-
case that: MethodType =>
2864-
paramNames.eqElements(that.paramNames) &&
2865-
companion.eq(that.companion) && {
2866-
val e1 = e.withBinders(this, that)
2867-
e1.equals(paramInfos, that.paramInfos) &&
2868-
e1.equals(resType, that.resType)
2869-
}
2870-
case _ =>
2871-
false
2872-
}
28732859

28742860
protected def prefixString = "MethodType"
28752861
}
@@ -3028,7 +3014,7 @@ object Types {
30283014
*/
30293015
class PolyType(val paramNames: List[TypeName])(
30303016
paramInfosExp: PolyType => List[TypeBounds], resultTypeExp: PolyType => Type)
3031-
extends UncachedGroundType with MethodOrPoly with TypeLambda {
3017+
extends MethodOrPoly with TypeLambda {
30323018

30333019
type This = PolyType
30343020
def companion = PolyType
@@ -3059,18 +3045,6 @@ object Types {
30593045
case _ => this
30603046
}
30613047

3062-
final override def iso(that: Any, e: StructEquality) = that match { // TODO: Move up to MethodOrPoly
3063-
case that: PolyType =>
3064-
paramNames.eqElements(that.paramNames) &&
3065-
companion.eq(that.companion) && {
3066-
val e1 = e.withBinders(this, that)
3067-
e1.equals(paramInfos, that.paramInfos) &&
3068-
e1.equals(resType, that.resType)
3069-
}
3070-
case _ =>
3071-
false
3072-
}
3073-
30743048
protected def prefixString = "PolyType"
30753049
}
30763050

0 commit comments

Comments
 (0)