Skip to content

Commit db05a36

Browse files
committed
Specialize byname functions
I tried to extend specialization to all context functions, not just ones of 0 arity. But that runs into problems for dependent context functions, since the necessary casts get complicated. Since context functions over primitive types are an anti-pattern anyway I don't think we need to optimize this case, after all.
1 parent 937c9d0 commit db05a36

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,8 @@ class Definitions {
15461546
new PerRun(Function2SpecializedReturnTypes.map(_.symbol))
15471547

15481548
def isSpecializableFunction(cls: ClassSymbol, paramTypes: List[Type], retType: Type)(using Context): Boolean =
1549-
paramTypes.length <= 2 && cls.derivesFrom(FunctionClass(paramTypes.length))
1549+
paramTypes.length <= 2
1550+
&& (cls.derivesFrom(FunctionClass(paramTypes.length)) || isByNameClass(cls))
15501551
&& isSpecializableFunctionSAM(paramTypes, retType)
15511552

15521553
/** If the Single Abstract Method of a Function class has this type, is it specializable? */

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,15 @@ class SpecializeFunctions extends MiniPhase {
8080
val specializedApply = nme.apply.specializedFunction(retType, argTypes)
8181
val newSel = fun match
8282
case Select(qual, _) =>
83-
qual.select(specializedApply)
83+
val qual1 = qual.tpe.widen match
84+
case ByNameType(res) =>
85+
// Need to cast to regular function, since specialied apply methods
86+
// are not members of ContextFunction0. The cast will be eliminated in
87+
// erasure.
88+
qual.cast(defn.FunctionOf(Nil, res))
89+
case _ =>
90+
qual
91+
qual1.select(specializedApply)
8492
case _ =>
8593
(fun.tpe: @unchecked) match
8694
case TermRef(prefix: ThisType, name) =>

0 commit comments

Comments
 (0)