Skip to content

Commit 65746c8

Browse files
committed
Small optimization
1 parent ad8d7e8 commit 65746c8

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Compiler {
6969
new CacheAliasImplicits, // Cache RHS of parameterless alias implicits
7070
new ByNameClosures, // Expand arguments to by-name parameters to closures
7171
new HoistSuperArgs, // Hoist complex arguments of supercalls to enclosing scope
72-
new SpecializedApplyMethods,// Adds specialized methods to FunctionN
72+
new SpecializeApplyMethods, // Adds specialized methods to FunctionN
7373
new RefChecks) :: // Various checks mostly related to abstract members and overriding
7474
List(new ElimOpaque, // Turn opaque into normal aliases
7575
new TryCatchPatterns, // Compile cases in try/catch

compiler/src/dotty/tools/dotc/transform/SpecializedApplyMethods.scala renamed to compiler/src/dotty/tools/dotc/transform/SpecializeApplyMethods.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import scala.collection.mutable
1717
* can hardcode them. This should, however be removed once we're using a
1818
* different standard library.
1919
*/
20-
class SpecializedApplyMethods extends MiniPhase with InfoTransformer {
20+
class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
2121
import ast.tpd._
2222

23-
val phaseName = "specializedApplyMethods"
23+
val phaseName = "specializeApplyMethods"
2424

2525
override def isEnabled(using Context): Boolean =
2626
!ctx.settings.scalajs.value
@@ -56,6 +56,17 @@ class SpecializedApplyMethods 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+
67+
override def infoMayChange(sym: Symbol)(using Context) =
68+
specializableName(sym.name)
69+
5970
/** Add symbols for specialized methods to FunctionN */
6071
override def transformInfo(tp: Type, sym: Symbol)(using Context) = tp match {
6172
case tp: ClassInfo if defn.isPlainFunctionClass(sym) =>
@@ -85,7 +96,7 @@ class SpecializedApplyMethods extends MiniPhase with InfoTransformer {
8596
override def transformTemplate(tree: Template)(using Context) = {
8697
val cls = tree.symbol.owner.asClass
8798

88-
if (!defn.isPlainFunctionClass(cls)) return tree
99+
if (!specializableName(cls.name) || !defn.isPlainFunctionClass(cls)) return tree
89100

90101
def synthesizeApply(names: collection.Set[TermName]): Tree = {
91102
val applyBuf = new mutable.ListBuffer[DefDef]

0 commit comments

Comments
 (0)