Skip to content

Commit

Permalink
viper: make expression cache thread-local for safety of WeakHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
pieter-bos committed Apr 18, 2024
1 parent b13e385 commit aef15a4
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/viper/viper/api/backend/silicon/CachedExpRender.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ import viper.silver.ast.Node
import scala.collection.mutable

case object CachedExpRender {
private val cache: mutable.WeakHashMap[Node, String] = mutable.WeakHashMap()
private val cache: ThreadLocal[mutable.WeakHashMap[Node, String]] = ThreadLocal.withInitial(() => mutable.WeakHashMap())

def apply(e: Node): String = {
val x = cache.getOrElseUpdate(e, e.toString())
if (x == null) {
val repr = e.toString()
cache(e) = repr
repr
} else {
x
}
}
def apply(e: Node): String =
cache.get().getOrElseUpdate(e, e.toString())
}

0 comments on commit aef15a4

Please sign in to comment.