From d6945753218818242c844aa13bacee482a5b7232 Mon Sep 17 00:00:00 2001 From: Alex Miller Date: Thu, 21 Dec 2023 14:45:51 -0600 Subject: [PATCH] faster slow-write-string --- README.md | 2 ++ src/main/clojure/clojure/data/json.clj | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2e9135d..4671e3b 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,8 @@ Change Log * Add [DJSON-46]: In `read`, add `:extra-data-fn` that can be provided to cause an eof check after value is read * Add [DJSON-54]: In `write`, add custom fallback fn for writing unknown types * Perf [DJSON-61]: Faster string writing when string is "simple" + * Perf: Faster string writing when string is not simple + * Perf: Faster `read-str` * Release [2.4.0] on 2021-Jul-12 * Fix [DJSON-52]: Remove Classloader workaround to support Clojure 1.2.x and below * Fix [DJSON-53]: Move deprecated API functions from compat ns into main ns diff --git a/src/main/clojure/clojure/data/json.clj b/src/main/clojure/clojure/data/json.clj index 22b454b..aacaaba 100644 --- a/src/main/clojure/clojure/data/json.clj +++ b/src/main/clojure/clojure/data/json.clj @@ -562,14 +562,17 @@ shorts)) (defn- slow-write-string [^CharSequence s ^Appendable out options] - (let [decoder codepoint-decoder] + (let [decoder codepoint-decoder + slash (get options :escape-slash) + escape-js-separators (get options :escape-js-separators) + escape-unicode (get options :escape-unicode)] (dotimes [i (.length s)] (let [cp (int (.charAt s i))] (if (< cp 128) (case (aget decoder cp) 0 (.append out (char cp)) 1 (do (.append out (char (codepoint \\))) (.append out (char cp))) - 2 (.append out (if (get options :escape-slash) "\\/" "/")) + 2 (.append out (if slash "\\/" "/")) 3 (.append out "\\b") 4 (.append out "\\f") 5 (.append out "\\n") @@ -577,10 +580,10 @@ 7 (.append out "\\t") 8 (->hex-string out cp)) (codepoint-case cp - :js-separators (if (get options :escape-js-separators) + :js-separators (if escape-js-separators (->hex-string out cp) (.append out (char cp))) - (if (get options :escape-unicode) + (if escape-unicode (->hex-string out cp) ; Hexadecimal-escaped (.append out (char cp)))))))))