-
-
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
Don't overload * for linearindexing type computations #11579
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,10 +113,10 @@ linearindexing{T<:AbstractArray}(::Type{T}) = LinearSlow() | |
linearindexing{T<:Array}(::Type{T}) = LinearFast() | ||
linearindexing{T<:Range}(::Type{T}) = LinearFast() | ||
|
||
*(::LinearFast, ::LinearFast) = LinearFast() | ||
*(::LinearSlow, ::LinearFast) = LinearSlow() | ||
*(::LinearFast, ::LinearSlow) = LinearSlow() | ||
*(::LinearSlow, ::LinearSlow) = LinearSlow() | ||
linearindexing(A::AbstractArray, B::AbstractArray) = linearindexing(linearindexing(A), linearindexing(B)) | ||
linearindexing(A::AbstractArray, B::AbstractArray...) = linearindexing(linearindexing(A), linearindexing(B...)) | ||
linearindexing(::LinearFast, ::LinearFast) = LinearFast() | ||
linearindexing(::LinearIndexing, ::LinearIndexing) = LinearSlow() | ||
|
||
# The real @inline macro is not available this early in the bootstrap, so this | ||
# internal macro splices the meta Expr directly into the function body. | ||
|
@@ -385,9 +385,14 @@ eachindex(::LinearFast, A::AbstractArray) = 1:length(A) | |
|
||
function eachindex(A::AbstractArray, B::AbstractArray) | ||
@_inline_meta | ||
eachindex(linearindexing(A)*linearindexing(B), A, B) | ||
eachindex(linearindexing(A,B), A, B) | ||
end | ||
function eachindex(A::AbstractArray, B::AbstractArray...) | ||
@_inline_meta | ||
eachindex(linearindexing(A,B...), A, B...) | ||
end | ||
eachindex(::LinearFast, A::AbstractArray, B::AbstractArray) = 1:max(length(A),length(B)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: why does this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out The idea is that, in cases where sizes differ, you should visit "each index," which means you should essentially traverse the bounding box. |
||
eachindex(::LinearFast, A::AbstractArray, B::AbstractArray...) = 1:max(length(A), map(length, B)...) | ||
|
||
isempty(a::AbstractArray) = (length(a) == 0) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like
linearindexing
for this; it makes sense. I haven't tried this, but could it be simplified with just two methods?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if I understand this correctly...
If I'm right, I think @timholy needs to add a comment explaining why that is all spelled out that way, to avoid some later well meaning julian from optimizing away his optimization...
If you have A, B, C, D, and A, B, C are LinearFast, and D is LinearSlow, the way he has it does two fast operations, and one slow, but yours does 3 slow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you only need the two-argument form. But yes, it could be written with the
Union
. I'll change that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timholy Was I correct about how using the Varargs form would actually make things slower?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ScottPJones #11248. But should be fine if it is inlined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I am correct, until the very nice #11248 gets merged in (hopefully soon)? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errr. #11248 is just a report. not a merge request....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Darn!!! I guess I was just too hopeful. Do you think you (or somebody) will be tackling that issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make all operations slow, if any are slow... The way it is now, some can stay fast
Sent from my iPhone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify,
LinearFast()
means "linear indexing is fast." When that's not true, we useCartesianIndex
, which is also fast 😄. Linear indexing is every-so-slightly (well, maybe a little more than slightly) faster than cartesian indexing for certain array types (most observable with high-dimensional arrays), but it's catastrophically slower for others. So all this logic is simply to use linear indexing when we know it's safe (LinearFast
), but use cartesian indexing (LinearSlow
) whenever we're unsure or know that linear would be a bad idea.All this code we're discussing is only about defining and reading traits; once the iteration scheme has been selected, it has no further impact on the cost of indexing.