diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 8760c6f..aaa24a0 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -861,6 +861,20 @@ (comment (wrap-header (.getBytes "foo") {:compressor-id :lz4 :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 "Serializes arg (any Clojure data type) to a byte array. To freeze custom types, extend the Clojure reader or see `extend-freeze`." @@ -1177,6 +1191,17 @@ (def ^:private err-msg-unrecognized-header "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 "Deserializes a frozen Nippy byte array to its original Clojure data type. To thaw custom types, extend the Clojure reader or see `extend-thaw`. diff --git a/src/taoensso/nippy/benchmarks.clj b/src/taoensso/nippy/benchmarks.clj index d75edd5..aaa70be 100644 --- a/src/taoensso/nippy/benchmarks.clj +++ b/src/taoensso/nippy/benchmarks.clj @@ -49,13 +49,7 @@ #(thaw % {:password [:cached "p"]}))}) (println {:default (bench1 #(freeze % {}) #(thaw % {}))}) - (println {:fast1 (bench1 #(freeze % {:compressor nil}) - #(thaw % {:compressor nil}))}) - (println {:fast2 (bench1 #(freeze % {:no-header? true - :compressor nil}) - #(thaw % {:no-header? true - :compressor nil - :encryptor nil}))})) + (println {:fast (bench1 nippy/fast-freeze nippy/fast-thaw)})) (println "\nDone! (Time for cake?)") true)