Micro optimization: remove & args
This commit is contained in:
parent
53d993e132
commit
7ae954a229
1 changed files with 93 additions and 96 deletions
|
|
@ -366,7 +366,7 @@
|
||||||
|
|
||||||
(defn freeze-to-out!
|
(defn freeze-to-out!
|
||||||
"Low-level API. Serializes arg (any Clojure data type) to a DataOutput."
|
"Low-level API. Serializes arg (any Clojure data type) to a DataOutput."
|
||||||
[^DataOutput data-output x & _]
|
[^DataOutput data-output x]
|
||||||
(freeze-to-out data-output x))
|
(freeze-to-out data-output x))
|
||||||
|
|
||||||
(defn default-freeze-compressor-selector
|
(defn default-freeze-compressor-selector
|
||||||
|
|
@ -394,40 +394,41 @@
|
||||||
(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`."
|
||||||
^bytes [x & [{:keys [compressor encryptor password skip-header?]
|
(^bytes [x] (freeze x nil))
|
||||||
:or {compressor :auto
|
(^bytes [x {:keys [compressor encryptor password skip-header?]
|
||||||
encryptor aes128-encryptor}
|
:or {compressor :auto
|
||||||
:as opts}]]
|
encryptor aes128-encryptor}
|
||||||
(let [legacy-mode? (:legacy-mode opts) ; DEPRECATED Nippy v1-compatible freeze
|
:as opts}]
|
||||||
compressor (if legacy-mode? snappy-compressor compressor)
|
(let [legacy-mode? (:legacy-mode opts) ; DEPRECATED Nippy v1-compatible freeze
|
||||||
encryptor (when password (if-not legacy-mode? encryptor nil))
|
compressor (if legacy-mode? snappy-compressor compressor)
|
||||||
skip-header? (or skip-header? legacy-mode?)
|
encryptor (when password (if-not legacy-mode? encryptor nil))
|
||||||
baos (ByteArrayOutputStream.)
|
skip-header? (or skip-header? legacy-mode?)
|
||||||
dos (DataOutputStream. baos)]
|
baos (ByteArrayOutputStream.)
|
||||||
(freeze-to-out! dos x)
|
dos (DataOutputStream. baos)]
|
||||||
(let [ba (.toByteArray baos)
|
(freeze-to-out! dos x)
|
||||||
|
(let [ba (.toByteArray baos)
|
||||||
|
|
||||||
compressor
|
compressor
|
||||||
(if (identical? compressor :auto)
|
(if (identical? compressor :auto)
|
||||||
(if skip-header?
|
(if skip-header?
|
||||||
lz4-compressor
|
lz4-compressor
|
||||||
(*default-freeze-compressor-selector* ba))
|
(*default-freeze-compressor-selector* ba))
|
||||||
(if (fn? compressor)
|
(if (fn? compressor)
|
||||||
(compressor ba) ; Assume compressor selector fn
|
(compressor ba) ; Assume compressor selector fn
|
||||||
compressor ; Assume compressor
|
compressor ; Assume compressor
|
||||||
))
|
))
|
||||||
|
|
||||||
ba (if compressor (compress compressor ba) ba)
|
ba (if compressor (compress compressor ba) ba)
|
||||||
ba (if encryptor (encrypt encryptor password ba) ba)]
|
ba (if encryptor (encrypt encryptor password ba) ba)]
|
||||||
|
|
||||||
(if skip-header? ba
|
(if skip-header? ba
|
||||||
(wrap-header ba
|
(wrap-header ba
|
||||||
{:compressor-id (when-let [c compressor]
|
{:compressor-id (when-let [c compressor]
|
||||||
(or (compression/standard-header-ids
|
(or (compression/standard-header-ids
|
||||||
(compression/header-id c)) :else))
|
(compression/header-id c)) :else))
|
||||||
:encryptor-id (when-let [e encryptor]
|
:encryptor-id (when-let [e encryptor]
|
||||||
(or (encryption/standard-header-ids
|
(or (encryption/standard-header-ids
|
||||||
(encryption/header-id e)) :else))})))))
|
(encryption/header-id e)) :else))}))))))
|
||||||
|
|
||||||
;;;; Thawing
|
;;;; Thawing
|
||||||
|
|
||||||
|
|
@ -597,7 +598,7 @@
|
||||||
(defn thaw-from-in!
|
(defn thaw-from-in!
|
||||||
"Low-level API. Deserializes a frozen object from given DataInput to its
|
"Low-level API. Deserializes a frozen object from given DataInput to its
|
||||||
original Clojure data type."
|
original Clojure data type."
|
||||||
[data-input & _]
|
[data-input]
|
||||||
(thaw-from-in data-input))
|
(thaw-from-in data-input))
|
||||||
|
|
||||||
(defn- try-parse-header [ba]
|
(defn- try-parse-header [ba]
|
||||||
|
|
@ -634,73 +635,74 @@
|
||||||
Options include:
|
Options include:
|
||||||
:compressor - An ICompressor, :auto (requires Nippy header), or nil.
|
:compressor - An ICompressor, :auto (requires Nippy header), or nil.
|
||||||
:encryptor - An IEncryptor, :auto (requires Nippy header), or nil."
|
:encryptor - An IEncryptor, :auto (requires Nippy header), or nil."
|
||||||
[^bytes ba
|
|
||||||
& [{:keys [compressor encryptor password v1-compatibility?]
|
|
||||||
:or {compressor :auto
|
|
||||||
encryptor :auto
|
|
||||||
v1-compatibility? true ; Recommend disabling when possible
|
|
||||||
}
|
|
||||||
:as opts}]]
|
|
||||||
|
|
||||||
(assert (not (contains? opts :headerless-meta))
|
([ba] (thaw ba nil))
|
||||||
":headerless-meta `thaw` option removed as of Nippy v2.7.")
|
([^bytes ba
|
||||||
|
{:keys [v1-compatibility? compressor encryptor password]
|
||||||
|
:or {v1-compatibility? true ; Recommend disabling when possible
|
||||||
|
compressor :auto
|
||||||
|
encryptor :auto}
|
||||||
|
:as opts}]
|
||||||
|
|
||||||
(let [ex (fn [msg & [e]] (throw (ex-info (format "Thaw failed: %s" msg)
|
(assert (not (:headerless-meta opts))
|
||||||
{:opts (merge opts
|
":headerless-meta `thaw` opt removed in Nippy v2.7+")
|
||||||
{:compressor compressor
|
|
||||||
:encryptor encryptor})}
|
|
||||||
e)))
|
|
||||||
thaw-data
|
|
||||||
(fn [data-ba compressor-id encryptor-id]
|
|
||||||
(let [compressor (if (identical? compressor :auto)
|
|
||||||
(get-auto-compressor compressor-id)
|
|
||||||
compressor)
|
|
||||||
encryptor (if (identical? encryptor :auto)
|
|
||||||
(get-auto-encryptor encryptor-id)
|
|
||||||
encryptor)]
|
|
||||||
|
|
||||||
(when (and encryptor (not password))
|
(let [ex (fn [msg & [e]] (throw (ex-info (format "Thaw failed: %s" msg)
|
||||||
(ex "Password required for decryption."))
|
{:opts (merge opts
|
||||||
|
{:compressor compressor
|
||||||
|
:encryptor encryptor})}
|
||||||
|
e)))
|
||||||
|
thaw-data
|
||||||
|
(fn [data-ba compressor-id encryptor-id]
|
||||||
|
(let [compressor (if (identical? compressor :auto)
|
||||||
|
(get-auto-compressor compressor-id)
|
||||||
|
compressor)
|
||||||
|
encryptor (if (identical? encryptor :auto)
|
||||||
|
(get-auto-encryptor encryptor-id)
|
||||||
|
encryptor)]
|
||||||
|
|
||||||
(try
|
(when (and encryptor (not password))
|
||||||
(let [ba data-ba
|
(ex "Password required for decryption."))
|
||||||
ba (if encryptor (decrypt encryptor password ba) ba)
|
|
||||||
ba (if compressor (decompress compressor ba) ba)
|
|
||||||
dis (DataInputStream. (ByteArrayInputStream. ba))]
|
|
||||||
(thaw-from-in! dis))
|
|
||||||
|
|
||||||
(catch Exception e
|
(try
|
||||||
(ex "Decryption/decompression failure, or data unfrozen/damaged."
|
(let [ba data-ba
|
||||||
e)))))
|
ba (if encryptor (decrypt encryptor password ba) ba)
|
||||||
|
ba (if compressor (decompress compressor ba) ba)
|
||||||
|
dis (DataInputStream. (ByteArrayInputStream. ba))]
|
||||||
|
(thaw-from-in! dis))
|
||||||
|
|
||||||
;; This is hackish and can actually currently result in JVM core dumps
|
(catch Exception e
|
||||||
;; due to buggy Snappy behaviour, Ref. http://goo.gl/mh7Rpy.
|
(ex "Decryption/decompression failure, or data unfrozen/damaged."
|
||||||
thaw-nippy-v1-data
|
e)))))
|
||||||
(fn [data-ba]
|
|
||||||
(if-not v1-compatibility?
|
|
||||||
(throw (Exception. "v1 compatibility disabled"))
|
|
||||||
(try (thaw-data data-ba :snappy nil)
|
|
||||||
(catch Exception _
|
|
||||||
(thaw-data data-ba nil nil)))))]
|
|
||||||
|
|
||||||
(if-let [[data-ba {:keys [compressor-id encryptor-id unrecognized-meta?]
|
;; This is hackish and can actually currently result in JVM core dumps
|
||||||
:as head-meta}] (try-parse-header ba)]
|
;; due to buggy Snappy behaviour, Ref. http://goo.gl/mh7Rpy.
|
||||||
|
thaw-nippy-v1-data
|
||||||
;; A well-formed header _appears_ to be present (it's possible though
|
(fn [data-ba]
|
||||||
;; unlikely that this is a fluke and data is actually headerless):
|
(if-not v1-compatibility?
|
||||||
(try (thaw-data data-ba compressor-id encryptor-id)
|
(throw (Exception. "v1 compatibility disabled"))
|
||||||
(catch Exception e
|
(try (thaw-data data-ba :snappy nil)
|
||||||
(try (thaw-nippy-v1-data data-ba)
|
|
||||||
(catch Exception _
|
(catch Exception _
|
||||||
(if unrecognized-meta?
|
(thaw-data data-ba nil nil)))))]
|
||||||
(ex "Unrecognized (but apparently well-formed) header. Data frozen with newer Nippy version?"
|
|
||||||
e)
|
|
||||||
(throw e))))))
|
|
||||||
|
|
||||||
;; Well-formed header definitely not present
|
(if-let [[data-ba {:keys [compressor-id encryptor-id unrecognized-meta?]
|
||||||
(try (thaw-nippy-v1-data ba)
|
:as head-meta}] (try-parse-header ba)]
|
||||||
(catch Exception _
|
|
||||||
(thaw-data ba :no-header :no-header))))))
|
;; A well-formed header _appears_ to be present (it's possible though
|
||||||
|
;; unlikely that this is a fluke and data is actually headerless):
|
||||||
|
(try (thaw-data data-ba compressor-id encryptor-id)
|
||||||
|
(catch Exception e
|
||||||
|
(try (thaw-nippy-v1-data data-ba)
|
||||||
|
(catch Exception _
|
||||||
|
(if unrecognized-meta?
|
||||||
|
(ex "Unrecognized (but apparently well-formed) header. Data frozen with newer Nippy version?"
|
||||||
|
e)
|
||||||
|
(throw e))))))
|
||||||
|
|
||||||
|
;; Well-formed header definitely not present
|
||||||
|
(try (thaw-nippy-v1-data ba)
|
||||||
|
(catch Exception _
|
||||||
|
(thaw-data ba :no-header :no-header)))))))
|
||||||
|
|
||||||
(comment (thaw (freeze "hello"))
|
(comment (thaw (freeze "hello"))
|
||||||
(thaw (freeze "hello" {:compressor nil}))
|
(thaw (freeze "hello" {:compressor nil}))
|
||||||
|
|
@ -912,8 +914,3 @@
|
||||||
|
|
||||||
(comment (inspect-ba (freeze "hello"))
|
(comment (inspect-ba (freeze "hello"))
|
||||||
(seq (:data-ba (inspect-ba (freeze "hello")))))
|
(seq (:data-ba (inspect-ba (freeze "hello")))))
|
||||||
|
|
||||||
;;;; Deprecated API
|
|
||||||
|
|
||||||
(def freeze-to-stream! "DEPRECATED: Use `freeze-to-out!` instead." freeze-to-out!)
|
|
||||||
(def thaw-from-stream! "DEPRECATED: Use `thaw-from-in!` instead." thaw-from-in!)
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue