Skip to content

Commit d3fe81f

Browse files
committed
Refactorings for efficiency
- split LambdaType.equals into two equals so that tests are more specific (also avoids type testing against a trait) - re-order cases in some pattern matches with the aim to (1) move common cases earlier, (2) move more expensive trait type tests later.
1 parent 3f8ad70 commit d3fe81f

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,13 +1141,13 @@ object SymDenotations {
11411141
case tp: NamedType => hasSkolems(tp.prefix)
11421142
case tp: RefinedType => hasSkolems(tp.parent) || hasSkolems(tp.refinedInfo)
11431143
case tp: RecType => hasSkolems(tp.parent)
1144-
case tp: LambdaType => tp.paramInfos.exists(hasSkolems) || hasSkolems(tp.resType)
1144+
case tp: TypeBounds => hasSkolems(tp.lo) || hasSkolems(tp.hi)
1145+
case tp: TypeVar => hasSkolems(tp.inst)
11451146
case tp: ExprType => hasSkolems(tp.resType)
11461147
case tp: HKApply => hasSkolems(tp.tycon) || tp.args.exists(hasSkolems)
1148+
case tp: LambdaType => tp.paramInfos.exists(hasSkolems) || hasSkolems(tp.resType)
11471149
case tp: AndOrType => hasSkolems(tp.tp1) || hasSkolems(tp.tp2)
1148-
case tp: TypeBounds => hasSkolems(tp.lo) || hasSkolems(tp.hi)
11491150
case tp: AnnotatedType => hasSkolems(tp.tpe)
1150-
case tp: TypeVar => hasSkolems(tp.inst)
11511151
case _ => false
11521152
}
11531153

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

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,27 +2389,40 @@ object Types {
23892389
x => paramInfos.mapConserve(_.subst(this, x).asInstanceOf[PInfo]),
23902390
x => resType.subst(this, x))
23912391

2392+
protected def prefixString: String
2393+
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
2394+
}
2395+
2396+
abstract class HKLambda extends CachedProxyType with LambdaType {
2397+
final override def underlying(implicit ctx: Context) = resType
2398+
2399+
final override def computeHash = doHash(paramNames, resType, paramInfos)
2400+
2401+
// Defined here instead of in LambdaType for efficiency
23922402
final override def equals(that: Any) = that match {
2393-
case that: LambdaType =>
2403+
case that: HKLambda =>
23942404
this.paramNames == that.paramNames &&
23952405
this.paramInfos == that.paramInfos &&
23962406
this.resType == that.resType &&
23972407
(this.companion eq that.companion)
23982408
case _ =>
23992409
false
24002410
}
2401-
2402-
protected def prefixString: String
2403-
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
2404-
}
2405-
2406-
abstract class HKLambda extends CachedProxyType with LambdaType {
2407-
final override def computeHash = doHash(paramNames, resType, paramInfos)
2408-
final override def underlying(implicit ctx: Context) = resType
24092411
}
24102412

24112413
abstract class MethodOrPoly extends CachedGroundType with LambdaType with TermType {
24122414
final override def computeHash = doHash(paramNames, resType, paramInfos)
2415+
2416+
// Defined here instead of in LambdaType for efficiency
2417+
final override def equals(that: Any) = that match {
2418+
case that: MethodOrPoly =>
2419+
this.paramNames == that.paramNames &&
2420+
this.paramInfos == that.paramInfos &&
2421+
this.resType == that.resType &&
2422+
(this.companion eq that.companion)
2423+
case _ =>
2424+
false
2425+
}
24132426
}
24142427

24152428
trait TermLambda extends LambdaType { thisLambdaType =>
@@ -3539,6 +3552,26 @@ object Types {
35393552
variance = -variance
35403553
derivedTypeBounds(tp, lo1, this(tp.hi))
35413554

3555+
case tp: RecType =>
3556+
derivedRecType(tp, this(tp.parent))
3557+
3558+
case tp: TypeVar =>
3559+
val inst = tp.instanceOpt
3560+
if (inst.exists) apply(inst) else tp
3561+
3562+
case tp: HKApply =>
3563+
def mapArg(arg: Type, tparam: ParamInfo): Type = {
3564+
val saved = variance
3565+
variance *= tparam.paramVariance
3566+
try this(arg)
3567+
finally variance = saved
3568+
}
3569+
derivedAppliedType(tp, this(tp.tycon),
3570+
tp.args.zipWithConserve(tp.typeParams)(mapArg))
3571+
3572+
case tp: ExprType =>
3573+
derivedExprType(tp, this(tp.resultType))
3574+
35423575
case tp: LambdaType =>
35433576
def mapOverLambda = {
35443577
variance = -variance
@@ -3548,12 +3581,6 @@ object Types {
35483581
}
35493582
mapOverLambda
35503583

3551-
case tp: ExprType =>
3552-
derivedExprType(tp, this(tp.resultType))
3553-
3554-
case tp: RecType =>
3555-
derivedRecType(tp, this(tp.parent))
3556-
35573584
case tp @ SuperType(thistp, supertp) =>
35583585
derivedSuperType(tp, this(thistp), this(supertp))
35593586

@@ -3563,21 +3590,7 @@ object Types {
35633590
case tp: ClassInfo =>
35643591
mapClassInfo(tp)
35653592

3566-
case tp: TypeVar =>
3567-
val inst = tp.instanceOpt
3568-
if (inst.exists) apply(inst) else tp
3569-
3570-
case tp: HKApply =>
3571-
def mapArg(arg: Type, tparam: ParamInfo): Type = {
3572-
val saved = variance
3573-
variance *= tparam.paramVariance
3574-
try this(arg)
3575-
finally variance = saved
3576-
}
3577-
derivedAppliedType(tp, this(tp.tycon),
3578-
tp.args.zipWithConserve(tp.typeParams)(mapArg))
3579-
3580-
case tp: AndOrType =>
3593+
case tp: AndOrType =>
35813594
derivedAndOrType(tp, this(tp.tp1), this(tp.tp2))
35823595

35833596
case tp: SkolemType =>
@@ -3758,17 +3771,14 @@ object Types {
37583771
this(y, hi)
37593772
}
37603773

3761-
case tp: LambdaType =>
3762-
variance = -variance
3763-
val y = foldOver(x, tp.paramInfos)
3764-
variance = -variance
3765-
this(y, tp.resultType)
3774+
case tp: RecType =>
3775+
this(x, tp.parent)
37663776

37673777
case ExprType(restpe) =>
37683778
this(x, restpe)
37693779

3770-
case tp: RecType =>
3771-
this(x, tp.parent)
3780+
case tp: TypeVar =>
3781+
this(x, tp.underlying)
37723782

37733783
case SuperType(thistp, supertp) =>
37743784
this(this(x, thistp), supertp)
@@ -3793,6 +3803,12 @@ object Types {
37933803
}
37943804
foldArgs(this(x, tycon), tp.typeParams, args)
37953805

3806+
case tp: LambdaType =>
3807+
variance = -variance
3808+
val y = foldOver(x, tp.paramInfos)
3809+
variance = -variance
3810+
this(y, tp.resultType)
3811+
37963812
case tp: AndOrType =>
37973813
this(this(x, tp.tp1), tp.tp2)
37983814

@@ -3802,9 +3818,6 @@ object Types {
38023818
case AnnotatedType(underlying, annot) =>
38033819
this(applyToAnnot(x, annot), underlying)
38043820

3805-
case tp: TypeVar =>
3806-
this(x, tp.underlying)
3807-
38083821
case tp: WildcardType =>
38093822
this(x, tp.optBounds)
38103823

0 commit comments

Comments
 (0)