-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add caching #86
Merged
Merged
Add caching #86
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ | |
"shadow-cljs": "^2.27.5" | ||
}, | ||
"dependencies": { | ||
"memoizee": "^0.4.15" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(ns inferenceql.query.cache | ||
"For caching expensive results." | ||
(:require #?(:clj [clojure.core.memoize :as memo] | ||
:cljs ["memoizee" :as memoizee]) | ||
[clojure.string :as string])) | ||
|
||
(def default-threshold 100) | ||
|
||
|
||
(defn lru | ||
"Memoizes a fn with a least-recently-used eviction policy. | ||
|
||
After the number of cached results exceeds the threshold, the | ||
least-recently-used ones will be evicted." | ||
([f] | ||
(lru f default-threshold)) | ||
([f lru-threshold] | ||
#?(:clj (memo/lru f :lru/threshold lru-threshold) | ||
:cljs (memoizee f #js {"max" lru-threshold | ||
"normalizer" js/JSON.stringify})))) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
(ns inferenceql.query.cache-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[inferenceql.query.cache :as cache])) | ||
|
||
(deftest basic-caching | ||
(let [cache-size 2 | ||
a (atom 0) | ||
incrementer (fn [_ignored-but-cached-key] | ||
(swap! a inc)) | ||
cached-incrementer (cache/lru incrementer cache-size)] | ||
|
||
(is (= 1 (cached-incrementer :foo))) | ||
KingMob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(is (= 2 (cached-incrementer :bar))) | ||
|
||
(is (= 1 (cached-incrementer :foo))) | ||
(is (= 2 (cached-incrementer :bar))) | ||
|
||
(is (= 3 (cached-incrementer :moop))) | ||
|
||
;; cache cleared for :foo | ||
(is (= 4 (cached-incrementer :foo))))) | ||
|
||
(deftest disambiguate-between-0-and-nil | ||
(let [cache-size 1000 | ||
englishize (fn [x] | ||
(case x | ||
0 "zero" | ||
nil "nil" | ||
"other")) | ||
cached-englishize (cache/lru englishize cache-size)] | ||
;; Add them both. | ||
(is (= "zero" (cached-englishize 0))) | ||
(is (= "nil" (cached-englishize nil))) | ||
|
||
;; Check that they return the correct values. | ||
(is (= "zero" (cached-englishize 0))) | ||
(is (= "nil" (cached-englishize nil))))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we'll run into trouble using
js/JSON.stringify
with some of the parameters passed to these functions, likemodel
(which can be a special type, eg. reify) or cljs data types which look pretty weird (though maybe that's ok, if it's more performant than usingclj->js
first..)It might be worth doing some simple benchmarks before committing to an approach, as some of these conversions can be surprisingly costly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My predictions are that it should be fine, but it won't hurt to check. Will have to wait until I get back from vacation, though.
The serialized weirdness should be ok as long as they never result in accidentally identical string representations. If anything, I think the opposite is true. There are probably things that could share keys, but won't. Luckily, that just means some cache misses.
I would be very surprised if clj->js + JSON/stringify was faster than JSON/stringify on the original, but no need to guess! I'll try it out when I get back. We'll see what my posteriors are then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it turns out I am very surprised. Unfortunately, I don't think it will work out to use clj->js first.
I ran a quick-and-dirty test in cljs:
and got:
Using clj->js before JSON.stringify is slightly faster, but unfortunately, it can't distinguish between string and keyword keys. Having duplicate string and keyword keys would cause other problems, so it might be safe to do. But, I'd rather err on the side of correctness, especially since these timings aren't too far off from each other.
We can always revisit our caching strategy, if necessary.