对于相对较小且重命名较少的 xrel,似乎可以有一些性能提升。
user> (def xrel {:a 1, :b 2, :c 3})
;; => #'user/xrel
user> (def kmap {:a :z})
;; => #'user/kmap
user> (quick-bench (set/rename-keys xrel kmap))
Evaluation count : 2959512 in 6 samples of 493252 calls.
Execution time mean : 200.870412 ns
Execution time std-deviation : 0.882651 ns
Execution time lower quantile : 200.116289 ns ( 2.5%)
Execution time upper quantile : 202.248092 ns (97.5%)
Overhead used : 2.151518 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
;; => nil
user> (quick-bench (set/rename-keys2 xrel kmap))
Evaluation count : 9627438 in 6 samples of 1604573 calls.
Execution time mean : 59.646747 ns
Execution time std-deviation : 0.083186 ns
Execution time lower quantile : 59.540415 ns ( 2.5%)
Execution time upper quantile : 59.723488 ns (97.5%)
Overhead used : 2.151518 ns
;; => nil
user>
对于定义为 set/rename-keys2
的
(defn rename-keys2
"Returns the map with the keys in kmap renamed to the vals in kmap"
{:added "1.0"}
[map kmap]
(reduce-kv
(fn [m old new]
(if (contains? map old)
(-> m
(assoc new (get map old))
(dissoc old))
m)) map kmap))
这样的东西是否可以作为提升 rename-keys 功能的开始进行调查?