-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add caching, tests, and cache scalar pdf/prob/condition/con…
…strain/mutual-info fns in clj Also bump up inferenceql.inference version to avoid arrow constructor bug
- Loading branch information
Showing
6 changed files
with
145 additions
and
36 deletions.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "inferenceql.query", | ||
"version": "1.0.0", | ||
"description": " ", | ||
"main": "index.js", | ||
"directories": { | ||
"doc": "docs", | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/InferenceQL/inferenceql.query.git" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/InferenceQL/inferenceql.query/issues" | ||
}, | ||
"homepage": "https://github.com/InferenceQL/inferenceql.query#readme", | ||
"devDependencies": { | ||
"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))) | ||
(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))))) |