Skip to content

Commit f40a9cf

Browse files
tonskydnolen
authored and
dnolen
committed
CLJS-892: Improve performance of compare-symbols/compare-keywords
1 parent 6679e2b commit f40a9cf

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

benchmark/cljs/benchmark_runner.cljs

+12
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@
111111
(= a b) 1)
112112
(println)
113113

114+
(println ";;; keyword compare")
115+
(let [seed ["amelia" "olivia" "jessica" "emily" "lily" "ava" "isla" "sophie" "mia" "isabella" "evie" "poppy" "ruby" "grace" "sophia" "chloe" "freya" "isabelle" "ella" "charlotte" "scarlett" "daisy" "lola" "holly" "eva" "lucy" "millie" "phoebe" "layla" "maisie" "sienna" "alice" "florence" "lilly" "ellie" "erin" "elizabeth" "imogen" "summer" "molly" "hannah" "sofia" "abigail" "jasmine" "matilda" "megan" "rosie" "lexi" "lacey" "emma" "amelie" "maya" "gracie" "emilia" "georgia" "hollie" "evelyn" "eliza" "amber" "eleanor" "bella" "amy" "brooke" "leah" "esme" "harriet" "anna" "katie" "zara" "willow" "elsie" "annabelle" "bethany" "faith" "madison" "isabel" "rose" "julia" "martha" "maryam" "paige" "heidi" "maddison" "niamh" "skye" "aisha" "mollie" "ivy" "francesca" "darcey" "maria" "zoe" "keira" "sarah" "tilly" "isobel" "violet" "lydia" "sara" "caitlin"]]
116+
(simple-benchmark
117+
[arr (into-array (repeatedly 10000 #(keyword (rand-nth seed))))]
118+
(.sort arr compare)
119+
100)
120+
(simple-benchmark
121+
[arr (into-array (repeatedly 10000 #(keyword (rand-nth seed) (rand-nth seed))))]
122+
(.sort arr compare)
123+
100))
124+
(println)
125+
114126
(println ";;; reduce lazy-seqs, vectors, ranges")
115127
(simple-benchmark [coll (take 100000 (iterate inc 0))] (reduce + 0 coll) 1)
116128
(simple-benchmark [coll (range 1000000)] (reduce + 0 coll) 1)

src/cljs/cljs/core.cljs

+18-7
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,15 @@
566566

567567
(defn- compare-symbols [a b]
568568
(cond
569-
(= a b) 0
569+
(identical? (.-str a) (.-str b)) 0
570570
(and (not (.-ns a)) (.-ns b)) -1
571571
(.-ns a) (if-not (.-ns b)
572572
1
573-
(let [nsc (compare (.-ns a) (.-ns b))]
574-
(if (zero? nsc)
575-
(compare (.-name a) (.-name b))
573+
(let [nsc (garray/defaultCompare (.-ns a) (.-ns b))]
574+
(if (== 0 nsc)
575+
(garray/defaultCompare (.-name a) (.-name b))
576576
nsc)))
577-
:default (compare (.-name a) (.-name b))))
577+
:default (garray/defaultCompare (.-name a) (.-name b))))
578578

579579
(deftype Symbol [ns name str ^:mutable _hash _meta]
580580
Object
@@ -2419,6 +2419,18 @@ reduces them without incurring seq initialization"
24192419
(defn hash-keyword [k]
24202420
(int (+ (hash-symbol k) 0x9e3779b9)))
24212421

2422+
(defn- compare-keywords [a b]
2423+
(cond
2424+
(identical? (.-fqn a) (.-fqn b)) 0
2425+
(and (not (.-ns a)) (.-ns b)) -1
2426+
(.-ns a) (if-not (.-ns b)
2427+
1
2428+
(let [nsc (garray/defaultCompare (.-ns a) (.-ns b))]
2429+
(if (== 0 nsc)
2430+
(garray/defaultCompare (.-name a) (.-name b))
2431+
nsc)))
2432+
:default (garray/defaultCompare (.-name a) (.-name b))))
2433+
24222434
(deftype Keyword [ns name fqn ^:mutable _hash]
24232435
Object
24242436
(toString [_] (str ":" fqn))
@@ -8307,8 +8319,7 @@ reduces them without incurring seq initialization"
83078319
(-compare [x y] (compare-symbols x y))
83088320

83098321
Keyword
8310-
; keyword happens to have the same fields as Symbol, so this just works
8311-
(-compare [x y] (compare-symbols x y))
8322+
(-compare [x y] (compare-keywords x y))
83128323

83138324
Subvec
83148325
(-compare [x y] (compare-indexed x y))

0 commit comments

Comments
 (0)