当前clojure.core/distinct的实现使用持久化集合。此补丁通过使用transient集合而不是持久化集合,将懒操作符的性能提高了约25%-30%,将transducer的性能提高了约40%-50%。
10个元素
(doall (distinct coll)) 5.773439 µs => 4.179092 µs (-27%)
(into [] (distinct) coll) 3.238236 µs => 1.943254 µs (-39%)
100个元素
(doall (distinct coll)) 67.725764 µs => 42.129993 µs (-37%)
(into [] (distinct) coll) 35.702741 µs => 16.495947 µs (-53%)
1000个元素
(doall (distinct coll)) 540.652739 µs => 399.053873 µs (-26%)
(into [] (distinct) coll) 301.423077 µs => 164.025500 µs (-45%)
10000个元素
(doall (distinct coll)) 3.439137 ms => 3.058872 ms (-11%)
(into [] (distinct) coll) 1.437390 ms => 848.277178 µs (-40%)
基准测试代码:
https://gist.github.com/tonsky/97dfe1f9c48eccafc983a49c7042fb21