diff --git a/base/anyall.jl b/base/anyall.jl index e51515bb3187d..7ab76899bd2ac 100644 --- a/base/anyall.jl +++ b/base/anyall.jl @@ -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. @@ -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) @@ -227,5 +255,3 @@ end return _all_tuple(f, anymissing, rest...) end @inline _all_tuple(f, anymissing) = anymissing ? missing : true - -all(::Tuple{Missing}) = missing diff --git a/base/tuple.jl b/base/tuple.jl index 2ff8a1185a007..4982ef1b23eb0 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -656,19 +656,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