欢迎!请查看 关于 页面,获取有关此信息。
在我们的 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