Skip to content

Commit 430cf69

Browse files
FiV0fogus
authored andcommitted
CCACHE-63 Improve LRU/LU cache initialization when base is small
Also fix LU miss logic when the cache is not full. Signed-off-by: Fogus <mefogus@gmail.com>
1 parent 9038e65 commit 430cf69

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/main/clojure/clojure/core/cache.clj

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,8 @@
205205
(str cache \, \space (pr-str q))))
206206

207207
(defn- build-leastness-queue
208-
[base limit start-at]
209-
(into (clojure.data.priority-map/priority-map)
210-
(concat (take (- limit (count base)) (for [k (range (- limit) 0)] [k k]))
211-
(for [[k _] base] [k start-at]))))
212-
208+
[base start-at]
209+
(into (clojure.data.priority-map/priority-map) (for [[k _] base] [k start-at])))
213210

214211
(defcache LRUCache [cache lru tick limit]
215212
CacheProtocol
@@ -249,7 +246,7 @@
249246
this))
250247
(seed [_ base]
251248
(LRUCache. base
252-
(build-leastness-queue base limit 0)
249+
(build-leastness-queue base 0)
253250
0
254251
limit))
255252
Object
@@ -332,7 +329,7 @@
332329
l (-> lu (dissoc min-key) (update-in [item] (fnil inc 0)))]
333330
(LUCache. c l limit))
334331
(LUCache. (assoc cache item result) ;; no change case
335-
(assoc lu item 0)
332+
(update-in lu [item] (fnil inc 0))
336333
limit)))
337334
(evict [this key]
338335
(if (contains? this key)
@@ -342,7 +339,7 @@
342339
this))
343340
(seed [_ base]
344341
(LUCache. base
345-
(build-leastness-queue base limit 0)
342+
(build-leastness-queue base 0)
346343
limit))
347344
Object
348345
(toString [_]

src/test/clojure/clojure/core/cache_test.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
{:a 1, :b 2, :c 3, :d 4} (-> C (assoc :c 3) (assoc :d 4) .cache)
277277
{:a 1, :c 3, :d 4, :e 5} (-> C (assoc :c 3) (assoc :d 4) (.hit :a) (assoc :e 5) .cache)
278278
{:b 2, :c 3, :d 4, :e 5} (-> C (assoc :c 3) (assoc :d 4) (.hit :b) (.hit :c) (.hit :d) (assoc :e 5) .cache))))
279-
(testing "regressions against LRU eviction before threshold met"
279+
(testing "regressions against LU eviction before threshold met"
280280
(is (= (-> (clojure.core.cache/lu-cache-factory {} :threshold 2)
281281
(assoc :a 1)
282282
(assoc :b 2)
@@ -301,6 +301,13 @@
301301
(assoc :a 1)
302302
(assoc :b 2)
303303
(assoc :b 3)
304+
.cache)))
305+
306+
(is (= {:c 3 :d 4}
307+
(-> (clojure.core.cache/lu-cache-factory {:a 1 :b 2} :threshold 2)
308+
(dissoc :a)
309+
(assoc :c 3)
310+
(assoc :d 4)
304311
.cache)))))
305312

306313
;; # LIRS

0 commit comments

Comments
 (0)