Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Created July 18, 2019 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvandijk/b963ef7438b1511ed4362cca022f68f6 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/b963ef7438b1511ed4362cca022f68f6 to your computer and use it in GitHub Desktop.
Binary encoding comparisons (Clojure)
;; -- Deps
;; Transit
;; [com.cognitect/transit-clj "0.8.285"]
;; Fressian
;; [org.fressian/fressian "0.6.6"]
;; [org.clojure/data.fressian "0.2.1"]
(import '[java.io ByteArrayInputStream ByteArrayOutputStream])
(require '[cognitect.transit :as transit])
(defn data->transit-bytes [value]
(let [out (ByteArrayOutputStream. 4096)
writer (transit/writer out :msgpack {})]
(transit/write writer value)
(.toByteArray out)))
(defn transit-bytes->data [value]
(let [in (ByteArrayInputStream. value)
reader (transit/reader in :msgpack {})]
(transit/read reader)))
(require '[clojure.data.fressian :as fres])
(defn data->fressian-bytes [data]
(let [out-stream (ByteArrayOutputStream.)]
(with-open [wrt (fres/create-writer out-stream #_:handlers #_write-handler-lookup)]
;(fres/write-object wrt data)
(.writeObject wrt data true) ;; does the caching option do something?
(.toByteArray out-stream))))
(defn fressian-bytes->data [bytes]
(fres/read-object (fres/create-reader (ByteArrayInputStream. bytes))))
(defn print-bytesize [x]
(assert (class (byte-array [])) x)
(println "Bytesize" (count x))
x)
;; Transit (way) more compact than
(let [a (vec (repeat 10 100))]
(-> [a a a a a]
(data->transit-bytes)
(print-bytesize) ;=> 56
(transit-bytes->data)))
(let [a (repeat 10 100)]
(-> [a a a a a]
(data->fressian-bytes)
(print-bytesize) ;=> 112
(fressian-bytes->data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment