Permalink
Browse files

optimize case keyword mode - emit case* over the fully qualified keyw…

…ord name
  • Loading branch information...
1 parent 5bbe242 commit 9872788b3caa86f639633ff14dc0db49f16d3e2a David Nolen committed May 10, 2014
Showing with 7 additions and 4 deletions.
  1. +7 −4 src/clj/cljs/core.clj
View
@@ -1170,10 +1170,13 @@
`(let [~esym ~e] (case* ~esym ~tests ~thens ~default)))
(every? core/keyword? tests)
- `(let [~esym ~e]
- (cond
- ~@(mapcat (fn [[m c]] `((cljs.core/keyword-identical? ~m ~esym) ~c)) pairs)
- :else ~default))
+ (let [tests (->> tests
+ (map #(.substring (core/str %) 1))
+ vec
+ (mapv #(if (seq? %) (vec %) [%])))
+ thens (vec (vals pairs))]
+ `(let [~esym (if (keyword? ~e) (.-fqn ~e) ~default)]
@favila

favila May 11, 2014

Contributor

This is strange logic and produces this bug:

(let [x "a"] (case x :a 1 "a"))
;=> 1
;;; Should be "a"

I think you mean something like:

`(if (keyword? ~e)
   (let [~esym (.-fqn ~e)]
     (case* ~esym ~tests ~thens ~default))
   ~default)

If you want to avoid emitting the default clause twice I think this would work also as long as the (every? core/keyword? tests) invariant is kept:

`(let [~esym (if (keyword? ~e) (.-fqn ~e) nil)]
   (case* ~esym ~tests ~thens ~default))
@swannodette

swannodette May 11, 2014

Member

@favila thanks, fixed in master!

+ (case* ~esym ~tests ~thens ~default)))
;; equality
:else

0 comments on commit 9872788

Please sign in to comment.