Skip to content

Commit c0e7113

Browse files
committed
Cache all method and polytypes
1 parent 03377c7 commit c0e7113

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,7 @@ object Types {
26542654
final def isHigherKinded = isInstanceOf[TypeProxy]
26552655

26562656
private[this] var myParamRefs: List[ParamRefType] = null
2657+
private[this] var myStableHash: Byte = 0
26572658

26582659
def paramRefs: List[ParamRefType] = {
26592660
if (myParamRefs == null) myParamRefs = paramNames.indices.toList.map(newParamRef)
@@ -2686,24 +2687,20 @@ object Types {
26862687
x => paramInfos.mapConserve(_.subst(this, x).asInstanceOf[PInfo]),
26872688
x => resType.subst(this, x))
26882689

2689-
protected def prefixString: String
2690-
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
2691-
}
2692-
2693-
abstract class HKLambda extends CachedProxyType with LambdaType {
2694-
final override def underlying(implicit ctx: Context) = resType
2695-
26962690
override def computeHash(bs: Binders) =
26972691
doHash(new Binders(this, bs), paramNames, resType, paramInfos)
26982692

2699-
override def stableHash = resType.stableHash && paramInfos.stableHash
2693+
override def stableHash = {
2694+
if (myStableHash == 0) myStableHash = if (resType.stableHash && paramInfos.stableHash) 1 else -1
2695+
myStableHash > 0
2696+
}
27002697

27012698
final override def equals(that: Any) = equals(that, null)
27022699

27032700
// No definition of `eql` --> fall back on equals, which calls iso
27042701

27052702
final override def iso(that: Any, bs: BinderPairs) = that match {
2706-
case that: HKLambda =>
2703+
case that: LambdaType =>
27072704
paramNames.eqElements(that.paramNames) &&
27082705
companion.eq(that.companion) && {
27092706
val bs1 = new BinderPairs(this, that, bs)
@@ -2713,13 +2710,17 @@ object Types {
27132710
case _ =>
27142711
false
27152712
}
2713+
2714+
protected def prefixString: String
2715+
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
27162716
}
27172717

2718-
abstract class MethodOrPoly extends UncachedGroundType with LambdaType with MethodicType {
2719-
final override def hashCode = System.identityHashCode(this)
2720-
final override def equals(other: Any) = this `eq` other.asInstanceOf[AnyRef]
2718+
abstract class HKLambda extends CachedProxyType with LambdaType {
2719+
final override def underlying(implicit ctx: Context) = resType
27212720
}
27222721

2722+
abstract class MethodOrPoly extends CachedGroundType with LambdaType with MethodicType
2723+
27232724
trait TermLambda extends LambdaType { thisLambdaType =>
27242725
import DepStatus._
27252726
type ThisName = TermName

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,11 @@ object ProtoTypes {
400400
/** Ensure that `tl` is not already in constraint, make a copy of necessary */
401401
def ensureFresh(tl: TypeLambda): TypeLambda =
402402
if (state.constraint contains tl) {
403-
var paramInfos = tl.paramInfos
404-
if (tl.isInstanceOf[HKLambda]) {
405-
// HKLambdas care hash-consed, need to create an artificial difference by adding
406-
// a LazyRef to a bound.
407-
val TypeBounds(lo, hi) :: pinfos1 = tl.paramInfos
408-
paramInfos = TypeBounds(lo, LazyRef(_ => hi)) :: pinfos1
409-
}
410-
ensureFresh(tl.newLikeThis(tl.paramNames, paramInfos, tl.resultType))
403+
// Type lambdas are hash-consed, need to create an artificial difference by adding
404+
// a LazyRef to a bound.
405+
val TypeBounds(lo, hi) :: pinfos1 = tl.paramInfos
406+
val newParamInfos = TypeBounds(lo, LazyRef(_ => hi)) :: pinfos1
407+
ensureFresh(tl.newLikeThis(tl.paramNames, newParamInfos, tl.resultType))
411408
}
412409
else tl
413410
val added = ensureFresh(tl)

0 commit comments

Comments
 (0)