Skip to content

Commit b30b157

Browse files
committed
More tweaks for performance
1 parent 5d200df commit b30b157

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

compiler/src/dotty/tools/dotc/transform/SpecializeFunctions.scala

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ class SpecializeFunctions extends MiniPhase with InfoTransformer {
2525
while (arity < 3) {
2626
val func = defn.FunctionClass(arity)
2727
if (tp.derivesFrom(func)) {
28-
lazy val paramTypes = tp.cls.typeRef.baseType(func).argInfos
28+
var specializedMethodName: Name = null
2929
def isSpecializable =
30-
defn.isSpecializableFunction(
31-
sym.asClass,
32-
paramTypes.init,
33-
paramTypes.last
34-
)
30+
val paramTypes = tp.cls.typeRef.baseType(func).argInfos
31+
val argTypes = paramTypes.init
32+
val retType = paramTypes.last
33+
defn.isSpecializableFunction(sym.asClass, argTypes, retType) && {
34+
specializedMethodName = nme.apply.specializedFunction(retType, argTypes)
35+
true
36+
}
3537

3638
val apply = tp.decls.lookup(nme.apply)
3739
if (apply.exists && isSpecializable) {
38-
val specializedMethodName = specializedName(nme.apply, paramTypes)
3940
val applySpecialized = newSymbol(
4041
sym,
4142
specializedMethodName,
@@ -78,7 +79,7 @@ class SpecializeFunctions extends MiniPhase with InfoTransformer {
7879
val paramTypes = ddef.vparamss.head.map(_.symbol.info)
7980
val retType = ddef.tpe.widen.finalResultType
8081

81-
val specName = specializedName(nme.apply, paramTypes :+ retType)
82+
val specName = nme.apply.specializedFunction(retType, paramTypes)
8283
val specializedApply = cls.info.decls.lookup(specName)
8384
if (specializedApply.exists) {
8485
val specializedDecl =
@@ -106,17 +107,18 @@ class SpecializeFunctions extends MiniPhase with InfoTransformer {
106107
override def transformApply(tree: Apply)(using Context) =
107108
tree match {
108109
case Apply(fun, args) if fun.symbol.name == nme.apply =>
109-
val paramTypes = (fun.tpe.widen.firstParamTypes :+ tree.tpe).map(_.widenSingleton.dealias)
110+
val argTypes = fun.tpe.widen.firstParamTypes.map(_.widenSingleton.dealias)
111+
val retType = tree.tpe.widenSingleton.dealias
110112
val isSpecializable =
111113
defn.isSpecializableFunction(
112114
fun.symbol.owner.asClass,
113-
paramTypes.init,
114-
paramTypes.last
115+
argTypes,
116+
retType
115117
)
116118

117-
if (!isSpecializable || paramTypes.exists(_.isInstanceOf[ExprType])) return tree
119+
if (!isSpecializable || argTypes.exists(_.isInstanceOf[ExprType])) return tree
118120

119-
val specializedApply = specializedName(nme.apply, paramTypes)
121+
val specializedApply = nme.apply.specializedFunction(retType, argTypes)
120122
val newSel = fun match {
121123
case Select(qual, _) =>
122124
qual.select(specializedApply)
@@ -134,9 +136,6 @@ class SpecializeFunctions extends MiniPhase with InfoTransformer {
134136
case _ => tree
135137
}
136138

137-
private def specializedName(name: Name, args: List[Type])(using Context) =
138-
name.specializedFunction(args.last, args.init)
139-
140139
private def derivesFromFn012(sym: Symbol)(using Context): Boolean =
141140
sym.derivesFrom(defn.FunctionClass(0)) ||
142141
sym.derivesFrom(defn.FunctionClass(1)) ||

0 commit comments

Comments
 (0)