Skip to content

Commit e9ca9f9

Browse files
committed
Faster infoMayChange
1 parent e5d45d3 commit e9ca9f9

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

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

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,30 @@ class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
5656
op(t1, t2, r)
5757
}
5858

59-
/** Whether the name is possibly specializable?
60-
*
61-
* This is a micro-optimization, as name test is fast and most test would fail.
62-
*/
63-
private def specializableName(name: Name): Boolean =
64-
val arity = name.functionArity
65-
arity >= 0 && arity < 3
66-
6759
override def infoMayChange(sym: Symbol)(using Context) =
68-
specializableName(sym.name)
60+
sym == defn.SpecializableFunctions(0)
61+
|| sym == defn.SpecializableFunctions(1)
62+
|| sym == defn.SpecializableFunctions(2)
6963

7064
/** Add symbols for specialized methods to FunctionN */
7165
override def transformInfo(tp: Type, sym: Symbol)(using Context) = tp match {
72-
case tp: ClassInfo if defn.isPlainFunctionClass(sym) =>
73-
sym.name.functionArity match {
74-
case 0 =>
66+
case tp: ClassInfo =>
67+
if sym == defn.SpecializableFunctions(0) then
7568
val scope = tp.decls.cloneScope
7669
specFun0 { r => scope.enter(specApplySymbol(sym, Nil, r)) }
7770
tp.derivedClassInfo(decls = scope)
7871

79-
case 1 =>
72+
else if sym == defn.SpecializableFunctions(1) then
8073
val scope = tp.decls.cloneScope
8174
specFun1 { (t1, r) => scope.enter(specApplySymbol(sym, List(t1), r)) }
8275
tp.derivedClassInfo(decls = scope)
8376

84-
case 2 =>
77+
else if sym == defn.SpecializableFunctions(2) then
8578
val scope = tp.decls.cloneScope
8679
specFun2 { (t1, t2, r) => scope.enter(specApplySymbol(sym, List(t1, t2), r)) }
8780
tp.derivedClassInfo(decls = scope)
8881

89-
case _ => tp
90-
}
82+
else tp
9183

9284
case _ => tp
9385
}
@@ -96,8 +88,6 @@ class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
9688
override def transformTemplate(tree: Template)(using Context) = {
9789
val cls = tree.symbol.owner.asClass
9890

99-
if (!specializableName(cls.name) || !defn.isPlainFunctionClass(cls)) return tree
100-
10191
def synthesizeApply(names: collection.Set[TermName]): Tree = {
10292
val applyBuf = new mutable.ListBuffer[DefDef]
10393
names.foreach { name =>
@@ -116,18 +106,13 @@ class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
116106
cpy.Template(tree)(body = tree.body ++ applyBuf)
117107
}
118108

119-
cls.name.functionArity match {
120-
case 0 =>
121-
synthesizeApply(defn.Function0SpecializedApplyNames)
122-
123-
case 1 =>
124-
synthesizeApply(defn.Function1SpecializedApplyNames)
125-
126-
case 2 =>
127-
synthesizeApply(defn.Function2SpecializedApplyNames)
128-
129-
case _ =>
130-
tree
131-
}
109+
if cls == defn.SpecializableFunctions(0) then
110+
synthesizeApply(defn.Function0SpecializedApplyNames)
111+
else if cls == defn.SpecializableFunctions(1) then
112+
synthesizeApply(defn.Function1SpecializedApplyNames)
113+
else if cls == defn.SpecializableFunctions(2) then
114+
synthesizeApply(defn.Function2SpecializedApplyNames)
115+
else
116+
tree
132117
}
133118
}

0 commit comments

Comments
 (0)