Permalink
Browse files

Optimize caching with volatile var instead of synchronizing the whole…

… block.
  • Loading branch information...
1 parent c13b8f4 commit 2d7622067352ad7c3bd0d119d486693854d7987d Joni Freeman committed Aug 31, 2012
Showing with 2 additions and 3 deletions.
  1. +2 −3 core/json/src/main/scala/net/liftweb/json/Meta.scala
@@ -191,15 +191,14 @@ private[json] object Meta {
private[json] def fail(msg: String, cause: Exception = null) = throw new MappingException(msg, cause)
private class Memo[A, R] {
- private var cache = Map[A, R]()
+ @volatile private var cache = Map[A, R]()
- def memoize(x: A, f: A => R): R = synchronized {
+ def memoize(x: A, f: A => R): R =
if (cache contains x) cache(x) else {
val ret = f(x)
cache += (x -> ret)
ret
}
- }
}
object Reflection {

0 comments on commit 2d76220

Please sign in to comment.