Skip to content

Commit 0c83123

Browse files
committed
Space: Cache simplify
1 parent 917b6ea commit 0c83123

File tree

1 file changed

+12
-6
lines changed
  • compiler/src/dotty/tools/dotc/transform/patmat

1 file changed

+12
-6
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ sealed trait Space:
6666
else trace(s"isSubspace(${show(this)}, ${show(b)})", debug) {
6767
isSubspaceCache.getOrElseUpdate(b, computeIsSubspace(this, b))
6868
}
69+
70+
private var mySimplify: Space = _
71+
72+
def simplify(using Context): Space =
73+
if mySimplify == null then mySimplify = SpaceEngine.computeSimplify(this)
74+
mySimplify
6975
end Space
7076

7177
/** Empty space */
@@ -98,8 +104,13 @@ case class Or(spaces: Seq[Space]) extends Space
98104
object SpaceEngine {
99105
import tpd._
100106

107+
def simplify(space: Space)(using Context): Space = space.simplify
108+
def isSubspace(a: Space, b: Space)(using Context): Boolean = a.isSubspace(b)
109+
def canDecompose(typ: Typ)(using Context): Boolean = typ.canDecompose
110+
def decompose(typ: Typ)(using Context): List[Typ] = typ.decompose
111+
101112
/** Simplify space such that a space equal to `Empty` becomes `Empty` */
102-
def simplify(space: Space)(using Context): Space = trace(s"simplify ${show(space)} --> ", debug, show)(space match {
113+
def computeSimplify(space: Space)(using Context): Space = trace(s"simplify ${show(space)} --> ", debug, show)(space match {
103114
case Prod(tp, fun, spaces) =>
104115
val sps = spaces.mapconserve(simplify)
105116
if sps.contains(Empty) then Empty
@@ -150,8 +161,6 @@ object SpaceEngine {
150161
}
151162

152163
/** Is `a` a subspace of `b`? Equivalent to `simplify(simplify(a) - simplify(b)) == Empty`, but faster */
153-
def isSubspace(a: Space, b: Space)(using Context): Boolean = a.isSubspace(b)
154-
155164
def computeIsSubspace(a: Space, b: Space)(using Context): Boolean = {
156165
val a2 = simplify(a)
157166
val b2 = simplify(b)
@@ -578,9 +587,6 @@ object SpaceEngine {
578587
scrutineeTp <:< tp
579588
}
580589

581-
def canDecompose(typ: Typ)(using Context): Boolean = typ.canDecompose
582-
def decompose(typ: Typ)(using Context): List[Typ] = typ.decompose
583-
584590
/** Decompose a type into subspaces -- assume the type can be decomposed */
585591
def decompose(tp: Type)(using Context): List[Type] = trace(i"decompose($tp)", debug) {
586592
def rec(tp: Type, mixins: List[Type]): List[Type] = tp.dealias match

0 commit comments

Comments
 (0)