8000 Short circuit type comparisons of type refs when eta-expansion is futile · scala/scala@babf510 · GitHub
[go: up one dir, main page]

Skip to content

Commit babf510

Browse files
committed
Short circuit type comparisons of type refs when eta-expansion is futile
If the initial comparison of a pair of higher kinded type refs yield false, there is no point eta expanding them both to poly types to repeat the comparison (in an more expensive fashion!)
1 parent 1517573 commit babf510

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3067,7 +3067,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
30673067
override def tpeHK: Type = typeConstructor
30683068

30693069
private def tyconCacheNeedsUpdate = (tyconCache eq null) || tyconRunId != currentRunId
3070-
private def setTyconCache(tycon: Type) {
3070+
private[Symbols] def setTyconCache(tycon: Type) {
30713071
tyconCache = tycon
30723072
tyconRunId = currentRunId
30733073
assert(tyconCache ne null, this)

src/reflect/scala/reflect/internal/tpe/TypeComparers.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ trait TypeComparers {
180180
!settings.isScala211 || methodHigherOrderTypeParamsSameVariance(low, high) || low.variance.isInvariant
181181

182182
def isSameType2(tp1: Type, tp2: Type): Boolean = {
183-
def retry(lhs: Type, rhs: Type) = ((lhs ne tp1) || (rhs ne tp2)) && isSameType(lhs, rhs)
183+
def retry(lhs: Type, rhs: Type) = ((lhs ne tp1) || (rhs ne tp2)) && {
184+
def sameSyms(tp1: Type, tp2: Type) = tp1.typeSymbolDirect eq tp2.typeSymbolDirect
185+
val skipRetry = lhs.isInstanceOf[PolyType] && rhs.isInstanceOf[PolyType] && sameSyms(tp1, lhs) && sameSyms(tp2, rhs)
186+
!skipRetry && isSameType(lhs, rhs)
187+
}
184188

185189
/* Here we highlight those unfortunate type-like constructs which
186190
* are hidden bundles of mutable state, cruising the type system picking
@@ -360,10 +364,18 @@ trait TypeComparers {
360364
devWarning(s"HK subtype check on $tp1 and $tp2, but both don't normalize to polytypes:\n tp1=${tp_s(ntp1)}\n tp2=${tp_s(ntp2)}")
361365
false
362366
}
367+
def isTrivialTypeRef(tp: Type) = tp match {
368+
case TypeRef(pre, sym, Nil) if pre.isTrivial && !sym.isAliasType => true
369+
case _ => false
370+
}
363371

364372
( (tp1.typeSymbol eq NothingClass) // @M Nothing is subtype of every well-kinded type
365373
|| (tp2.typeSymbol eq AnyClass) // @M Any is supertype of every well-kinded type (@PP: is it? What about continuations plugin?)
366-
|| isSub(tp1.normalize, tp2.normalize) && annotationsConform(tp1, tp2) // @M! normalize reduces higher-kinded case to PolyType's
374+
|| (if (isTrivialTypeRef(tp1) && isTrivialTypeRef(tp2)) {
375+
tp1.typeSymbolDirect.isNonBottomSubClass(tp2.typeSymbolDirect) // OPT faster than comparing eta-expanded types
376+
} else
377+
isSub(tp1.normalize, tp2.normalize) && annotationsConform(tp1, tp2) // @M! normalize reduces higher-kinded case to PolyType's
378+
)
367379
)
368380
}
369381

0 commit comments

Comments
 (0)
0