Skip to content

Commit 84e155d

Browse files
committed
Avoid more collection ops in frontend
1 parent 92a5f8c commit 84e155d

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,13 @@ object SymDenotations {
15561556
completeChildrenIn(companionClass)
15571557
setFlag(ChildrenQueried)
15581558

1559-
annotations.collect { case Annotation.Child(child) => child }.reverse
1559+
/** The children recorded in `annots`, in reverse order */
1560+
def getChildren(annots: List[Annotation], acc: List[Symbol]): List[Symbol] = annots match
1561+
case Annotation.Child(child) :: annots1 => getChildren(annots1, child :: acc)
1562+
case _ :: annots1 => getChildren(annots1, acc)
1563+
case nil => acc
1564+
1565+
getChildren(annotations, Nil)
15601566
end children
15611567
}
15621568

compiler/src/dotty/tools/dotc/core/tasty/NameBuffer.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ class NameBuffer extends TastyBuffer(10000) {
9797
}
9898
}
9999

100-
override def assemble(): Unit = {
100+
override def assemble(): Unit =
101101
var i = 0
102-
for ((name, ref) <- nameRefs) {
102+
val nr = nameRefs.iterator
103+
while nr.hasNext do
104+
val (name, ref) = nr.next()
103105
assert(ref.index == i)
104106
i += 1
105107
pickleNameContents(name)
106-
}
107-
}
108108
}
109109

110110
object NameBuffer {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ object SymUtils {
2222

2323
extension (self: Symbol) {
2424

25-
/** All traits implemented by a class or trait except for those inherited through the superclass. */
25+
/** All traits implemented by a class or trait except for those inherited
26+
* through the superclass. Traits are given in the order they appear in the
27+
* parents clause (which is the reverse of their order in baseClasses)
28+
*/
2629
def directlyInheritedTraits(using Context): List[ClassSymbol] = {
2730
val superCls = self.asClass.superClass
2831
val baseClasses = self.asClass.baseClasses
2932
if (baseClasses.isEmpty) Nil
30-
else baseClasses.tail.takeWhile(_ ne superCls).reverse
33+
else
34+
def recur(bcs: List[ClassSymbol], acc: List[ClassSymbol]): List[ClassSymbol] = bcs match
35+
case bc :: bcs1 => if bc eq superCls then acc else recur(bcs1, bc :: acc)
36+
case nil => acc
37+
recur(baseClasses.tail, Nil)
3138
}
3239

3340
/** All traits implemented by a class, except for those inherited through the superclass.

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,9 @@ object RefChecks {
491491
*/
492492
def missingTermSymbols: List[Symbol] =
493493
val buf = new mutable.ListBuffer[Symbol]
494-
for bc <- clazz.baseClasses
495-
sym <- bc.info.decls.toList
496-
if sym.is(DeferredTerm) && !isImplemented(sym) && !ignoreDeferred(sym)
497-
do buf += sym
494+
for bc <- clazz.baseClasses; sym <- bc.info.decls.toList do
495+
if sym.is(DeferredTerm) && !isImplemented(sym) && !ignoreDeferred(sym)
496+
buf += sym
498497
buf.toList
499498

500499
// 2. Check that only abstract classes have deferred members

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,21 +1224,24 @@ class Typer extends Namer
12241224
}
12251225

12261226
val desugared =
1227-
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
1227+
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head))
12281228
val isGenericTuple =
12291229
protoFormals.head.derivesFrom(defn.TupleClass)
12301230
&& !defn.isTupleClass(protoFormals.head.typeSymbol)
12311231
desugar.makeTupledFunction(params, fnBody, isGenericTuple)
1232-
}
1233-
else {
1234-
val inferredParams: List[untpd.ValDef] =
1235-
for ((param, i) <- params.zipWithIndex) yield
1236-
if (!param.tpt.isEmpty) param
1237-
else cpy.ValDef(param)(
1238-
tpt = untpd.TypeTree(
1239-
inferredParamType(param, protoFormal(i)).translateFromRepeated(toArray = false)))
1240-
desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual)
1241-
}
1232+
else
1233+
def inferredParams(params: List[untpd.ValDef], idx: Int): List[untpd.ValDef] = params match
1234+
case param :: rest =>
1235+
val param1 =
1236+
if !param.tpt.isEmpty then param
1237+
else cpy.ValDef(param)(
1238+
tpt = untpd.TypeTree(
1239+
inferredParamType(param, protoFormal(idx)).translateFromRepeated(toArray = false)))
1240+
param1 :: inferredParams(rest, idx + 1)
1241+
case nil =>
1242+
Nil
1243+
desugar.makeClosure(inferredParams(params, 0), fnBody, resultTpt, isContextual)
1244+
12421245
typed(desugared, pt)
12431246
}
12441247

0 commit comments

Comments
 (0)