来自 https://clojure.org/reference/transducers#_creating_transducible_processes 的引用
转换过程必须封装对由调用转换器返回的函数的引用
这些可能是状态性的且不安全的
跨线程使用。
- 这是否意味着创建不安全的转换器以获得更好的
性能是可以接受的吗?
- 核心的有状态转换器可以写入不安全的 bewildertly
性能接近 loop
/recur
吗?
关于 clojure.core/drop
的不安全实现基准测试
(definterface IMutable
(get [])
(set [new-val]))
(deftype UnsynchronizedMutable [^:unsynchronized-mutable n]
IMutable
(get [_] n)
(set [_, nv] (set! n nv)))
(defn drop!
([n]
(fn [rf]
(let [nv (UnsynchronizedMutable. n)]
(fn
([] (rf))
([result] (rf result))
([result input]
(let [^long n (.get nv)]
(.set nv (dec n))
(if (pos? n)
result
(rf result input)))))))))
(comment
(criterium.core/quick-bench
(transduce (comp
(map inc)
(drop 1))
+ (range 1000)))
#_"Execution time mean : 39,455570 µs"
(criterium.core/quick-bench
(transduce (comp
(map inc)
(drop! 1))
+ (range 1000)))
#_"Execution time mean : 24,979885 µs")