From b051d9fc7c1c63ac849d00c434dbb6c8324a00d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Thu, 22 Apr 2021 13:52:39 +0200 Subject: [PATCH 1/2] Improve count performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a speculative PR as I do not fully understand why the original code was not properly optimized by the compiler (so maybe a better fix is to change compiler). Here is an example of the performance difference: ``` julia> x = rand([1, missing], 10^6); julia> @btime count(ismissing, $x) 501.200 μs (0 allocations: 0 bytes) 500396 julia> @btime count(Base.Generator(ismissing, $x)) 70.800 μs (0 allocations: 0 bytes) 500396 ``` (this is run on 1.6, but this PR is essentially doing the same by introducing a function barrier) --- base/reduce.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index ca252a08922c3..3fbe7675d861e 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -1200,10 +1200,12 @@ count(itr; init=0) = count(identity, itr; init) count(f, itr; init=0) = _simple_count(f, itr, init) -function _simple_count(pred, itr, init::T) where {T} +_simple_count(pred, itr, init) = _simple_count_helper(Generator(pred, itr), init) + +function _simple_count_helper(g, init::T) where {T} n::T = init for x in itr - n += pred(x)::Bool + n += x::Bool end return n end From 72651dffad61f6cd2b4db9533c8cf82ce9b87eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Thu, 22 Apr 2021 13:57:27 +0200 Subject: [PATCH 2/2] Update base/reduce.jl --- base/reduce.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/reduce.jl b/base/reduce.jl index 3fbe7675d861e..cbd661e19220e 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -1204,7 +1204,7 @@ _simple_count(pred, itr, init) = _simple_count_helper(Generator(pred, itr), init function _simple_count_helper(g, init::T) where {T} n::T = init - for x in itr + for x in g n += x::Bool end return n