diff --git a/project.clj b/project.clj index ae44847..ef4fc72 100644 --- a/project.clj +++ b/project.clj @@ -13,4 +13,4 @@ :aliases {"test-all" ["with-profile" "test,1.3:test,1.4:test,1.5" "test"]} :plugins [[codox "0.6.4"]] :min-lein-version "2.0.0" - :warn-on-reflection true) + :warn-on-reflection true) \ No newline at end of file diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 2ab1b57..e005e1c 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -174,7 +174,7 @@ stream (DataOutputStream. ba)] (freeze-to-stream! stream x print-dup?) (let [ba (.toByteArray ba) - ba (if compress? (utils/compress-bytes ba) ba) + ba (if compress? (utils/compress-snappy ba) ba) ba (if password (crypto/encrypt-aes128 password ba) ba)] ba))) @@ -260,7 +260,7 @@ }}] (try (-> (let [ba (if password (crypto/decrypt-aes128 password ba) ba) - ba (if compressed? (utils/uncompress-bytes ba) ba)] + ba (if compressed? (utils/uncompress-snappy ba) ba)] ba) (ByteArrayInputStream.) (DataInputStream.) diff --git a/src/taoensso/nippy/benchmarks.clj b/src/taoensso/nippy/benchmarks.clj index 902c5c1..3765b94 100644 --- a/src/taoensso/nippy/benchmarks.clj +++ b/src/taoensso/nippy/benchmarks.clj @@ -1,8 +1,7 @@ (ns taoensso.nippy.benchmarks {:author "Peter Taoussanis"} (:use [taoensso.nippy :as nippy :only (freeze-to-bytes thaw-from-bytes)]) - (:require [taoensso.nippy.utils :as utils] - [taoensso.nippy.crypto :as crypto])) + (:require [taoensso.nippy.utils :as utils])) ;; Remove stuff from stress-data that breaks reader (def data (dissoc nippy/stress-data :queue :queue-empty :bytes)) @@ -19,8 +18,8 @@ (def roundtrip-fast (comp #(nippy/thaw-from-bytes % :compressed? false) #(nippy/freeze-to-bytes % :compress? false))) -(defn autobench [] (bench (roundtrip-defaults data) - (roundtrip-encrypted data))) +(defn autobench [] {:defaults (bench (roundtrip-defaults data)) + :encrypted (bench (roundtrip-encrypted data))}) (comment diff --git a/src/taoensso/nippy/utils.clj b/src/taoensso/nippy/utils.clj index a02f2e5..c3eeeae 100644 --- a/src/taoensso/nippy/utils.clj +++ b/src/taoensso/nippy/utils.clj @@ -1,7 +1,6 @@ (ns taoensso.nippy.utils {:author "Peter Taoussanis"} - (:require [clojure.string :as str]) - (:import org.iq80.snappy.Snappy)) + (:require [clojure.string :as str])) (defmacro case-eval "Like `case` but evaluates test constants for their compile-time value." @@ -14,25 +13,12 @@ clauses) ~(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 - "Returns number of nanoseconds it takes to execute body." - [& body] - `(let [t0# (System/nanoTime)] - ~@body - (- (System/nanoTime) t0#))) +(defmacro time-ns "Returns number of nanoseconds it takes to execute body." + [& body] `(let [t0# (System/nanoTime)] ~@body (- (System/nanoTime) t0#))) (defmacro bench "Repeatedly executes form and returns time taken to complete execution." - [num-laps form & {:keys [warmup-laps num-threads as-ms?] - :or {as-ms? true}}] + [num-laps form & {:keys [warmup-laps num-threads as-ns?]}] `(try (when ~warmup-laps (dotimes [_# ~warmup-laps] ~form)) (let [nanosecs# (if-not ~num-threads @@ -44,23 +30,17 @@ doall (map deref) dorun))))] - (if ~as-ms? (Math/round (/ nanosecs# 1000000.0)) nanosecs#)) + (if ~as-ns? nanosecs# (Math/round (/ nanosecs# 1000000.0)))) (catch Exception e# (str "DNF: " (.getMessage e#))))) -(defn version-compare - "Comparator for version strings like x.y.z, etc." - [x y] - (let [vals (fn [s] (vec (map #(Integer/parseInt %) (str/split s #"\."))))] - (compare (vals x) (vals y)))) +(defn version-compare "Comparator for version strings like x.y.z, etc." + [x y] (let [vals (fn [s] (vec (map #(Integer/parseInt %) (str/split s #"\."))))] + (compare (vals x) (vals y)))) -(defn version-sufficient? - [version-str min-version-str] +(defn version-sufficient? [version-str min-version-str] (try (>= (version-compare version-str min-version-str) 0) (catch Exception _ false))) -(defn compress-bytes [^bytes ba] (Snappy/compress ba)) -(defn uncompress-bytes [^bytes ba] (Snappy/uncompress ba 0 (alength ba))) - (defn memoized "Like `memoize` but takes an explicit cache atom (possibly nil) and immediately applies memoized f to given arguments." @@ -76,6 +56,10 @@ (comment (memoized nil +) (memoized nil + 5 12)) +(defn compress-snappy ^bytes [^bytes ba] (org.iq80.snappy.Snappy/compress ba)) +(defn uncompress-snappy ^bytes [^bytes ba] (org.iq80.snappy.Snappy/uncompress ba + 0 (alength ba))) + (defn ba-concat ^bytes [^bytes ba1 ^bytes ba2] (let [s1 (alength ba1) s2 (alength ba2) @@ -90,4 +74,4 @@ (comment (String. (ba-concat (.getBytes "foo") (.getBytes "bar"))) (let [[x y] (ba-split (.getBytes "foobar") 3)] - [(String. x) (String. y)])) \ No newline at end of file + [(String. x) (String. y)]))