Skip to content

Commit 691bae2

Browse files
committed
Performance improvement: Specialize folds in accumulators.
1 parent afb7444 commit 691bae2

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ object Types {
22162216
abstract class TypeAccumulator[T](implicit ctx: Context) extends ((T, Type) => T) {
22172217
def apply(x: T, tp: Type): T
22182218

2219-
protected def apply(x: T, annot: Annotation): T = x // don't go into annotations
2219+
protected def applyToAnnot(x: T, annot: Annotation): T = x // don't go into annotations
22202220

22212221
protected var variance = 1
22222222

@@ -2237,7 +2237,7 @@ object Types {
22372237

22382238
case tp @ MethodType(pnames, ptypes) =>
22392239
variance = -variance
2240-
val y = (x /: ptypes)(this)
2240+
val y = foldOver(x, ptypes)
22412241
variance = -variance
22422242
this(y, tp.resultType)
22432243

@@ -2246,7 +2246,7 @@ object Types {
22462246

22472247
case tp @ PolyType(pnames) =>
22482248
variance = -variance
2249-
val y = (x /: tp.paramBounds)(this)
2249+
val y = foldOver(x, tp.paramBounds)
22502250
variance = -variance
22512251
this(y, tp.resultType)
22522252

@@ -2275,7 +2275,7 @@ object Types {
22752275
this(this(x, tp.tp1), tp.tp2)
22762276

22772277
case AnnotatedType(annot, underlying) =>
2278-
this(this(x, annot), underlying)
2278+
this(applyToAnnot(x, annot), underlying)
22792279

22802280
case tp: TypeVar =>
22812281
this(x, tp.underlying)
@@ -2288,6 +2288,11 @@ object Types {
22882288

22892289
case _ => x
22902290
}
2291+
2292+
final def foldOver(x: T, ts: List[Type]): T = ts match {
2293+
case t :: ts1 => foldOver(apply(x, t), ts1)
2294+
case nil => x
2295+
}
22912296
}
22922297

22932298
class ExistsAccumulator(p: Type => Boolean)(implicit ctx: Context) extends TypeAccumulator[Boolean] {

src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ object Inferencing {
249249
def map(tm: TypeMap)(implicit ctx: Context): PolyProto =
250250
derivedPolyProto(targs mapConserve tm, tm(resultType))
251251

252-
def fold[T](x: T, ta: TypeAccumulator[T])(implicit ctx: Context): T = ta((x /: targs)(ta), resultType)
252+
def fold[T](x: T, ta: TypeAccumulator[T])(implicit ctx: Context): T =
253+
ta(ta.foldOver(x, targs), resultType)
253254
}
254255

255256
/** A prototype for expressions [] that are known to be functions:

0 commit comments

Comments
 (0)