From 1ecf8872b61b6fc10307003a1d6c401bef4c7d5a Mon Sep 17 00:00:00 2001 From: Jutho Date: Mon, 9 Feb 2015 10:00:23 +0100 Subject: [PATCH] improve setindex! performance --- base/multidimensional.jl | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 18972b800ef72..00fdc8983291e 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -259,6 +259,23 @@ end stagedfunction setindex!(A::Array, x, J::Union(Real,AbstractArray)...) N = length(J) + if x<:AbstractArray + ex=quote + X = x + @ncall $N setindex_shape_check X I + Xs = start(X) + @nloops $N i d->(1:length(I_d)) d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin + v, Xs = next(X, Xs) + @inbounds A[offset_0] = v + end + end + else + ex=quote + @nloops $N i d->(1:length(I_d)) d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin + @inbounds A[offset_0] = x + end + end + end quote @nexprs $N d->(J_d = J[d]) @ncall $N checkbounds A J @@ -266,20 +283,7 @@ stagedfunction setindex!(A::Array, x, J::Union(Real,AbstractArray)...) stride_1 = 1 @nexprs $N d->(stride_{d+1} = stride_d*size(A,d)) @nexprs $N d->(offset_d = 1) # really only need offset_$N = 1 - if !isa(x, AbstractArray) - @nloops $N i d->(1:length(I_d)) d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin - @inbounds A[offset_0] = x - end - else - X = x - @ncall $N setindex_shape_check X I - iter = eachindex(X) - Xs = start(iter) - @nloops $N i d->(1:length(I_d)) d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin - Xindex, Xs = next(iter, Xs) - @inbounds A[offset_0] = X[Xindex] - end - end + $ex A end end