Skip to content

Commit cdc91e2

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 15e60e8 commit cdc91e2

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
@@ -1146,13 +1146,13 @@ object SymDenotations {
11461146
case tp: NamedType => hasSkolems(tp.prefix)
11471147
case tp: RefinedType => hasSkolems(tp.parent) || hasSkolems(tp.refinedInfo)
11481148
case tp: RecType => hasSkolems(tp.parent)
1149-
case tp: LambdaType => tp.paramInfos.exists(hasSkolems) || hasSkolems(tp.resType)
1149+
case tp: TypeBounds => hasSkolems(tp.lo) || hasSkolems(tp.hi)
1150+
case tp: TypeVar => hasSkolems(tp.inst)
11501151
case tp: ExprType => hasSkolems(tp.resType)
11511152
case tp: HKApply => hasSkolems(tp.tycon) || tp.args.exists(hasSkolems)
1153+
case tp: LambdaType => tp.paramInfos.exists(hasSkolems) || hasSkolems(tp.resType)
11521154
case tp: AndOrType => hasSkolems(tp.tp1) || hasSkolems(tp.tp2)
1153-
case tp: TypeBounds => hasSkolems(tp.lo) || hasSkolems(tp.hi)
11541155
case tp: AnnotatedType => hasSkolems(tp.tpe)
1155-
case tp: TypeVar => hasSkolems(tp.inst)
11561156
case _ => false
11571157
}
11581158

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

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

2396+
protected def prefixString: String
2397+
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
2398+
}
2399+
2400+
abstract class HKLambda extends CachedProxyType with LambdaType {
2401+
final override def underlying(implicit ctx: Context) = resType
2402+
2403+
final override def computeHash = doHash(paramNames, resType, paramInfos)
2404+
2405+
// Defined here instead of in LambdaType for efficiency
23962406
final override def equals(that: Any) = that match {
2397-
case that: LambdaType =>
2407+
case that: HKLambda =>
23982408
this.paramNames == that.paramNames &&
23992409
this.paramInfos == that.paramInfos &&
24002410
this.resType == that.resType &&
24012411
(this.companion eq that.companion)
24022412
case _ =>
24032413
false
24042414
}
2405-
2406-
protected def prefixString: String
2407-
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
2408-
}
2409-
2410-
abstract class HKLambda extends CachedProxyType with LambdaType {
2411-
final override def computeHash = doHash(paramNames, resType, paramInfos)
2412-
final override def underlying(implicit ctx: Context) = resType
24132415
}
24142416

24152417
abstract class MethodOrPoly extends CachedGroundType with LambdaType with TermType {
24162418
final override def computeHash = doHash(paramNames, resType, paramInfos)
2419+
2420+
// Defined here instead of in LambdaType for efficiency
2421+
final override def equals(that: Any) = that match {
2422+
case that: MethodOrPoly =>
2423+
this.paramNames == that.paramNames &&
2424+
this.paramInfos == that.paramInfos &&
2425+
this.resType == that.resType &&
2426+
(this.companion eq that.companion)
2427+
case _ =>
2428+
false
2429+
}
24172430
}
24182431

24192432
trait TermLambda extends LambdaType { thisLambdaType =>
@@ -3543,6 +3556,26 @@ object Types {
35433556
variance = -variance
35443557
derivedTypeBounds(tp, lo1, this(tp.hi))
35453558

3559+
case tp: RecType =>
3560+
derivedRecType(tp, this(tp.parent))
3561+
3562+
case tp: TypeVar =>
3563+
val inst = tp.instanceOpt
3564+
if (inst.exists) apply(inst) else tp
3565+
3566+
case tp: HKApply =>
3567+
def mapArg(arg: Type, tparam: ParamInfo): Type = {
3568+
val saved = variance
3569+
variance *= tparam.paramVariance
3570+
try this(arg)
3571+
finally variance = saved
3572+
}
3573+
derivedAppliedType(tp, this(tp.tycon),
3574+
tp.args.zipWithConserve(tp.typeParams)(mapArg))
3575+
3576+
case tp: ExprType =>
3577+
derivedExprType(tp, this(tp.resultType))
3578+
35463579
case tp: LambdaType =>
35473580
def mapOverLambda = {
35483581
variance = -variance
@@ -3552,12 +3585,6 @@ object Types {
35523585
}
35533586
mapOverLambda
35543587

3555-
case tp: ExprType =>
3556-
derivedExprType(tp, this(tp.resultType))
3557-
3558-
case tp: RecType =>
3559-
derivedRecType(tp, this(tp.parent))
3560-
35613588
case tp @ SuperType(thistp, supertp) =>
35623589
derivedSuperType(tp, this(thistp), this(supertp))
35633590

@@ -3567,21 +3594,7 @@ object Types {
35673594
case tp: ClassInfo =>
35683595
mapClassInfo(tp)
35693596

3570-
case tp: TypeVar =>
3571-
val inst = tp.instanceOpt
3572-
if (inst.exists) apply(inst) else tp
3573-
3574-
case tp: HKApply =>
3575-
def mapArg(arg: Type, tparam: ParamInfo): Type = {
3576-
val saved = variance
3577-
variance *= tparam.paramVariance
3578-
try this(arg)
3579-
finally variance = saved
3580-
}
3581-
derivedAppliedType(tp, this(tp.tycon),
3582-
tp.args.zipWithConserve(tp.typeParams)(mapArg))
3583-
3584-
case tp: AndOrType =>
3597+
case tp: AndOrType =>
35853598
derivedAndOrType(tp, this(tp.tp1), this(tp.tp2))
35863599

35873600
case tp: SkolemType =>
@@ -3762,17 +3775,14 @@ object Types {
37623775
this(y, hi)
37633776
}
37643777

3765-
case tp: LambdaType =>
3766-
variance = -variance
3767-
val y = foldOver(x, tp.paramInfos)
3768-
variance = -variance
3769-
this(y, tp.resultType)
3778+
case tp: RecType =>
3779+
this(x, tp.parent)
37703780

37713781
case ExprType(restpe) =>
37723782
this(x, restpe)
37733783

3774-
case tp: RecType =>
3775-
this(x, tp.parent)
3784+
case tp: TypeVar =>
3785+
this(x, tp.underlying)
37763786

37773787
case SuperType(thistp, supertp) =>
37783788
this(this(x, thistp), supertp)
@@ -3797,6 +3807,12 @@ object Types {
37973807
}
37983808
foldArgs(this(x, tycon), tp.typeParams, args)
37993809

3810+
case tp: LambdaType =>
3811+
variance = -variance
3812+
val y = foldOver(x, tp.paramInfos)
3813+
variance = -variance
3814+
this(y, tp.resultType)
3815+
38003816
case tp: AndOrType =>
38013817
this(this(x, tp.tp1), tp.tp2)
38023818

@@ -3806,9 +3822,6 @@ object Types {
38063822
case AnnotatedType(underlying, annot) =>
38073823
this(applyToAnnot(x, annot), underlying)
38083824

3809-
case tp: TypeVar =>
3810-
this(x, tp.underlying)
3811-
38123825
case tp: WildcardType =>
38133826
this(x, tp.optBounds)
38143827

0 commit comments

Comments
 (0)