Swap hash-map thaw implementation for perf & to avoid OOM errors

Thanks to moonranger for pointing out the OOM issue.
This commit is contained in:
Peter Taoussanis 2012-12-04 13:16:29 +07:00
parent df3c687acd
commit c2c46167ec
3 changed files with 24 additions and 2 deletions

View file

@ -34,6 +34,11 @@
;; :nippy {:freeze 3751, :thaw 4184, :round 7769}} ;; :nippy {:freeze 3751, :thaw 4184, :round 7769}}
;; (float (/ 59545 7769)) = 7.6644354 ;; (float (/ 59545 7769)) = 7.6644354
;; Clojure 1.4.0, Nippy 1.0.0
;; {:reader {:freeze 22595, :thaw 31148, :round 54059}
;; :nippy {:freeze 3324, :thaw 3725, :round 6918}}
;; (float (/ 54059 6918)) = 7.814253
;;; Data size ;;; Data size
(let [frozen (reader-freeze data)] (count (.getBytes frozen "UTF8"))) (let [frozen (reader-freeze data)] (count (.getBytes frozen "UTF8")))
(let [frozen (freeze-to-bytes data)] (count frozen)) (let [frozen (freeze-to-bytes data)] (count frozen))

View file

@ -175,6 +175,12 @@
[^DataInputStream s] [^DataInputStream s]
(repeatedly (.readInt s) (partial thaw-from-stream!* s))) (repeatedly (.readInt s) (partial thaw-from-stream!* s)))
(defn coll-thaw-pairs!
"Helper to thaw pair-based collection types (e.g. hash maps)."
[^DataInputStream s]
(repeatedly (/ (.readInt s) 2)
(fn [] [(thaw-from-stream!* s) (thaw-from-stream!* s)])))
(defn- thaw-from-stream!* (defn- thaw-from-stream!*
[^DataInputStream s] [^DataInputStream s]
(let [type-id (.readByte s)] (let [type-id (.readByte s)]
@ -190,10 +196,13 @@
id-string (String. (read-bytes! s) "UTF-8") id-string (String. (read-bytes! s) "UTF-8")
id-keyword (keyword (.readUTF s)) id-keyword (keyword (.readUTF s))
id-list (apply list (coll-thaw! s)) id-list (apply list (coll-thaw! s)) ; TODO OOMs for big colls
id-vector (into [] (coll-thaw! s)) id-vector (into [] (coll-thaw! s))
id-set (into #{} (coll-thaw! s)) id-set (into #{} (coll-thaw! s))
id-map (apply hash-map (coll-thaw! s)) ;; id-map (apply hash-map (coll-thaw! s)) ; OOMs for big colls
;; id-map (into {} (map vec (partition 2 x))) ; ~6.4x time
;; id-map (into {} (utils/pairs (coll-thaw! s))) ; ~1.8x time
id-map (into {} (coll-thaw-pairs! s)) ; ~0.8x time
id-coll (doall (coll-thaw! s)) id-coll (doall (coll-thaw! s))
id-queue (into (PersistentQueue/EMPTY) (coll-thaw! s)) id-queue (into (PersistentQueue/EMPTY) (coll-thaw! s))

View file

@ -13,6 +13,14 @@
clauses) clauses)
~(when default default)))) ~(when default default))))
(defn pairs
"Like (partition 2 coll) but faster and returns lazy seq of vector pairs."
[coll]
(lazy-seq
(when-let [s (seq coll)]
(let [n (next s)]
(cons [(first s) (first n)] (pairs (next n)))))))
(defmacro time-ns (defmacro time-ns
"Returns number of nanoseconds it takes to execute body." "Returns number of nanoseconds it takes to execute body."
[& body] [& body]