Skip to content

Commit

Permalink
Add a max size for normalize cache (#2878)
Browse files Browse the repository at this point in the history
This fixes #2484. Added a capacity to normalize cache. Default to 1024
and can be override by `quill.query.cacheDynamicMaxSize`.

Added a new dependency Caffeine to use its cache implementation, which
is recommended by Guava. For a critical use case in quill, I think
it's better to have a widely tested implementation than reinventing the wheel.

Co-authored-by: Jules Ivanic <[email protected]>
  • Loading branch information
wb14123 and guizmaii authored Sep 29, 2023
1 parent df29d02 commit d5833bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/main/scala/io/getquill/norm/NormalizeCaching.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package io.getquill.norm

import com.github.benmanes.caffeine.cache.{Cache, Caffeine}
import io.getquill.ast.Ast
import java.util.concurrent.ConcurrentHashMap
import io.getquill.util.Messages

object NormalizeCaching {
private val cache = new ConcurrentHashMap[Ast, Ast]

private val cache: Cache[Ast, Ast] = Caffeine
.newBuilder()
.maximumSize(Messages.cacheDynamicMaxSize)
.recordStats()
.build()

def apply(f: Ast => Ast): Ast => Ast = { ori =>
val (stabilized, state) = StabilizeLifts.stabilize(ori)
val cachedR = cache.get(stabilized)
val normalized = if (cachedR != null) {
cachedR
} else {
val r = f(stabilized)
cache.put(stabilized, r)
r
}
val normalized = cache.get(stabilized, ast => f(ast))
StabilizeLifts.revert(normalized, state)
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/io/getquill/util/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ object Messages {
"quill.query.cacheDynamic",
variable("quill.query.cacheDynamic", "query_query_cacheDynamic", "true").toBoolean
)
def cacheDynamicMaxSize = cache(
"quill.query.cacheDynamicMaxSize",
variable("quill.query.cacheDynamicMaxSize", "query_query_cacheDynamicMaxSize", "1024").toLong
)
def querySubexpand =
cache("quill.query.subexpand", variable("quill.query.subexpand", "query_query_subexpand", "true").toBoolean)
def quillLogFile = cache("quill.log.file", LogToFile(variable("quill.log.file", "quill_log_file", "false")))
Expand Down

0 comments on commit d5833bd

Please sign in to comment.