Add fast-freeze, fast-thaw utils

This commit is contained in:
Peter Taoussanis 2016-04-14 10:43:09 +07:00
parent 3ab91763c6
commit 414b787684
2 changed files with 26 additions and 7 deletions

View file

@ -861,6 +861,20 @@
(comment (wrap-header (.getBytes "foo") {:compressor-id :lz4 (comment (wrap-header (.getBytes "foo") {:compressor-id :lz4
:encryptor-id nil})) :encryptor-id nil}))
(defn fast-freeze
"Like `freeze` but:
- Writes data without a Nippy header
- Drops all support for compression and encryption
- Must be thawed with `fast-thaw`
Equivalent to (but a little faster than):
`(freeze x {:compressor nil :encryptor nil :no-header? true})"
^bytes [x]
(let [baos (ByteArrayOutputStream. 64)
dos (DataOutputStream. baos)]
(freeze-to-out! dos x)
(.toByteArray baos)))
(defn freeze (defn freeze
"Serializes arg (any Clojure data type) to a byte array. To freeze custom "Serializes arg (any Clojure data type) to a byte array. To freeze custom
types, extend the Clojure reader or see `extend-freeze`." types, extend the Clojure reader or see `extend-freeze`."
@ -1177,6 +1191,17 @@
(def ^:private err-msg-unrecognized-header (def ^:private err-msg-unrecognized-header
"Unrecognized (but apparently well-formed) header. Data frozen with newer Nippy version?") "Unrecognized (but apparently well-formed) header. Data frozen with newer Nippy version?")
(defn fast-thaw
"Like `thaw` but:
- Drops all support for compression and encryption
- Supports only data frozen with `fast-freeze`
Equivalent to (but a little faster than):
`(thaw x {:compressor nil :encryptor nil :no-header? true})"
[^bytes ba]
(let [dis (DataInputStream. (ByteArrayInputStream. ba))]
(thaw-from-in! dis)))
(defn thaw (defn thaw
"Deserializes a frozen Nippy byte array to its original Clojure data type. "Deserializes a frozen Nippy byte array to its original Clojure data type.
To thaw custom types, extend the Clojure reader or see `extend-thaw`. To thaw custom types, extend the Clojure reader or see `extend-thaw`.

View file

@ -49,13 +49,7 @@
#(thaw % {:password [:cached "p"]}))}) #(thaw % {:password [:cached "p"]}))})
(println {:default (bench1 #(freeze % {}) (println {:default (bench1 #(freeze % {})
#(thaw % {}))}) #(thaw % {}))})
(println {:fast1 (bench1 #(freeze % {:compressor nil}) (println {:fast (bench1 nippy/fast-freeze nippy/fast-thaw)}))
#(thaw % {:compressor nil}))})
(println {:fast2 (bench1 #(freeze % {:no-header? true
:compressor nil})
#(thaw % {:no-header? true
:compressor nil
:encryptor nil}))}))
(println "\nDone! (Time for cake?)") (println "\nDone! (Time for cake?)")
true) true)