Specialise any and all for tuples#44063
Merged
KristofferC merged 6 commits intoJuliaLang:masterfrom May 9, 2022
Merged
Conversation
Member
|
|
simeonschaub
reviewed
Feb 7, 2022
Contributor
Author
|
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 : falseShould this be limited to small tuples? |
0cbdce3 to
d329e32
Compare
iterate(::ProductIterator)any and all for tuples
Member
|
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
Contributor
Author
|
@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
Member
|
@jipolanco Is this good to go? |
Contributor
Author
|
@KristofferC Yes I think it's ready to be merged. |
aviatesk
added a commit
that referenced
this pull request
May 9, 2022
aviatesk
added a commit
that referenced
this pull request
May 10, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This avoids a small allocation (and improves performance) when iterating over a
ProductIteratorconstructed 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
ProductIteratorissue.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: