Skip to content

Commit

Permalink
More consistent inlining strategy:
Browse files Browse the repository at this point in the history
The most important thing here is to ensure that scalar indexing never allocates and always inlines.
  • Loading branch information
mbauman committed Mar 16, 2015
1 parent a7ea8e3 commit 17c649b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
4 changes: 2 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,11 @@ setindex!(t::AbstractArray, x) = throw(MethodError(setindex!, (t, x)))

stagedfunction getindex(A::AbstractArray, I...)
Isplat = Expr[:(I[$d]) for d = 1:length(I)]
:(_getindex(linearindexing(A), A, $(Isplat...)))
:($(Expr(:meta, :inline)); _getindex(linearindexing(A), A, $(Isplat...)))
end
stagedfunction unsafe_getindex(A::AbstractArray, I...)
Isplat = Expr[:(I[$d]) for d = 1:length(I)]
:(_unsafe_getindex(linearindexing(A), A, $(Isplat...)))
:($(Expr(:meta, :inline)); _unsafe_getindex(linearindexing(A), A, $(Isplat...)))
end
## Internal defitions
_getindex(::LinearFast, A::AbstractArray) = getindex(A, 1)
Expand Down
11 changes: 4 additions & 7 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ end
# sensitive.
_getindex(::LinearFast, A::AbstractArray, index::CartesianIndex{1}) = getindex(A, index[1])
stagedfunction _getindex{N}(l::LinearFast, A::AbstractArray, index::CartesianIndex{N})
:(@ncall $N _getindex l A d->index[d])
:($(Expr(:meta, :inline)); @ncall $N _getindex l A d->index[d])
end
stagedfunction _getindex{N}(::LinearSlow, A::AbstractArray, index::CartesianIndex{N})
:(@ncall $N getindex A d->index[d])
:($(Expr(:meta, :inline)); @ncall $N getindex A d->index[d])
end
_unsafe_getindex(::LinearFast, A::AbstractArray, index::CartesianIndex{1}) = unsafe_getindex(A, index[1])
stagedfunction _unsafe_getindex{N}(l::LinearFast, A::AbstractArray, index::CartesianIndex{N})
:(@ncall $N _unsafe_getindex l A d->index[d])
:($(Expr(:meta, :inline)); @ncall $N _unsafe_getindex l A d->index[d])
end
stagedfunction _unsafe_getindex{N}(::LinearSlow, A::AbstractArray, index::CartesianIndex{N})
:(@ncall $N unsafe_getindex A d->index[d])
:($(Expr(:meta, :inline)); @ncall $N unsafe_getindex A d->index[d])
end

# Indexing with just one array is always linear in the source, fast or slow
Expand All @@ -254,7 +254,6 @@ stagedfunction _unsafe_getindex!(::LinearFast, dest::AbstractArray, ::LinearFast
N = length(I)
Isplat = Expr[:(I[$d]) for d = 1:N]
quote
$(Expr(:meta, :inline))
stride_1 = 1
@nexprs $N d->(stride_{d+1} = stride_d*size(src, d))
$(symbol(:offset_, N)) = 1
Expand All @@ -271,7 +270,6 @@ stagedfunction _unsafe_getindex!(::LinearFast, dest::AbstractArray, ::LinearSlow
N = length(I)
Isplat = Expr[:(I[$d]) for d = 1:N]
quote
$(Expr(:meta, :inline))
k = 1
@nloops $N i dest d->(@inbounds j_d = unsafe_getindex(I[d], i_d)) begin
v = @ncall $N unsafe_getindex src j
Expand All @@ -287,7 +285,6 @@ stagedfunction _unsafe_getindex!(::LinearSlow, dest::AbstractArray, ::LinearInde
N = length(I)
Isplat = Expr[:(I[$d]) for d = 1:N]
quote
$(Expr(:meta, :inline))
@nloops $N i dest d->(@inbounds j_d = unsafe_getindex(I[d], i_d)) begin
v = @ncall $N unsafe_getindex src j
@ncall $N unsafe_setindex! dest v i
Expand Down

0 comments on commit 17c649b

Please sign in to comment.