From 3a3683c1307a5a67de1d7e6cf90fa1647edeada9 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Mon, 7 May 2018 12:02:34 +0200 Subject: [PATCH] Fix accumulate/cumsum performance regression by adding getindex(::LinearIndices, ::AbstractRange) Plus two small fixes. --- base/indices.jl | 7 +++++-- test/abstractarray.jl | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/base/indices.jl b/base/indices.jl index 9e23ee62c1b406..62931be93c5e8a 100644 --- a/base/indices.jl +++ b/base/indices.jl @@ -357,11 +357,14 @@ LinearIndices(A::Union{AbstractArray,SimpleVector}) = LinearIndices(axes(A)) IndexStyle(::Type{<:LinearIndices}) = IndexLinear() axes(iter::LinearIndices) = iter.indices size(iter::LinearIndices) = map(unsafe_length, iter.indices) -length(iter::LinearIndices) = prod(size(iter)) -function getindex(iter::LinearIndices, i::Int) +@propagate_inbounds function getindex(iter::LinearIndices, i::Int) @boundscheck checkbounds(iter, i) i end +@propagate_inbounds function getindex(iter::LinearIndices, i::AbstractRange{<:Integer}) + @boundscheck checkbounds(iter, i) + @inbounds (first(iter):last(iter))[i] +end # More efficient iteration — predominantly for non-vector LinearIndices # but one-dimensional LinearIndices must be special-cased to support OffsetArrays start(iter::LinearIndices{1}) = start(iter.indices[1]) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 57fef29c33de0b..2183e4013a77a7 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -119,11 +119,13 @@ end for i in ((), fill(0)) @test LinearIndices(i)[1] == 1 @test_throws BoundsError LinearIndices(i)[2] + @test_throws BoundsError LinearIndices(i)[1:2] @test LinearIndices(i)[1,1] == 1 @test LinearIndices(i)[] == 1 @test size(LinearIndices(i)) == () @test CartesianIndices(i)[1] == CartesianIndex() @test_throws BoundsError CartesianIndices(i)[2] + @test_throws BoundsError CartesianIndices(i)[1:2] end end @@ -135,6 +137,9 @@ end @test LinearIndices((3,))[2,1] == 2 @test LinearIndices((3,))[[1]] == [1] @test size(LinearIndices((3,))) == (3,) + @test LinearIndices((3,))[1:2] === 1:2 + @test LinearIndices((3,))[1:2:3] === 1:2:3 + @test_throws BoundsError LinearIndices((3,))[2:4] @test_throws BoundsError CartesianIndices((3,))[2,2] # ambiguity btw cartesian indexing and linear indexing in 1d when # indices may be nontraditional @@ -162,6 +167,9 @@ end @test cartesian[vec(linear)] == vec(cartesian) @test cartesian[cartesian] == cartesian @test cartesian[vec(cartesian)] == vec(cartesian) + @test linear[2:3] === 2:3 + @test linear[3:-1:1] === 3:-1:1 + @test_throws BoundsError linear[4:13] end @testset "3-dimensional" begin