Skip to content

Commit 01d8ead

Browse files
committed
Extend caching in the space engine beyond local context
Previously isSubspace calls would be cached only as part of individual Space object, so when recursing through their ADT contents, we were not able to reuse the previously computed information. Now the cache is shared across the whole space engine run.
1 parent 4b97d25 commit 01d8ead

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,21 @@ import SpaceEngine.*
4848
*
4949
*/
5050

51+
/** A key to be used in a context property that caches the unpickled trees */
52+
private val IsSubspaceCacheKey = new Property.Key[mutable.HashMap[(Space, Space), Boolean]]
53+
5154
/** space definition */
5255
sealed trait Space extends Showable:
5356

54-
@sharable private val isSubspaceCache = mutable.HashMap.empty[Space, Boolean]
55-
5657
def isSubspace(b: Space)(using Context): Boolean =
5758
val a = this
5859
val a2 = a.simplify
5960
val b2 = b.simplify
6061
if (a ne a2) || (b ne b2) then a2.isSubspace(b2)
6162
else if a == Empty then true
6263
else if b == Empty then false
63-
else isSubspaceCache.getOrElseUpdate(b, computeIsSubspace(a, b))
64+
else
65+
ctx.property(IsSubspaceCacheKey).get.getOrElseUpdate((a, b), computeIsSubspace(a, b))
6466

6567
@sharable private var mySimplified: Space | Null = null
6668

@@ -968,6 +970,8 @@ object SpaceEngine {
968970
end checkReachability
969971

970972
def checkMatch(m: Match)(using Context): Unit =
971-
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
972-
if reachabilityCheckable(m.selector) then checkReachability(m)
973+
inContext(ctx.withProperty(IsSubspaceCacheKey, Some(mutable.HashMap.empty))) {
974+
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
975+
if reachabilityCheckable(m.selector) then checkReachability(m)
976+
}
973977
}

tests/pos/i23317.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.quoted.*
2+
3+
def goImpl(using Quotes): Expr[Int] =
4+
List.empty[Type[?]] match
5+
case Nil =>
6+
Expr(0)
7+
case '[t1] :: Nil =>
8+
Expr(1)
9+
case '[t1] :: '[t2] :: Nil =>
10+
Expr(2)
11+
case '[t1] :: '[t2] :: '[t3] :: Nil =>
12+
Expr(3)
13+
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: Nil =>
14+
Expr(4)
15+
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: Nil =>
16+
Expr(5)
17+
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: Nil =>
18+
Expr(6)
19+
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: '[t7] :: Nil =>
20+
Expr(7)
21+
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: '[t7] :: '[t8] :: Nil =>
22+
Expr(8)
23+
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: '[t7] :: '[t8] :: '[t9] :: Nil =>
24+
Expr(9)
25+
case _ =>
26+
Expr(999)

0 commit comments

Comments
 (0)