don't iterate twice over uncounted seqs, and use explicit key/val accessors for kv-collections, appears to give ~25% improvement in 'freeze'
This commit is contained in:
parent
7f6f6cf741
commit
5492c1ea0f
1 changed files with 18 additions and 5 deletions
|
|
@ -114,17 +114,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# (reduce
|
||||||
|
(fn [cnt# i#]
|
||||||
|
(freeze-to-stream! s# i#)
|
||||||
|
(unchecked-inc cnt#))
|
||||||
|
0
|
||||||
|
~'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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue