Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by
closed by
Two performance enhancements on clojure.set/map-invert:

1) Use reduce-kv to avoid realizing mapentry's from input map
2) Use transients to create the output map


(use 'criterium.core)
(require '[clojure.set :as set])
(def m1 (zipmap (range 1) (range 1)))
(def m10 (zipmap (range 10) (range 10)))
(def m100 (zipmap (range 100) (range 100)))
(def m1000 (zipmap (range 1000) (range 1000)))
(quick-bench (set/map-invert m1000))
(quick-bench (set/map-invert m100))
(quick-bench (set/map-invert m10))
(quick-bench (set/map-invert m1))

;; means before:  138 ns  1.8 µs  20.6 µs  304 µs
;; means after:   151 ns  1.3 µs   9.0 µs  126 µs

*Patch:* clj-1808-map-invert-should-use-reduce-kv-and-transient.patch

*Screened by:* Alex Miller
closed with the note: Fixed in 1.11.0-alpha3

2 Answers

0 votes

Comment made by: alexmiller

Would be nice to see a quick perf test that compared before/after times.

0 votes
Reference: https://clojure.atlassian.net/browse/CLJ-1808 (reported by tonsky)