Skip to content
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

more efficient array indexing with AbstractUnitRange #39896

Merged
merged 21 commits into from
Apr 28, 2021

Conversation

sanjibansg
Copy link
Contributor

@sanjibansg sanjibansg commented Mar 3, 2021

Fixes #39858
Relaxed method signature for getindex(A::Array, I:: UnitRange{Int}) which now has AbstractUnitRange{<:Integer} as the second argument.

@simeonschaub simeonschaub added arrays [a, r, r, a, y, s] needs tests Unit tests are required for this change labels Mar 3, 2021
@sostock
Copy link
Contributor

sostock commented Mar 3, 2021

Some tests should be added to check that indexing still works correctly for newly-allowed types of ranges, for example Base.OneTo, UnitRange{UInt} and UnitRange{BigInt}.

@jishnub
Copy link
Contributor

jishnub commented Mar 7, 2021

Would be good to have this, @Kahanikaar could you add the tests that have been suggested? Not too sure where the tests should go, perhaps to test/abstractarray.jl? You may create a new testset block stating the issue number where you can add the tests. Have a look at the test files to get an idea.

@sanjibansg sanjibansg marked this pull request as ready for review March 7, 2021 08:41
@sanjibansg
Copy link
Contributor Author

Would be good to have this, @Kahanikaar could you add the tests that have been suggested? Not too sure where the tests should go, perhaps to test/abstractarray.jl? You may create a new testset block stating the issue number where you can add the tests. Have a look at the test files to get an idea.

Sure, will look after this, will add the tests to test/abstractarray.jl

@sanjibansg
Copy link
Contributor Author

Tests added as asked. Do review @sostock @jishnub

test/abstractarray.jl Outdated Show resolved Hide resolved
Copy link
Contributor

@sostock sostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the test arrays only contain ones, we don’t know that it actually copies the correct numbers. Better use rand or something like collect(1:10).

@sanjibansg
Copy link
Contributor Author

If the test arrays only contain ones, we don’t know that it actually copies the correct numbers. Better use rand or something like collect(1:10).

Tests now use collect in place of ones.

@sanjibansg sanjibansg requested a review from sostock March 7, 2021 18:22
Copy link
Member

@simeonschaub simeonschaub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and welcome! Hope this hasn't been too unpleasant an experience, but looks good to me now.

@sanjibansg
Copy link
Contributor Author

Thanks for the PR and welcome! Hope this hasn't been too unpleasant an experience, but looks good to me now.

Not at all. It was really great contributing to Julia. Thanks for all the help! @jishnub @sostock @simeonschaub

test/abstractarray.jl Outdated Show resolved Hide resolved
test/abstractarray.jl Outdated Show resolved Hide resolved
@sostock sostock removed the needs tests Unit tests are required for this change label Mar 7, 2021
@sanjibansg sanjibansg requested a review from sostock March 7, 2021 22:33
test/abstractarray.jl Outdated Show resolved Hide resolved
test/abstractarray.jl Outdated Show resolved Hide resolved
Copy link
Contributor

@sostock sostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might also be good to test that indexing preserves the type, i.e., typeof(a[x]) === typeof(a).

@jishnub
Copy link
Contributor

jishnub commented Mar 8, 2021

This is not true in general though, although it probably holds for the range types in Base that use 1-based indexing. If this test is added, it's better to leave a comment specifying this. Types are perhaps an internal detail, and checking for types might be too strict a test. Checking for the values and the axes should suffice in most cases.

Eg cases where this doesn't hold (using OffsetArrays and StaticArrays):

julia> ones(10)[SOneTo(2)]
2-element SizedArray{Tuple{2},Float64,1,1,Array{Float64,1}} with indices SOneTo(2):
 1.0
 1.0

julia> ones(10)[Base.IdentityUnitRange(3:5)]
3-element OffsetArray(::Array{Float64,1}, 3:5) with eltype Float64 with indices 3:5:
 1.0
 1.0
 1.0

@sanjibansg sanjibansg requested a review from sostock March 14, 2021 13:21
@jishnub
Copy link
Contributor

jishnub commented Apr 3, 2021

@Kahanikaar is this ready? Waiting for this before merging JuliaArrays/OffsetArrays.jl#211

@sanjibansg
Copy link
Contributor Author

sanjibansg commented Apr 3, 2021

@Kahanikaar is this ready? Waiting for this before merging JuliaArrays/OffsetArrays.jl#211

Commits are ready and approved, should I squash them now? @jishnub

@sostock
Copy link
Contributor

sostock commented Apr 3, 2021

Squashing isn’t necessary, the PR will be squashed when merging it. But it would be nice to resolve the merge conflict.

Copy link
Contributor

@sostock sostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conflict wasn’t resolved correctly, there is an end missing.

test/abstractarray.jl Show resolved Hide resolved
Co-authored-by: Sebastian Stock <[email protected]>
test/abstractarray.jl Outdated Show resolved Hide resolved
Co-authored-by: Sebastian Stock <[email protected]>
@jishnub
Copy link
Contributor

jishnub commented Apr 28, 2021

Gentle bump, since this is ready perhaps this may be reviewed? Would be nice to have this merged as there is a demonstrable performance improvement here.

@simeonschaub simeonschaub changed the title Added: Indexing an Array with a single AbstractUnitRange might use contiguity of elements to run faster more efficient array indexing with AbstractUnitRange Apr 28, 2021
@simeonschaub simeonschaub added the merge me PR is reviewed. Merge when all tests are passing label Apr 28, 2021
@simeonschaub simeonschaub merged commit bbaca9d into JuliaLang:master Apr 28, 2021
@simeonschaub simeonschaub removed the merge me PR is reviewed. Merge when all tests are passing label Apr 28, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
jarlebring pushed a commit to jarlebring/julia that referenced this pull request May 4, 2021
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request May 9, 2021
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indexing an Array with a single AbstractUnitRange might use contiguity of elements to run faster
4 participants