-
Notifications
You must be signed in to change notification settings - Fork 46
Specialize copy and getindex with Colons #207
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #207 +/- ##
==========================================
+ Coverage 98.33% 98.36% +0.02%
==========================================
Files 5 5
Lines 301 305 +4
==========================================
+ Hits 296 300 +4
Misses 5 5
Continue to review full report at Codecov.
|
|
Before: julia> s = SArray{Tuple{2,2,2},Int,3,8}((1,2,3,4,5,6,7,8))
2×2×2 SArray{Tuple{2,2,2},Int64,3,8} with indices SOneTo(2)×SOneTo(2)×SOneTo(2):
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> so = OffsetArray(s, axes(s));
julia> so[:] # not static anymore
8-element Array{Int64,1}:
1
2
3
4
5
6
7
8After 0155aae julia> so[:] # still static
8-element SArray{Tuple{8},Int64,1,8} with indices SOneTo(8):
1
2
3
4
5
6
7
8Also, Before: julia> a = ones(2000, 2000);
julia> ao = OffsetArray(a, 2, 2);
julia> @btime $a[:]; # uses fast contiguous indexing with unsafe_copyto!
4.196 ms (2 allocations: 30.52 MiB)
julia> @btime $ao[:];
7.010 ms (2 allocations: 30.52 MiB)After 0155aae julia> @btime $ao[:];
4.151 ms (2 allocations: 30.52 MiB) |
| @inline function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N} | ||
| @propagate_inbounds Base.getindex(A::OffsetArray{<:Any,0}) = A.parent[] | ||
|
|
||
| @inline function Base.getindex(A::OffsetArray{<:Any,N}, I::Vararg{Int,N}) where N |
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 guess this should be @propagate_inbounds too?
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.
Should it? The idea here was to carry out the bounds checking using the axes of the OffsetArray and not to propagate it to the parent.
ca9914a to
1ec64a8
Compare
|
Good to merge? |
There are a couple of changes that I have removed from other PRs, as they are potentially breaking (leads to changes in return types). We may consider these for version 2.0 (or perhaps a 1.x minor release if these are not considered breaking).
copyis forwarded to the parent. This preserves the type of the parent in case there is a special structure associated with it. For example:On master:
The
copyoperation on theAdjointof anAbstractVectorpreserves its type, as a row-vector is distinct from a 1-row matrix. HoweverOffsetArrays do not preserve this type information. Also for immutable parent structs such asAbstarctRanges andStaticArrays, thecopyappears to not respect the parent's behavior:After this PR:
getindexwith as manyColons as the number of dimensions may preserve the type of the parent. This operation is similar tocopy, and in the case ofAbstactRanges it's actually implemented as as acopy.