Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve count performance #40564

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
for x in g
n += x::Bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just n += true now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no pred can return true or false, e.g.:

julia> collect(Base.Generator(ismissing, [1,missing,2]))
3-element Vector{Bool}:
 0
 1
 0

and we still need ::Bool assertion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, somehow I thought the first argument was a filter with this syntax.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is why I was surprised that the compiler does not vectorize it (the only thing I do is introduce Generator wrapper and add a function barrier - but both are needed to force vectorization)

end
return n
end
Expand Down