Skip to content

Commit 2cefd3e

Browse files
committed
Performance tweak: use vector for effects and potentials
1 parent 07a2e41 commit 2cefd3e

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

compiler/src/dotty/tools/dotc/transform/init/Checking.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,14 @@ object Checking {
326326

327327
case Fun(pots, effs) =>
328328
val name = sym.name.toString
329-
if (name == "apply") Summary(pots, Effects.empty)
330-
else if (name == "tupled") Summary(Set(pot1), Effects.empty)
329+
if (name == "apply") Summary(pots)
330+
else if (name == "tupled") Summary(pot1)
331331
else if (name == "curried") {
332332
val arity = defn.functionArity(sym.info.finalResultType)
333-
val pots = (1 until arity).foldLeft(Set(pot1)) { (acc, i) =>
334-
Set(Fun(acc, Effects.empty)(pot1.source))
333+
val pots = (1 until arity).foldLeft(Vector(pot1)) { (acc, i) =>
334+
Vector(Fun(acc, Effects.empty)(pot1.source))
335335
}
336-
Summary(pots, Effects.empty)
336+
Summary(pots)
337337
}
338338
else Summary.empty
339339

compiler/src/dotty/tools/dotc/transform/init/Effects.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import core.Contexts._
1212
import Potentials._
1313

1414
object Effects {
15-
type Effects = Set[Effect]
16-
val empty: Effects = Set.empty
15+
type Effects = Vector[Effect]
16+
val empty: Effects = Vector.empty
1717

1818
def show(effs: Effects)(using Context): String =
1919
effs.map(_.show).mkString(", ")
@@ -25,6 +25,8 @@ object Effects {
2525
def show(using Context): String
2626

2727
def source: Tree
28+
29+
def toEffs: Effects = Vector(this)
2830
}
2931

3032
/** An effect means that a value that's possibly under initialization
@@ -58,8 +60,6 @@ object Effects {
5860

5961
// ------------------ operations on effects ------------------
6062

61-
extension (eff: Effect) def toEffs: Effects = Effects.empty + eff
62-
6363
def asSeenFrom(eff: Effect, thisValue: Potential)(implicit env: Env): Effect =
6464
trace(eff.show + " asSeenFrom " + thisValue.show + ", current = " + currentClass.show, init, _.asInstanceOf[Effect].show) { eff match {
6565
case Promote(pot) =>

compiler/src/dotty/tools/dotc/transform/init/Potentials.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import Types._, Symbols._, Contexts._
1515
import Effects._, Summary._
1616

1717
object Potentials {
18-
type Potentials = Set[Potential]
19-
val empty: Potentials = Set.empty
18+
type Potentials = Vector[Potential]
19+
val empty: Potentials = Vector.empty
2020

2121
def show(pots: Potentials)(using Context): String =
2222
pots.map(_.show).mkString(", ")
@@ -31,6 +31,8 @@ object Potentials {
3131

3232
def show(using Context): String
3333
def source: Tree
34+
35+
def toPots: Potentials = Vector(this)
3436
}
3537

3638
sealed trait Refinable extends Potential {
@@ -153,8 +155,6 @@ object Potentials {
153155

154156
// ------------------ operations on potentials ------------------
155157

156-
extension (pot: Potential) def toPots: Potentials = Potentials.empty + pot
157-
158158
/** Selection on a set of potentials
159159
*
160160
* @param ignoreSelectEffect Where selection effects should be ignored

compiler/src/dotty/tools/dotc/transform/init/Summarization.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ object Summarization {
304304
def parentArgEffsWithInit(stats: List[Tree], ctor: Symbol, source: Tree): Effects =
305305
val init =
306306
if env.canIgnoreMethod(ctor) then Effects.empty
307-
else Effects.empty + MethodCall(ThisRef()(source), ctor)(source)
307+
else Effects.empty :+ MethodCall(ThisRef()(source), ctor)(source)
308308
stats.foldLeft(init) { (acc, stat) =>
309309
val summary = Summarization.analyze(stat)
310310
acc ++ summary.effs
@@ -334,7 +334,7 @@ object Summarization {
334334
if tref.prefix == NoPrefix then Effects.empty
335335
else Summarization.analyze(tref.prefix, ref).effs
336336

337-
prefixEff + MethodCall(ThisRef()(ref), ctor)(ref)
337+
prefixEff :+ MethodCall(ThisRef()(ref), ctor)(ref)
338338
}
339339
})
340340
}

compiler/src/dotty/tools/dotc/transform/init/Summary.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ case class Summary(pots: Potentials, effs: Effects) {
1818
Summary(pots ++ summary2.pots, this.effs ++ summary2.effs)
1919

2020
def +(pot: Potential): Summary =
21-
Summary(pots + pot, effs)
21+
Summary(pots :+ pot, effs)
2222

2323
def +(eff: Effect): Summary =
24-
Summary(pots, effs + eff)
24+
Summary(pots, effs :+ eff)
2525

2626
def dropPotentials: Summary =
2727
Summary(Potentials.empty, effs)
@@ -44,14 +44,14 @@ case class Summary(pots: Potentials, effs: Effects) {
4444
object Summary {
4545
val empty: Summary = Summary(Potentials.empty, Effects.empty)
4646

47-
def apply(pots: Potentials): Summary = new Summary(pots, Effects.empty)
47+
def apply(pots: Potentials): Summary = empty ++ pots
4848

4949
@targetName("withEffects")
50-
def apply(effs: Effects): Summary = new Summary(Potentials.empty, effs)
50+
def apply(effs: Effects): Summary = empty ++ effs
5151

52-
def apply(pot: Potential): Summary = new Summary(Potentials.empty + pot, Effects.empty)
52+
def apply(pot: Potential): Summary = empty + pot
5353

54-
def apply(eff: Effect): Summary = new Summary(Potentials.empty, Effects.empty + eff)
54+
def apply(eff: Effect): Summary = empty + eff
5555
}
5656

5757
/** Summary of class.

0 commit comments

Comments
 (0)