Skip to content

Commit a1ce848

Browse files
committed
Optimize typeParams
Direct encoding of lazy val; make typeParams tail recursive.
1 parent 57beb7c commit a1ce848

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import util.Positions.Position
1818
import config.Printers.{core, typr}
1919
import collection.mutable
2020
import dotty.tools.dotc.config.Config
21+
import annotation.tailrec
2122
import java.util.NoSuchElementException
2223

2324
object TypeApplications {
@@ -168,31 +169,32 @@ class TypeApplications(val self: Type) extends AnyVal {
168169
* any type parameter that is-rebound by the refinement.
169170
*/
170171
final def typeParams(implicit ctx: Context): List[TypeParamInfo] = /*>|>*/ track("typeParams") /*<|<*/ {
171-
self match {
172+
@tailrec def recur(tp: Type): List[TypeParamInfo] = tp match {
172173
case self: TypeRef =>
173174
val tsym = self.symbol
174175
if (tsym.isClass) tsym.typeParams
175-
else if (!tsym.exists) self.info.typeParams
176+
else if (!tsym.exists) recur(self.info)
176177
else tsym.infoOrCompleter match {
177178
case info: LazyType => info.completerTypeParams(tsym)
178-
case info => info.typeParams
179+
case info => recur(info)
179180
}
180181
case self: AppliedType =>
181182
if (self.tycon.typeSymbol.isClass) Nil
182-
else self.superType.typeParams
183+
else recur(self.superType)
183184
case self: ClassInfo =>
184185
self.cls.typeParams
185186
case self: HKTypeLambda =>
186187
self.typeParams
187188
case _: SingletonType | _: RefinedType | _: RecType =>
188189
Nil
189190
case self: WildcardType =>
190-
self.optBounds.typeParams
191+
recur(self.optBounds)
191192
case self: TypeProxy =>
192-
self.superType.typeParams
193+
recur(self.superType)
193194
case _ =>
194195
Nil
195196
}
197+
recur(self)
196198
}
197199

198200
/** If `self` is a higher-kinded type, its type parameters, otherwise Nil */

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -3007,8 +3007,13 @@ object Types {
30073007

30083008
def newParamRef(n: Int) = new TypeParamRef(this, n) {}
30093009

3010-
lazy val typeParams: List[LambdaParam] =
3011-
paramNames.indices.toList.map(new LambdaParam(this, _))
3010+
private var myTypeParams: List[LambdaParam] = null
3011+
3012+
lazy val typeParams: List[LambdaParam] = {
3013+
if (myTypeParams == null)
3014+
myTypeParams = paramNames.indices.toList.map(new LambdaParam(this, _))
3015+
myTypeParams
3016+
}
30123017

30133018
def derivedLambdaAbstraction(paramNames: List[TypeName], paramInfos: List[TypeBounds], resType: Type)(implicit ctx: Context): Type =
30143019
resType match {

0 commit comments

Comments
 (0)