-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Specialise any
and all
for tuples
#44063
Conversation
|
Seems reasonable to add an optimised version of Here is one possible implementation: # any(f, itr) = _any(f, itr, :) # definition in base/reduce.jl
_any(f, itr::Tuple, ::Colon) = _any_tuple(f, false, itr...)
@inline function _any_tuple(f, anymissing, x, rest...)
v = f(x)
if ismissing(v)
anymissing = true
elseif v
return true
end
return _any_tuple(f, anymissing, rest...)
end
@inline _any_tuple(f, anymissing) = anymissing ? missing : false Should this be limited to small tuples? |
0cbdce3
to
d329e32
Compare
iterate(::ProductIterator)
any
and all
for tuples
I think it's OK to put these optimization in # If eltype(x) is concrete. Use for loop
any(f, x::NTuple{N}) where {N} = _any(f, x, :)
# Otherwise force unroll to avoid union-split. (Only for small Tuple)
function any(f, x::NTuple{N,Any}) where {N}
N >= 32 && return _any(f, x, :) # Not tuned, just follow Any32
_any_tuple(f, false, x...)
end |
f1671b1
to
e8c25b4
Compare
@N5N3 Thanks for the suggestions! This should be taken care of now. |
Fall back to original for-loop implementation if the tuple either: - is a homogeneous tuple (all elements have the same type); - has length > 32.
7117bde
to
a55e3a5
Compare
@jipolanco Is this good to go? |
@KristofferC Yes I think it's ready to be merged. |
This avoids a small allocation (and improves performance) when iterating over a
ProductIterator
constructed using mixed iterator types. See #40283 for details.I couldn't really identify where the allocation comes from, and there's likely an underlying issue to be still fixed. But this does solve the
ProductIterator
issue.When used with homogeneous iterator types, this has no additional cost compared to the original short-circuiting version, as seen in the example below.
Also note that
Base.map
(used in this PR) was already being used inisdone(z::Zip)
.Closes #40283.
Example: