From 938b56d860564fcf9be61144318e0a73382b186f Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sat, 9 Jul 2016 05:19:21 -0500 Subject: [PATCH] Force inlining of `linearindices` and `indices1`. Ref #17340 --- base/abstractarray.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 7338d1d072045..c9933b533e469 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -45,8 +45,11 @@ function indices{T,N}(A::AbstractArray{T,N}) map(s->OneTo(s), size(A)) end +# Performance optimization: get rid of a branch on `d` in `indices(A, +# d)` for d=1. 1d arrays are heavily used, and the first dimension +# comes up in other applications. indices1{T}(A::AbstractArray{T,0}) = OneTo(1) -indices1{T}(A::AbstractArray{T}) = indices(A)[1] +indices1{T}(A::AbstractArray{T}) = (@_inline_meta; indices(A)[1]) """ linearindices(A) @@ -60,8 +63,8 @@ is `indices(A, 1)`. Calling this function is the "safe" way to write algorithms that exploit linear indexing. """ -linearindices(A) = 1:length(A) -linearindices(A::AbstractVector) = indices1(A) +linearindices(A) = (@_inline_meta; 1:length(A)) +linearindices(A::AbstractVector) = (@_inline_meta; indices1(A)) eltype{T}(::Type{AbstractArray{T}}) = T eltype{T,N}(::Type{AbstractArray{T,N}}) = T elsize{T}(::AbstractArray{T}) = sizeof(T)