Merge branch 'ztellman'

This commit is contained in:
Peter Taoussanis 2013-10-17 17:44:27 +07:00
commit 3ecd6c9329
2 changed files with 59 additions and 14 deletions

View file

@ -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."
@ -114,17 +129,30 @@
"Extends Freezable to simple collection types." "Extends Freezable to simple collection types."
[type id & body] [type id & body]
`(freezer ~type ~id `(freezer ~type ~id
(.writeInt ~'s (count ~'x)) (if (counted? ~'x)
(doseq [i# ~'x] (freeze-to-stream ~'s i#)))) (do
(.writeInt ~'s (count ~'x))
(doseq [i# ~'x] (freeze-to-stream ~'s i#)))
(let [bas# (ByteArrayOutputStream.)
s# (DataOutputStream. bas#)
cnt# (loop [cnt# 0, x# ~'x]
(if (empty? x#)
cnt#
(do
(freeze-to-stream! s# (first x#))
(recur (unchecked-inc cnt#) (rest x#)))))
ba# (.toByteArray bas#)]
(.writeInt ~'s cnt#)
(.write ~'s ba# 0 (alength ba#))))))
(defmacro ^:private kv-freezer (defmacro ^:private kv-freezer
"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 [[k# v#] ~'x] (doseq [kv# ~'x]
(freeze-to-stream ~'s k#) (freeze-to-stream ~'s (key kv#))
(freeze-to-stream ~'s v#)))) (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)

View file

@ -66,6 +66,23 @@
;; (bench {:reader? false :laps 1}) ;; (bench {:reader? false :laps 1})
;; (bench {:reader? false :laps 2}) ;; (bench {:reader? false :laps 2})
;;; 11 Oct 2013: Nippy v2.2.0, with both ztellman mods
;; {:defaults {:round 4319, :freeze 2950, :thaw 1446, :data-size 12369}}
;; {:encrypted {:round 7675, :freeze 4479, :thaw 3160, :data-size 12388}}
;; {:fast {:round 3928, :freeze 2530, :thaw 1269, :data-size 13277}}
;; {:defaults-delta {:round 0.84 :freeze 0.79 :thaw 1.14}} ; vs 2.2.0
;;; 11 Oct 2013: Nippy v2.2.0, with first ztellman mod
;; {:defaults {:round 4059, :freeze 2578, :thaw 1351, :data-size 12342}}
;; {:encrypted {:round 7248, :freeze 4058, :thaw 3041, :data-size 12372}}
;; {:fast {:round 3430, :freeze 2085, :thaw 1229, :data-size 13277}}
;; {:defaults-delta {:round 0.79 :freeze 0.69 :thaw 1.07}} ; vs 2.2.0
;;; 11 Oct 2013: Nippy v2.2.0
;; {:defaults {:round 5135, :freeze 3711, :thaw 1266, :data-size 12393}}
;; {:encrypted {:round 8655, :freeze 5323, :thaw 3036, :data-size 12420}}
;; {:fast {:round 4670, :freeze 3282, :thaw 1294, :data-size 13277}}
;;; 7 Auguest 2013: Nippy v2.2.0-RC1 ;;; 7 Auguest 2013: Nippy v2.2.0-RC1
;; {:reader {:round 71582, :freeze 13656, :thaw 56730, :data-size 22964}} ;; {:reader {:round 71582, :freeze 13656, :thaw 56730, :data-size 22964}}
;; {:defaults {:round 5619, :freeze 3710, :thaw 1783, :data-size 12368}} ;; {:defaults {:round 5619, :freeze 3710, :thaw 1783, :data-size 12368}}