欢迎!请查看关于 页面以获取更多信息关于这是如何工作的。
在我们的TMD途径之一(来自 mastodon),我们使用 defrecords 扩展数据集大小。出人意料的是,考虑到所有其他性能案例所做的一切,实现 IReduceInit 显著增加了时间。
tech.v3.dataset.reductions-test> (require '[criterium.core :as crit]) nil tech.v3.dataset.reductions-test> (defrecord YMC [year-month ^long count] ;; clojure.lang.IReduceInit ;; (reduce [this rfn init] ;; (let [init (reduced-> rfn init ;; (clojure.lang.MapEntry/create :year-month year-month) ;; (clojure.lang.MapEntry/create :count count))] ;; (if (and __extmap (not (reduced? init))) ;; (reduce rfn init __extmap) ;; init))) ) tech.v3.dataset.reductions_test.YMC tech.v3.dataset.reductions-test> (let [yc (YMC. :a 1)] (crit/quick-bench (reduce (fn [acc v] v) nil yc))) Evaluation count : 6729522 in 6 samples of 1121587 calls. Execution time mean : 87.375170 ns Execution time std-deviation : 0.173728 ns Execution time lower quantile : 87.104982 ns ( 2.5%) Execution time upper quantile : 87.550708 ns (97.5%) Overhead used : 2.017589 ns nil tech.v3.dataset.reductions-test> (defrecord YMC [year-month ^long count] clojure.lang.IReduceInit (reduce [this rfn init] (let [init (reduced-> rfn init (clojure.lang.MapEntry/create :year-month year-month) (clojure.lang.MapEntry/create :count count))] (if (and __extmap (not (reduced? init))) (reduce rfn init __extmap) init))) ) tech.v3.dataset.reductions_test.YMC tech.v3.dataset.reductions-test> (let [yc (YMC. :a 1)] (crit/quick-bench (reduce (fn [acc v] v) nil yc))) Evaluation count : 43415358 in 6 samples of 7235893 calls. Execution time mean : 11.775423 ns Execution time std-deviation : 0.197683 ns Execution time lower quantile : 11.594695 ns ( 2.5%) Execution time upper quantile : 12.079668 ns (97.5%) Overhead used : 2.017589 ns nil tech.v3.dataset.reductions-test> (defmacro reduced-> [rfn acc & data] (reduce (fn [expr next-val] `(let [val# ~expr] (if (reduced? val#) val# (~rfn val# ~next-val)))) acc data)) #'tech.v3.dataset.reductions-test/reduced-> tech.v3.dataset.reductions-test>
与其他成员讨论后,看起来当 __extmap 不为 nil 时(它使用 clojure.core/get 而不是直接的 getorDefault 调用),值查找途径也可以进行优化。
这很有趣。我实际上在1.12.0-alpha1版本中的其他减少变化背景下看过这个,但我们没有发现它是问题。我明天将为这个问题创建一个工单。
记录为 https://clojure.atlassian.net/browse/CLJ-2750