add fast-path encoding for long, doubles, keywords, and strings
This commit is contained in:
parent
5492c1ea0f
commit
a92c493375
1 changed files with 33 additions and 18 deletions
|
|
@ -87,14 +87,29 @@
|
||||||
|
|
||||||
(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
|
|
||||||
"Like `freeze-to-stream*` but with metadata support."
|
(defn- freeze-to-stream
|
||||||
[s x]
|
"Like `freeze-to-stream*`, but with metadata support and certain fast-paths encoded."
|
||||||
`(let [x# ~x s# ~s]
|
[^DataOutputStream s x]
|
||||||
(when-let [m# (meta x#)]
|
(condp instance? x
|
||||||
(write-id s# ~id-meta)
|
Number (condp instance? x
|
||||||
(freeze-to-stream* m# s#))
|
Long (do (write-id s id-long) (.writeLong s x))
|
||||||
(freeze-to-stream* x# s#)))
|
Double (do (write-id s id-double) (.writeDouble s x))
|
||||||
|
(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."
|
||||||
|
|
@ -120,12 +135,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# (reduce
|
cnt# (loop [cnt# 0, x# ~'x]
|
||||||
(fn [cnt# i#]
|
(if (empty? x#)
|
||||||
(freeze-to-stream! s# i#)
|
cnt#
|
||||||
(unchecked-inc cnt#))
|
(do
|
||||||
0
|
(freeze-to-stream! s# (first x#))
|
||||||
~'x)
|
(recur (unchecked-inc cnt#) (rest 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#))))))
|
||||||
|
|
@ -134,10 +149,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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue