Skip to content

Extend caching in the space engine beyond local context #23483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ import SpaceEngine.*
*
*/

/** A key to be used in a context property that caches the unpickled trees */
private val IsSubspaceCacheKey = new Property.Key[mutable.HashMap[(Space, Space), Boolean]]

/** space definition */
sealed trait Space extends Showable:

@sharable private val isSubspaceCache = mutable.HashMap.empty[Space, Boolean]

def isSubspace(b: Space)(using Context): Boolean =
val a = this
val a2 = a.simplify
val b2 = b.simplify
if (a ne a2) || (b ne b2) then a2.isSubspace(b2)
else if a == Empty then true
else if b == Empty then false
else isSubspaceCache.getOrElseUpdate(b, computeIsSubspace(a, b))
else
ctx.property(IsSubspaceCacheKey).get.getOrElseUpdate((a, b), computeIsSubspace(a, b))

@sharable private var mySimplified: Space | Null = null

Expand Down Expand Up @@ -968,6 +970,8 @@ object SpaceEngine {
end checkReachability

def checkMatch(m: Match)(using Context): Unit =
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
if reachabilityCheckable(m.selector) then checkReachability(m)
inContext(ctx.withProperty(IsSubspaceCacheKey, Some(mutable.HashMap.empty))) {
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
if reachabilityCheckable(m.selector) then checkReachability(m)
}
}
26 changes: 26 additions & 0 deletions tests/pos/i23317.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.quoted.*

def goImpl(using Quotes): Expr[Int] =
List.empty[Type[?]] match
case Nil =>
Expr(0)
case '[t1] :: Nil =>
Expr(1)
case '[t1] :: '[t2] :: Nil =>
Expr(2)
case '[t1] :: '[t2] :: '[t3] :: Nil =>
Expr(3)
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: Nil =>
Expr(4)
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: Nil =>
Expr(5)
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: Nil =>
Expr(6)
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: '[t7] :: Nil =>
Expr(7)
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: '[t7] :: '[t8] :: Nil =>
Expr(8)
case '[t1] :: '[t2] :: '[t3] :: '[t4] :: '[t5] :: '[t6] :: '[t7] :: '[t8] :: '[t9] :: Nil =>
Expr(9)
case _ =>
Expr(999)
Loading