Skip to content

Commit

Permalink
Use sized based mi to speedup sequential access of ReshapedArray
Browse files Browse the repository at this point in the history
Update reshapedarray.jl
  • Loading branch information
N5N3 committed Feb 13, 2022
1 parent 8b1a1a8 commit 0f56891
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ _reshape(R::ReshapedArray, dims::Dims) = _reshape(R.parent, dims)

function __reshape(p::Tuple{AbstractArray,IndexStyle}, dims::Dims)
parent = p[1]
strds = front(size_to_strides(map(length, axes(parent))..., 1))
strds1 = map(s->max(1,Int(s)), strds) # for resizing empty arrays
mi = map(SignedMultiplicativeInverse, strds1)
ReshapedArray(parent, dims, reverse(mi))
szs = front(size(parent))
szs1 = map(s->max(1,Int(s)), szs) # for resizing empty arrays
mi = map(SignedMultiplicativeInverse, szs1)
ReshapedArray(parent, dims, mi)
end

function __reshape(p::Tuple{AbstractArray{<:Any,0},IndexCartesian}, dims::Dims)
Expand All @@ -216,11 +216,11 @@ unaliascopy(A::ReshapedArray) = typeof(A)(unaliascopy(A.parent), A.dims, A.mi)
dataids(A::ReshapedArray) = dataids(A.parent)

@inline ind2sub_rs(ax, ::Tuple{}, i::Int) = (i,)
@inline ind2sub_rs(ax, strds, i) = _ind2sub_rs(ax, strds, i - 1)
@inline ind2sub_rs(ax, szs, i) = _ind2sub_rs(ax, szs, i - 1)
@inline _ind2sub_rs(ax, ::Tuple{}, ind) = (ind + first(ax[end]),)
@inline function _ind2sub_rs(ax, strds, ind)
d, r = divrem(ind, strds[1])
(_ind2sub_rs(front(ax), tail(strds), r)..., d + first(ax[end]))
@inline function _ind2sub_rs(ax, szs, ind)
d, r = divrem(ind, szs[1])
(r + first(ax[1]), _ind2sub_rs(tail(ax), tail(szs), d)...)
end
offset_if_vec(i::Integer, axs::Tuple{<:AbstractUnitRange}) = i + first(axs[1]) - 1
offset_if_vec(i::Integer, axs::Tuple) = i
Expand Down

0 comments on commit 0f56891

Please sign in to comment.