JuliaLang/julia

SubArray indexing/yet another sketch for an ArrayView implementation

timholy opened this issue · 1 comments

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.

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