From 9125a9cfae02758053b0f1056a93594c33bd4dcf Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 10 Jul 2016 19:13:07 -0500 Subject: [PATCH] Faster and simpler indices for SubArray --- base/abstractarray.jl | 4 ++++ base/subarray.jl | 16 +++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index a65254b46fbe1b..554099a8218bba 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -51,6 +51,9 @@ end indices1{T}(A::AbstractArray{T,0}) = OneTo(1) indices1{T}(A::AbstractArray{T}) = (@_inline_meta; indices(A)[1]) +unsafe_indices(A) = indices(A) +unsafe_indices(r::Range) = (OneTo(unsafe_length(r)),) # Ranges use checked_sub for size + """ linearindices(A) @@ -252,6 +255,7 @@ Throw an error if the specified `indexes` are not in bounds for the given `array function checkbounds(A::AbstractArray, I...) @_inline_meta checkbounds(Bool, A, I...) || throw_boundserror(A, I) + nothing end function checkbounds(A::AbstractArray, i) @_inline_meta diff --git a/base/subarray.jl b/base/subarray.jl index e9a4cf1c57765a..48446fcc60cb4a 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -278,17 +278,11 @@ end # they are taken from the range/vector # Since bounds-checking is performance-critical and uses # indices, it's worth optimizing these implementations thoroughly -indices(S::SubArray) = (@_inline_meta; _indices_sub(S, 1, S.indexes...)) -_indices_sub(S::SubArray, dim::Int) = () -_indices_sub(S::SubArray, dim::Int, ::Real, I...) = (@_inline_meta; _indices_sub(S, dim+1, I...)) -_indices_sub(S::SubArray, dim::Int, ::Colon, I...) = (@_inline_meta; (indices(parent(S), dim), _indices_sub(S, dim+1, I...)...)) -_indices_sub(S::SubArray, dim::Int, i1::AbstractArray, I...) = (@_inline_meta; (indices(i1)..., _indices_sub(S, dim+1, I...)...)) -indices1(S::SubArray) = (@_inline_meta; _indices1(S, 1, S.indexes...)) -_indices1(S::SubArray, dim) = OneTo(1) -_indices1(S::SubArray, dim, i1::Real, I...) = (@_inline_meta; _indices1(S, dim+1, I...)) -_indices1(S::SubArray, dim, i1::Colon, I...) = (@_inline_meta; indices(parent(S), dim)) -_indices1(S::SubArray, dim, i1::AbstractArray, I...) = (@_inline_meta; indices1(i1)) -_indices1{T}(S::SubArray, dim, i1::AbstractArray{T,0}, I...) = (@_inline_meta; _indices1(S, dim+1, I...)) +indices(S::SubArray) = (@_inline_pure_meta; _indices_sub(S, indices(S.parent), S.indexes...)) +_indices_sub(S::SubArray, pinds) = () +_indices_sub(S::SubArray, pinds, ::Real, I...) = _indices_sub(S, tail(pinds), I...) +_indices_sub(S::SubArray, pinds, ::Colon, I...) = (pinds[1], _indices_sub(S, tail(pinds), I...)...) +_indices_sub(S::SubArray, pinds, i1::AbstractArray, I...) = (unsafe_indices(i1)..., _indices_sub(S, tail(pinds), I...)...) ## Compatability # deprecate?