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

SubArray indexing/yet another sketch for an ArrayView implementation #5394

Closed
timholy opened this issue Jan 14, 2014 · 1 comment
Closed

Comments

@timholy
Copy link
Member

timholy commented Jan 14, 2014

The current implementation of getindex for SubArrays computes a linear index to access the parent array:

getindex{T}(s::SubArray{T,2}, i::Integer, j::Integer) =
    s.parent[s.first_index + (i-1)*s.strides[1] + (j-1)*s.strides[2]]

This is appropriate for StridedArrays, where the parent is an Array, but it's counterproductive if the parent is one which does not efficiently support linear indexing. A more general implementation would look something like this:

getindex{T}(s::SubArray{T,2}, i::Integer, j::Integer) =
    s.parent[s.indexes[1][i], s.indexes[2][j]]

The benchmarks in #5393 suggest that there may no longer be any in-principle performance disadvantages with this approach. (Currently r[i] for a range r is slow because the bounds check prevents inlining, but once #3796 gets merged I suspect this won't be a major barrier anymore, although ultimately we'll still want a way to turn off bounds-checking.)

The challenge would be slices: you'd want implementations like this:

getindex{T,A,I<:(Int,Ranges{Int})}(s::SubArray{T,1, A, I}, i::Integer) =
    s.parent[s.indexes[1], s.indexes[2][i]]

That's a lot of functions, but we could generate them. Not quite sure how this works for arbitrary dimensions, though.

@JeffBezanson
Copy link
Member

There are several issues about re-doing SubArrays; this can be discussed there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants