Skip to content

Commit 0e3d4f1

Browse files
committed
CLJS-2864: Optimize str macro for single arity case
For the single arity case, emit code that doesn't involve constructing a JavaScript array and a call to join, but instead directly calls the runtime single-arity str implementation.
1 parent d8ae109 commit 0e3d4f1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/main/clojure/cljs/core.cljc

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -827,16 +827,22 @@
827827
(core/quot c 32)
828828
(core/inc (core/quot c 32)))))
829829

830-
(core/defmacro str [& xs]
831-
(core/let [interpolate (core/fn [x]
832-
(if (core/string? x)
833-
"~{}"
834-
"cljs.core.str.cljs$core$IFn$_invoke$arity$1(~{})"))
835-
strs (core/->> xs
836-
(map interpolate)
837-
(interpose ",")
838-
(apply core/str))]
839-
(list* 'js* (core/str "[" strs "].join('')") xs)))
830+
(core/defmacro str
831+
([] "")
832+
([x]
833+
(if (core/string? x)
834+
x
835+
(core/list 'js* "cljs.core.str.cljs$core$IFn$_invoke$arity$1(~{})" x)))
836+
([x & ys]
837+
(core/let [interpolate (core/fn [x]
838+
(if (core/string? x)
839+
"~{}"
840+
"cljs.core.str.cljs$core$IFn$_invoke$arity$1(~{})"))
841+
strs (core/->> (core/list* x ys)
842+
(map interpolate)
843+
(interpose ",")
844+
(apply core/str))]
845+
(list* 'js* (core/str "[" strs "].join('')") x ys))))
840846

841847
(core/defn- bool-expr [e]
842848
(vary-meta e assoc :tag 'boolean))

src/test/cljs/cljs/core_test.cljs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,13 @@
16261626
(is (thrown? js/Error ((not empty?) "foo")))
16271627
(is (thrown? js/Error ((not empty?) ""))))
16281628

1629+
(deftest test-cljs-2864
1630+
(is (= "" (str)))
1631+
(is (= "a" (str "a")))
1632+
(is (= "1" (str 1)))
1633+
(is (= "xyzzy" (str "x" "y" "z" "z" "y")))
1634+
(is (= "a1b2c3" (str "a" 1 "b" 2 "c" 3))))
1635+
16291636
(deftest test-cljs-2943
16301637
(let [m1 {:a 2, :b 3, :c 5}
16311638
m2 {:a 7, :b 11, :d 13, :e 17}

0 commit comments

Comments
 (0)