Revert "add fast-path encoding for long, doubles, keywords, and strings"

This reverts commit a92c493375.

As per https://github.com/ptaoussanis/nippy/pull/31, tests appear to be
inconclusive about the effetcs of this commit. Candidate for future
reconsideration.
This commit is contained in:
Peter Taoussanis 2013-10-17 17:44:54 +07:00
parent 3ecd6c9329
commit f6d5ebd862

View file

@ -87,29 +87,14 @@
(defmacro ^:private write-biginteger [s x] `(write-bytes ~s (.toByteArray ~x))) (defmacro ^:private write-biginteger [s x] `(write-bytes ~s (.toByteArray ~x)))
(defmacro ^:private write-utf8 [s x] `(write-bytes ~s (.getBytes ~x "UTF-8"))) (defmacro ^:private write-utf8 [s x] `(write-bytes ~s (.getBytes ~x "UTF-8")))
(defmacro ^:private freeze-to-stream
(defn- freeze-to-stream "Like `freeze-to-stream*` but with metadata support."
"Like `freeze-to-stream*`, but with metadata support and certain fast-paths encoded." [s x]
[^DataOutputStream s x] `(let [x# ~x s# ~s]
(condp instance? x (when-let [m# (meta x#)]
Number (condp instance? x (write-id s# ~id-meta)
Long (do (write-id s id-long) (.writeLong s x)) (freeze-to-stream* m# s#))
Double (do (write-id s id-double) (.writeDouble s x)) (freeze-to-stream* x# s#)))
(freeze-to-stream* x s))
Keyword (do
(write-id s id-keyword)
(write-utf8 s
(if-let [ns (namespace x)]
(str ns "/" (name x))
(name x))))
String (do
(write-id s id-string)
(write-utf8 s ^String x))
(do
(when-let [m (meta x)]
(write-id s id-meta)
(freeze-to-stream* m s))
(freeze-to-stream* x s))))
(defn freeze-to-stream! (defn freeze-to-stream!
"Low-level API. Serializes arg (any Clojure data type) to a DataOutputStream." "Low-level API. Serializes arg (any Clojure data type) to a DataOutputStream."
@ -135,12 +120,12 @@
(doseq [i# ~'x] (freeze-to-stream ~'s i#))) (doseq [i# ~'x] (freeze-to-stream ~'s i#)))
(let [bas# (ByteArrayOutputStream.) (let [bas# (ByteArrayOutputStream.)
s# (DataOutputStream. bas#) s# (DataOutputStream. bas#)
cnt# (loop [cnt# 0, x# ~'x] cnt# (reduce
(if (empty? x#) (fn [cnt# i#]
cnt# (freeze-to-stream! s# i#)
(do (unchecked-inc cnt#))
(freeze-to-stream! s# (first x#)) 0
(recur (unchecked-inc cnt#) (rest x#))))) ~'x)
ba# (.toByteArray bas#)] ba# (.toByteArray bas#)]
(.writeInt ~'s cnt#) (.writeInt ~'s cnt#)
(.write ~'s ba# 0 (alength ba#)))))) (.write ~'s ba# 0 (alength ba#))))))
@ -149,10 +134,10 @@
"Extends Freezable to key-value collection types." "Extends Freezable to key-value collection types."
[type id & body] [type id & body]
`(freezer ~type ~id `(freezer ~type ~id
(.writeInt ~'s (* 2 (count ~'x))) (.writeInt ~'s (* 2 (count ~'x)))
(doseq [kv# ~'x] (doseq [kv# ~'x]
(freeze-to-stream ~'s (key kv#)) (freeze-to-stream ~'s (key kv#))
(freeze-to-stream ~'s (val kv#))))) (freeze-to-stream ~'s (val kv#)))))
(freezer (Class/forName "[B") id-bytes (write-bytes s ^bytes x)) (freezer (Class/forName "[B") id-bytes (write-bytes s ^bytes x))
(freezer nil id-nil) (freezer nil id-nil)