From 7040a4a099c3e8ef6c41fc37b78a33e9b507edc3 Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Sun, 14 Mar 2021 18:58:40 +0200 Subject: [PATCH] OPTIMIZE: replace `doseq` with `reduce` --- src/cheshire/custom.clj | 21 +++++++------ src/cheshire/generate.clj | 58 +++++++++++++++++++---------------- src/cheshire/generate_seq.clj | 58 +++++++++++++++++++---------------- 3 files changed, 76 insertions(+), 61 deletions(-) diff --git a/src/cheshire/custom.clj b/src/cheshire/custom.clj index 9abaed78..35390654 100644 --- a/src/cheshire/custom.clj +++ b/src/cheshire/custom.clj @@ -155,8 +155,7 @@ "Encode a seq to the json generator." [s ^JsonGenerator jg] (.writeStartArray jg) - (doseq [i s] - (to-json i jg)) + (reduce (fn [jg i] (to-json i jg) jg) jg s) (.writeEndArray jg)) (defn encode-date @@ -182,13 +181,17 @@ "Encode a clojure map to the json generator." [^clojure.lang.IPersistentMap m ^JsonGenerator jg] (.writeStartObject jg) - (doseq [[k v] m] - (.writeFieldName jg (if (instance? clojure.lang.Keyword k) - (if-let [ns (namespace k)] - (str ns "/" (name k)) - (name k)) - (str k))) - (to-json v jg)) + (reduce (fn [^JsonGenerator jg kv] + (let [k (key kv) + v (val kv)] + (.writeFieldName jg (if (instance? clojure.lang.Keyword k) + (if-let [ns (namespace k)] + (str ns "/" (name k)) + (name k)) + (str k))) + (to-json v jg) + jg)) + jg m) (.writeEndObject jg)) (defn encode-symbol diff --git a/src/cheshire/generate.clj b/src/cheshire/generate.clj index f3e7fb04..31b8e651 100644 --- a/src/cheshire/generate.clj +++ b/src/cheshire/generate.clj @@ -61,13 +61,15 @@ (let [jg (tag jg)] `(do (.writeStartObject ~jg) - (doseq [m# ~obj] - (let [k# (key m#) - v# (val m#)] - (.writeFieldName ~jg (if (keyword? k#) - (.substring (str k#) 1) - (str k#))) - (generate ~jg v# ~date-format ~e nil))) + (reduce (fn [^JsonGenerator jg# kv#] + (let [k# (key kv#) + v# (val kv#)] + (.writeFieldName jg# (if (keyword? k#) + (.substring (str k#) 1) + (str k#))) + (generate jg# v# ~date-format ~e nil) + jg#)) + ~jg ~obj) (.writeEndObject ~jg)))) (definline generate-key-fn-map @@ -77,14 +79,16 @@ jg (tag jg)] `(do (.writeStartObject ~jg) - (doseq [m# ~obj] - (let [~k (key m#) - v# (val m#) - ^String name# (if (keyword? ~k) - (~key-fn ~k) - (str ~k))] - (.writeFieldName ~jg name#) - (generate ~jg v# ~date-format ~e ~key-fn))) + (reduce (fn [^JsonGenerator jg# kv#] + (let [~k (key kv#) + v# (val kv#) + ^String name# (if (keyword? ~k) + (~key-fn ~k) + (str ~k))] + (.writeFieldName jg# name#) + (generate jg# v# ~date-format ~e ~key-fn) + jg#)) + ~jg ~obj) (.writeEndObject ~jg)))) (definline generate-map @@ -98,8 +102,7 @@ (let [jg (tag jg)] `(do (.writeStartArray ~jg) - (doseq [item# ~obj] - (generate ~jg item# ~date-format ~e ~key-fn)) + (reduce (fn [jg# item#] (generate jg# item# ~date-format ~e ~key-fn) jg#) ~jg ~obj) (.writeEndArray ~jg)))) (defmacro i? @@ -188,8 +191,7 @@ "Encode a seq to the json generator." [s ^JsonGenerator jg] (.writeStartArray jg) - (doseq [i s] - (generate jg i *date-format* nil nil)) + (reduce (fn [jg i] (generate jg i *date-format* nil nil) jg) jg s) (.writeEndArray jg)) (defn encode-date @@ -215,13 +217,17 @@ "Encode a clojure map to the json generator." [^clojure.lang.IPersistentMap m ^JsonGenerator jg] (.writeStartObject jg) - (doseq [[k v] m] - (.writeFieldName jg (if (instance? clojure.lang.Keyword k) - (if-let [ns (namespace k)] - (str ns "/" (name k)) - (name k)) - (str k))) - (generate jg v *date-format* nil nil)) + (reduce (fn [^JsonGenerator jg kv] + (let [k (key kv) + v (val kv)] + (.writeFieldName jg (if (instance? clojure.lang.Keyword k) + (if-let [ns (namespace k)] + (str ns "/" (name k)) + (name k)) + (str k))) + (generate jg v *date-format* nil nil) + jg)) + jg m) (.writeEndObject jg)) (defn encode-symbol diff --git a/src/cheshire/generate_seq.clj b/src/cheshire/generate_seq.clj index 1dbf1dcb..c728129e 100644 --- a/src/cheshire/generate_seq.clj +++ b/src/cheshire/generate_seq.clj @@ -35,16 +35,18 @@ (let [jg (tag jg)] `(do (write-start-object ~jg ~wholeness) - (doseq [m# ~obj] - (let [k# (key m#) - v# (val m#)] - (.writeFieldName ~jg (if (keyword? k#) - (.substring (str k#) 1) - (str k#))) - (generate ~jg v# ~date-format ~e nil - :wholeness (if (= ~wholeness :start-inner) - :start - :all)))) + (reduce (fn [^JsonGenerator jg# kv#] + (let [k# (key kv#) + v# (val kv#)] + (.writeFieldName jg# (if (keyword? k#) + (.substring (str k#) 1) + (str k#))) + (generate jg# v# ~date-format ~e nil + :wholeness (if (= ~wholeness :start-inner) + :start + :all)) + jg#)) + ~jg ~obj) (write-end-object ~jg ~wholeness)))) (definline generate-key-fn-map @@ -55,17 +57,19 @@ jg (tag jg)] `(do (write-start-object ~jg ~wholeness) - (doseq [m# ~obj] - (let [~k (key m#) - v# (val m#) - ^String name# (if (keyword? ~k) - (~key-fn ~k) - (str ~k))] - (.writeFieldName ~jg name#) - (generate ~jg v# ~date-format ~e ~key-fn - :wholeness (if (= ~wholeness :start-inner) - :start - :all)))) + (reduce (fn [^JsonGenerator jg# kv#] + (let [~k (key kv#) + v# (val kv#) + ^String name# (if (keyword? ~k) + (~key-fn ~k) + (str ~k))] + (.writeFieldName jg# name#) + (generate jg# v# ~date-format ~e ~key-fn + :wholeness (if (= ~wholeness :start-inner) + :start + :all)) + jg#)) + ~jg ~obj) (write-end-object ~jg ~wholeness)))) (definline generate-map @@ -80,11 +84,13 @@ (let [jg (tag jg)] `(do (write-start-array ~jg ~wholeness) - (doseq [item# ~obj] - (generate ~jg item# ~date-format ~e ~key-fn - :wholeness (if (= ~wholeness :start-inner) - :start - :all))) + (reduce (fn [jg# item#] + (generate jg# item# ~date-format ~e ~key-fn + :wholeness (if (= ~wholeness :start-inner) + :start + :all)) + jg#) + ~jg ~obj) (write-end-array ~jg ~wholeness)))) (defn generate [^JsonGenerator jg obj ^String date-format