Skip to content

Commit 1443fd4

Browse files
committed
Optimize hk comparisons
Use (cached) superType where possible.
1 parent 7df0fa5 commit 1443fd4

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,25 +668,25 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
668668
* tp1 <:< tp2 using fourthTry (this might instantiate params in tp1)
669669
* tp1 <:< app2 using isSubType (this might instantiate params in tp2)
670670
*/
671-
def compareLower(tycon2bounds: TypeBounds): Boolean = {
672-
val app2 = tycon2bounds.lo.applyIfParameterized(args2)
673-
if (tycon2bounds.lo eq tycon2bounds.hi) isSubType(tp1, app2)
674-
else either(fourthTry(tp1, tp2), isSubType(tp1, app2))
671+
def compareLower(tycon2bounds: TypeBounds, tyconIsTypeRef: Boolean): Boolean = {
672+
def app2 = tycon2bounds.lo.applyIfParameterized(args2)
673+
if (tycon2bounds.lo eq tycon2bounds.hi)
674+
isSubType(tp1, if (tyconIsTypeRef) tp2.superType else app2)
675+
else
676+
either(fourthTry(tp1, tp2), isSubType(tp1, app2))
675677
}
676678

677679
tycon2 match {
678680
case param2: PolyParam =>
679681
isMatchingApply(tp1) || {
680682
if (canConstrain(param2)) canInstantiate(param2)
681-
else compareLower(bounds(param2))
683+
else compareLower(bounds(param2), tyconIsTypeRef = false)
682684
}
683685
case tycon2: TypeRef =>
684686
isMatchingApply(tp1) ||
685-
compareLower(tycon2.info.bounds)
686-
case tycon2: TypeVar =>
687-
isSubType(tp1, tycon2.underlying.appliedTo(args2))
688-
case tycon2: AnnotatedType =>
689-
compareHkApply2(tp1, tp2, tycon2.underlying, args2)
687+
compareLower(tycon2.info.bounds, tyconIsTypeRef = true)
688+
case _: TypeVar | _: AnnotatedType =>
689+
isSubType(tp1, tp2.superType)
690690
case _ =>
691691
false
692692
}
@@ -706,7 +706,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
706706
canConstrain(param1) && canInstantiate ||
707707
isSubType(bounds(param1).hi.applyIfParameterized(args1), tp2)
708708
case tycon1: TypeProxy =>
709-
isSubType(tycon1.superType.applyIfParameterized(args1), tp2)
709+
isSubType(tp1.superType, tp2)
710710
case _ =>
711711
false
712712
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,19 @@ object Types {
26612661
}
26622662
cachedSuper
26632663
}
2664+
2665+
def lowerBound(implicit ctx: Context) = tycon.stripTypeVar match {
2666+
case tycon: TypeRef =>
2667+
tycon.info match {
2668+
case TypeBounds(lo, hi) =>
2669+
if (lo eq hi) superType // optimization, can profit from caching in this case
2670+
else lo.applyIfParameterized(args)
2671+
case _ => NoType
2672+
}
2673+
case _ =>
2674+
NoType
2675+
}
2676+
26642677
/*
26652678
def lowerBound(implicit ctx: Context): Type = tycon.stripTypeVar match {
26662679
case tp: TypeRef =>

0 commit comments

Comments
 (0)