Skip to content

Commit

Permalink
Bump nippy for better freezable? impl (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
mk authored Nov 5, 2023
1 parent d1a3e57 commit 6d34518
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Changes can be:

* 🛠 Bump depdendencies

* `com.taoensso/nippy` to `3.3`
* `com.taoensso/nippy` to `3.4.0-beta1`
* `io.github.nextjournal/markdown` to `0.5.146`

* 🐞 Fix caching behaviour of `clerk/image` and support overriding image-viewer by name
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
babashka/process {:mvn/version "0.4.16"}
io.github.nextjournal/dejavu {:git/sha "4980e0cc18c9b09fb220874ace94ba6b57a749ca"}

com.taoensso/nippy {:mvn/version "3.3.0"}
com.taoensso/nippy {:mvn/version "3.4.0-beta1"}

mvxcvi/multiformats {:mvn/version "0.3.107"}
com.pngencoder/pngencoder {:mvn/version "0.13.1"}
Expand Down
15 changes: 8 additions & 7 deletions src/nextjournal/clerk/eval.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#_(-> [(clojure.java.io/file "notebooks") (find-ns 'user)] nippy/freeze nippy/thaw)


(defn ->cache-file [hash]
(str config/cache-dir fs/file-separator hash))

Expand Down Expand Up @@ -87,7 +86,7 @@
hash)))


(defn ^:private cachable-value? [value]
(defn cachable? [value]
(and (some? value)
(try
(and (not (analyzer/exceeds-bounded-count-limit? value))
Expand All @@ -97,10 +96,12 @@
(catch Exception _
false))))

#_(cachable-value? (vec (range 100)))
#_(cachable-value? (range))
#_(cachable-value? (map inc (range)))
#_(cachable-value? [{:hello (map inc (range))}])
#_(cachable? (vec (range 100)))
#_(cachable? (range))
#_(cachable? java.lang.String)
#_(cachable? (map inc (range)))
#_(cachable? [{:hello (map inc (range))}])
#_(cachable? {:foo (javax.imageio.ImageIO/read (clojure.java.io/file "trees.png"))})


(defn ^:private cache! [digest-file var-value]
Expand Down Expand Up @@ -138,7 +139,7 @@
(when (and (not no-cache?)
(not ns-effect?)
freezable?
(cachable-value? var-value)
(cachable? var-value)
(or (not var) var-from-def?))
(cache! digest-file var-value))
(let [blob-id (cond no-cache? (analyzer/->hash-str var-value)
Expand Down
17 changes: 9 additions & 8 deletions test/nextjournal/clerk/eval_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns nextjournal.clerk.eval-test
(:require [clojure.string :as str]
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[matcher-combinators.test :refer [match?]]
[nextjournal.clerk :as clerk]
Expand Down Expand Up @@ -219,15 +220,15 @@

(deftest cacheable-value?-test
(testing "finite sequence is cacheable"
(is (#'eval/cachable-value? (vec (range 100)))))
(testing "nippy doesn't know how to freeze instances of clojure.lang.Iterate"
(is (not (#'eval/cachable-value? (range 100)))))
(is (eval/cachable? (vec (range 100)))))
(testing "infinite sequences can't be cached"
(is (not (#'eval/cachable-value? (range))))
(is (not (#'eval/cachable-value? (map inc (range))))))
(is (not (eval/cachable? (range))))
(is (not (eval/cachable? (map inc (range))))))
(testing "class is not cachable"
(is (not (#'eval/cachable-value? java.lang.String)))
(is (not (#'eval/cachable-value? {:foo java.lang.String})))))
(is (not (eval/cachable? java.lang.String)))
(is (not (eval/cachable? {:foo java.lang.String}))))
(testing "image is cachable"
(is (eval/cachable? (javax.imageio.ImageIO/read (io/file "trees.png"))))))

(deftest show!-test
(testing "in-memory cache is preserved when exception is thrown (#549)"
Expand Down

0 comments on commit 6d34518

Please sign in to comment.