Skip to content

Commit 0b4a455

Browse files
committed
make ReshapedArray carry size rather than strides
with this we can reuse mi
1 parent 982b923 commit 0b4a455

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

base/multidimensional.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ module IteratorsMD
628628
fr, vr = _splitlinear(rs, last(iter.indices[1]))
629629
outer = @inbounds if ndims(oiter) == 1
630630
CartesianIndices((oiter.indices[1][vl:vr],))
631-
else
632-
view(ReshapedArray(oiter, (length(oiter),), ()), vl:vr)
631+
else # reuse mi when possible
632+
view(ReshapedArray(oiter, (length(oiter),), Base.safetail(rs.mi)), vl:vr)
633633
end
634634
# Use Generator to make inner loop branchless
635635
@inline function genouter(i::Int, I::CartesianIndex)

base/reshapedarray.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ _reshape(R::ReshapedArray, dims::Dims) = _reshape(R.parent, dims)
188188

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

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

218218
@inline ind2sub_rs(ax, ::Tuple{}, i::Int) = (i,)
219-
@inline ind2sub_rs(ax, strds, i) = _ind2sub_rs(ax, strds, i - 1)
220-
@inline _ind2sub_rs(ax, ::Tuple{}, ind) = (ind + first(ax[end]),)
221-
@inline function _ind2sub_rs(ax, strds, ind)
222-
d, r = divrem(ind, strds[1])
223-
(_ind2sub_rs(front(ax), tail(strds), r)..., d + first(ax[end]))
219+
@inline ind2sub_rs(ax, szs, i) = _ind2sub_rs(ax, szs, i - 1)
220+
@inline _ind2sub_rs(ax, ::Tuple{}, ind) = (ind + first(ax[1]),)
221+
@inline function _ind2sub_rs(ax, szs, ind)
222+
d, r = divrem(ind, szs[1])
223+
(r + first(ax[1]), _ind2sub_rs(tail(ax), tail(szs), d)...)
224224
end
225225
offset_if_vec(i::Integer, axs::Tuple{<:AbstractUnitRange}) = i + first(axs[1]) - 1
226226
offset_if_vec(i::Integer, axs::Tuple) = i

0 commit comments

Comments
 (0)