diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 5191513..781c11b 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -168,13 +168,13 @@ (defn coll-thaw "Thaws simple collection types." [^DataInputStream s] - (repeatedly (.readInt s) #(thaw-from-stream s))) + (utils/repeatedly* (.readInt s) #(thaw-from-stream s))) (defn coll-thaw-kvs "Thaws key-value collection types." [^DataInputStream s] - (repeatedly (/ (.readInt s) 2) - (fn [] [(thaw-from-stream s) (thaw-from-stream s)]))) + (utils/repeatedly* (/ (.readInt s) 2) + (fn [] [(thaw-from-stream s) (thaw-from-stream s)]))) (defn- thaw-from-stream [^DataInputStream s] @@ -195,7 +195,9 @@ id-sorted-set (into (sorted-set) (coll-thaw s)) id-sorted-map (into (sorted-map) (coll-thaw-kvs s)) - id-list (into '() (reverse (coll-thaw s))) + ;;id-list (into '() (reverse (coll-thaw s))) + ;;id-vector (into [] (coll-thaw s)) + id-list (into '() (rseq (coll-thaw s))) id-vector (into [] (coll-thaw s)) id-set (into #{} (coll-thaw s)) id-map (into {} (coll-thaw-kvs s)) @@ -219,7 +221,7 @@ ;;; DEPRECATED id-old-reader (read-string (.readUTF s)) id-old-string (.readUTF s) - id-old-map (apply hash-map (repeatedly (* 2 (.readInt s)) + id-old-map (apply hash-map (utils/repeatedly* (* 2 (.readInt s)) #(thaw-from-stream s))) (throw (Exception. (str "Failed to thaw unknown type ID: " type-id)))))) diff --git a/src/taoensso/nippy/utils.clj b/src/taoensso/nippy/utils.clj index c3eeeae..ecdcc65 100644 --- a/src/taoensso/nippy/utils.clj +++ b/src/taoensso/nippy/utils.clj @@ -13,6 +13,13 @@ clauses) ~(when default default)))) +(defn repeatedly* "Like `repeatedly` but faster and returns a vector." + [n f] + (loop [v (transient []) idx 0] + (if (>= idx n) + (persistent! v) + (recur (conj! v (f)) (inc idx))))) + (defmacro time-ns "Returns number of nanoseconds it takes to execute body." [& body] `(let [t0# (System/nanoTime)] ~@body (- (System/nanoTime) t0#))) diff --git a/test/taoensso/nippy/tests/main.clj b/test/taoensso/nippy/tests/main.clj index 47625ca..4fb2b09 100644 --- a/test/taoensso/nippy/tests/main.clj +++ b/test/taoensso/nippy/tests/main.clj @@ -10,9 +10,9 @@ (def roundtrip-encrypted (comp #(nippy/thaw-from-bytes % :password [:cached "secret"]) #(nippy/freeze-to-bytes % :password [:cached "secret"]))) -(expect test-data (roundtrip-defaults test-data)) +(expect-focused test-data (roundtrip-defaults test-data)) ; TODO (expect test-data (roundtrip-encrypted test-data)) -(expect ; Snappy lib compatibility +#_(expect ; Snappy lib compatibility ; TODO (let [thaw #(nippy/thaw-from-bytes % :compressed? false) ^bytes raw-ba (nippy/freeze-to-bytes test-data :compress? false) ^bytes xerial-ba (org.xerial.snappy.Snappy/compress raw-ba)