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

performance improvements for any and all #57091

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
30 changes: 28 additions & 2 deletions base/anyall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ for ItrT = (Tuple,Any)
end
end

# When the function is side effect-free, we may avoid short-circuiting to help
# vectorize the loop.
function _any(::typeof(identity), itr::Tuple{Vararg{Bool}}, ::Colon)
@_terminates_locally_meta
r = false
for i in eachindex(itr)
# Avoid bounds checking to help vectorization. Use `getfield` directly,
# instead of `@inbounds itr[i]`, for better effects.
v = getfield(itr, i, false)
r |= v
end
r
end

# Specialized versions of any(f, ::Tuple)
# We fall back to the for loop implementation all elements have the same type or
# if the tuple is too large.
Expand Down Expand Up @@ -205,6 +219,20 @@ for ItrT = (Tuple,Any)
end
end

# When the function is side effect-free, we may avoid short-circuiting to help
# vectorize the loop.
function _all(::typeof(identity), itr::Tuple{Vararg{Bool}}, ::Colon)
@_terminates_locally_meta
r = true
for i in eachindex(itr)
# Avoid bounds checking to help vectorization. Use `getfield` directly,
# instead of `@inbounds itr[i]`, for better effects.
v = getfield(itr, i, false)
r &= v
end
r
end

# Specialized versions of all(f, ::Tuple),
# This is similar to any(f, ::Tuple) defined above.
function all(f, itr::Tuple)
Expand All @@ -227,5 +255,3 @@ end
return _all_tuple(f, anymissing, rest...)
end
@inline _all_tuple(f, anymissing) = anymissing ? missing : true

all(::Tuple{Missing}) = missing
13 changes: 0 additions & 13 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -654,19 +654,6 @@ prod(x::Tuple{}) = 1
# than the general prod definition is available.
prod(x::Tuple{Int, Vararg{Int}}) = *(x...)

all(x::Tuple{}) = true
all(x::Tuple{Bool}) = x[1]
all(x::Tuple{Bool, Bool}) = x[1]&x[2]
all(x::Tuple{Bool, Bool, Bool}) = x[1]&x[2]&x[3]
all(x::Tuple{Any}) = x[1] || return false
all(f, x::Tuple{}) = true
all(f, x::Tuple{Any}) = all((f(x[1]),))

any(x::Tuple{}) = false
any(x::Tuple{Bool}) = x[1]
any(x::Tuple{Bool, Bool}) = x[1]|x[2]
any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3]

# a version of `in` esp. for NamedTuple, to make it pure, and not compiled for each tuple length
function sym_in(x::Symbol, itr::Tuple{Vararg{Symbol}})
@noinline
Expand Down
Loading