Skip to content

Commit 75cb2f6

Browse files
authored
performance improvements for any and all (#57091)
* Help ensure vectorization of `any` and `all` for homogeneous tuples of `Bool`, by avoiding bounds-checking and avoiding short-circuiting. Inspired by #55673, but more general by relying on a loop instead of on recursion, thus being performant for any input length. * Delete a bunch of now-useless methods. * *eight* methods of `all` are deleted * four methods of `any` are deleted Closes #55673
1 parent 528e07b commit 75cb2f6

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

base/anyall.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ for ItrT = (Tuple,Any)
132132
end
133133
end
134134

135+
# When the function is side effect-free, we may avoid short-circuiting to help
136+
# vectorize the loop.
137+
function _any(::typeof(identity), itr::Tuple{Vararg{Bool}}, ::Colon)
138+
@_terminates_locally_meta
139+
r = false
140+
for i in eachindex(itr)
141+
# Avoid bounds checking to help vectorization. Use `getfield` directly,
142+
# instead of `@inbounds itr[i]`, for better effects.
143+
v = getfield(itr, i, false)
144+
r |= v
145+
end
146+
r
147+
end
148+
135149
# Specialized versions of any(f, ::Tuple)
136150
# We fall back to the for loop implementation all elements have the same type or
137151
# if the tuple is too large.
@@ -205,6 +219,20 @@ for ItrT = (Tuple,Any)
205219
end
206220
end
207221

222+
# When the function is side effect-free, we may avoid short-circuiting to help
223+
# vectorize the loop.
224+
function _all(::typeof(identity), itr::Tuple{Vararg{Bool}}, ::Colon)
225+
@_terminates_locally_meta
226+
r = true
227+
for i in eachindex(itr)
228+
# Avoid bounds checking to help vectorization. Use `getfield` directly,
229+
# instead of `@inbounds itr[i]`, for better effects.
230+
v = getfield(itr, i, false)
231+
r &= v
232+
end
233+
r
234+
end
235+
208236
# Specialized versions of all(f, ::Tuple),
209237
# This is similar to any(f, ::Tuple) defined above.
210238
function all(f, itr::Tuple)
@@ -227,5 +255,3 @@ end
227255
return _all_tuple(f, anymissing, rest...)
228256
end
229257
@inline _all_tuple(f, anymissing) = anymissing ? missing : true
230-
231-
all(::Tuple{Missing}) = missing

base/tuple.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -656,19 +656,6 @@ prod(x::Tuple{}) = 1
656656
# than the general prod definition is available.
657657
prod(x::Tuple{Int, Vararg{Int}}) = *(x...)
658658

659-
all(x::Tuple{}) = true
660-
all(x::Tuple{Bool}) = x[1]
661-
all(x::Tuple{Bool, Bool}) = x[1]&x[2]
662-
all(x::Tuple{Bool, Bool, Bool}) = x[1]&x[2]&x[3]
663-
all(x::Tuple{Any}) = x[1] || return false
664-
all(f, x::Tuple{}) = true
665-
all(f, x::Tuple{Any}) = all((f(x[1]),))
666-
667-
any(x::Tuple{}) = false
668-
any(x::Tuple{Bool}) = x[1]
669-
any(x::Tuple{Bool, Bool}) = x[1]|x[2]
670-
any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3]
671-
672659
# a version of `in` esp. for NamedTuple, to make it pure, and not compiled for each tuple length
673660
function sym_in(x::Symbol, itr::Tuple{Vararg{Symbol}})
674661
@noinline

0 commit comments

Comments
 (0)