Skip to content

Commit bbef119

Browse files
committed
Performance tweeak
- Fix typo - Use StringBuilder instead of StringBuffer (thanks @smarter) StringBuffer is synchronized thus is slower.
1 parent 1b16557 commit bbef119

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Compiler {
8585
new SeqLiterals, // Express vararg arguments as arrays
8686
new InterceptedMethods, // Special handling of `==`, `|=`, `getClass` methods
8787
new Getters, // Replace non-private vals and vars with getter defs (fields are added later)
88-
// new SpecializeFunctions, // Specialized Function{0,1,2} by replacing super with specialized super
88+
new SpecializeFunctions, // Specialized Function{0,1,2} by replacing super with specialized super
8989
new LiftTry, // Put try expressions that might execute on non-empty stacks into their own methods
9090
new CollectNullableFields, // Collect fields that can be nulled out after use in lazy initialization
9191
new ElimOuterSelect, // Expand outer selections

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,13 +1188,6 @@ class Definitions {
11881188

11891189
def isBottomClassAfterErasure(cls: Symbol): Boolean = cls == NothingClass || cls == NullClass
11901190

1191-
def isBottomType(tp: Type): Boolean =
1192-
if (ctx.explicitNulls && !ctx.phase.erasedTypes) tp.derivesFrom(NothingClass)
1193-
else isBottomTypeAfterErasure(tp)
1194-
1195-
def isBottomTypeAfterErasure(tp: Type): Boolean =
1196-
tp.derivesFrom(NothingClass) || tp.derivesFrom(NullClass)
1197-
11981191
/** Is any function class where
11991192
* - FunctionXXL
12001193
* - FunctionN for N >= 0
@@ -1459,7 +1452,7 @@ class Definitions {
14591452
for
14601453
r <- Function2SpecializedReturnTypes
14611454
t1 <- Function2SpecializedParamTypes
1462-
t2 <- Function2SpecializedReturnTypes
1455+
t2 <- Function2SpecializedParamTypes
14631456
yield
14641457
nme.apply.specializedFunction(r, List(t1, t2)).asTermName
14651458

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,14 @@ object NameOps {
298298
* `<return type><first type><second type><...>`
299299
*/
300300
def specializedFunction(ret: Type, args: List[Type])(using Context): Name =
301-
name ++ nme.specializedTypeNames.prefix ++
302-
nme.specializedTypeNames.separator ++ defn.typeTag(ret) ++
303-
args.map(defn.typeTag).fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix
301+
val sb = new StringBuilder
302+
sb.append(name.toString)
303+
sb.append(nme.specializedTypeNames.prefix.toString)
304+
sb.append(nme.specializedTypeNames.separator)
305+
sb.append(defn.typeTag(ret).toString)
306+
args.foreach { arg => sb.append(defn.typeTag(arg)) }
307+
sb.append(nme.specializedTypeNames.suffix)
308+
termName(sb.toString)
304309

305310
/** If name length exceeds allowable limit, replace part of it by hash */
306311
def compactified(using Context): TermName = termName(compactify(name.toString))

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
3434
atNextPhase(newSymbol(sym, name, Flags.Method, MethodType(args, ret)))
3535
}
3636

37-
private def specFun0(op: Type => Unit)(using Context): Unit = {
37+
private inline def specFun0(inline op: Type => Unit)(using Context): Unit = {
3838
for (r <- defn.Function0SpecializedReturnTypes) do
3939
op(r)
4040
}
4141

42-
private def specFun1(op: (Type, Type) => Unit)(using Context): Unit = {
42+
private inline def specFun1(inline op: (Type, Type) => Unit)(using Context): Unit = {
4343
for
4444
r <- defn.Function1SpecializedReturnTypes
4545
t1 <- defn.Function1SpecializedParamTypes
4646
do
4747
op(t1, r)
4848
}
4949

50-
private def specFun2(op: (Type, Type, Type) => Unit)(using Context): Unit = {
50+
private inline def specFun2(inline op: (Type, Type, Type) => Unit)(using Context): Unit = {
5151
for
5252
r <- defn.Function2SpecializedReturnTypes
5353
t1 <- defn.Function2SpecializedParamTypes
54-
t2 <- defn.Function2SpecializedReturnTypes
54+
t2 <- defn.Function2SpecializedParamTypes
5555
do
5656
op(t1, t2, r)
5757
}
@@ -63,30 +63,29 @@ class SpecializeApplyMethods extends MiniPhase with InfoTransformer {
6363

6464
/** Add symbols for specialized methods to FunctionN */
6565
override def transformInfo(tp: Type, sym: Symbol)(using Context) = tp match {
66-
// case tp: ClassInfo =>
67-
// if sym == defn.SpecializableFunctions(0) then
68-
// val scope = tp.decls.cloneScope
69-
// specFun0 { r => scope.enter(specApplySymbol(sym, Nil, r)) }
70-
// tp.derivedClassInfo(decls = scope)
66+
case tp: ClassInfo =>
67+
if sym == defn.SpecializableFunctions(0) then
68+
val scope = tp.decls.cloneScope
69+
specFun0 { r => scope.enter(specApplySymbol(sym, Nil, r)) }
70+
tp.derivedClassInfo(decls = scope)
7171

72-
// else if sym == defn.SpecializableFunctions(1) then
73-
// val scope = tp.decls.cloneScope
74-
// specFun1 { (t1, r) => scope.enter(specApplySymbol(sym, List(t1), r)) }
75-
// tp.derivedClassInfo(decls = scope)
72+
else if sym == defn.SpecializableFunctions(1) then
73+
val scope = tp.decls.cloneScope
74+
specFun1 { (t1, r) => scope.enter(specApplySymbol(sym, t1 :: Nil, r)) }
75+
tp.derivedClassInfo(decls = scope)
7676

77-
// else if sym == defn.SpecializableFunctions(2) then
78-
// val scope = tp.decls.cloneScope
79-
// specFun2 { (t1, t2, r) => scope.enter(specApplySymbol(sym, List(t1, t2), r)) }
80-
// tp.derivedClassInfo(decls = scope)
77+
else if sym == defn.SpecializableFunctions(2) then
78+
val scope = tp.decls.cloneScope
79+
specFun2 { (t1, t2, r) => scope.enter(specApplySymbol(sym, t1 :: t2 :: Nil, r)) }
80+
tp.derivedClassInfo(decls = scope)
8181

82-
// else tp
82+
else tp
8383

8484
case _ => tp
8585
}
8686

8787
/** Create bridge methods for FunctionN with specialized applys */
88-
// override def transformTemplate(tree: Template)(using Context) = {
89-
def transformTemplate1(tree: Template)(using Context) = {
88+
override def transformTemplate(tree: Template)(using Context) = {
9089
val cls = tree.symbol.owner.asClass
9190

9291
def synthesizeApply(names: collection.Set[TermName]): Tree = {

0 commit comments

Comments
 (0)