• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #38043

12 Apr 2025 06:53AM UTC coverage: 19.945% (-0.3%) from 20.228%
#38043

push

local

web-flow
Eagerly evaluate indices in `eachindex` check (#58054)

This reduces TTFX in `eachindex` calls with multiple arguments, with
minimal impact on performance. Much of the latency comes from the error
path, and specializing it for the common case of 2 arguments helps a lot
with reducing latency. In this, I've also unrolled the `join` in the
error path, and we recursively generate a `LazyString`s instead. This
helps in reducing TTFX for a longer list of arguments.

```julia
julia> a = zeros(2,2);

julia> @time eachindex(a, a);
  0.046902 seconds (128.39 k allocations: 6.652 MiB, 99.93% compilation time) # nightly
  0.015368 seconds (19.91 k allocations: 1.048 MiB, 99.79% compilation time) # this PR

julia> @btime eachindex($a, $a, $a, $a, $a, $a, $a, $a);
  6.945 ns (0 allocations: 0 bytes) # nightly
  6.855 ns (0 allocations: 0 bytes) # this PR
```
This reduces TTFX for a longer list of arguments as well:
```julia
julia> @time eachindex(a, a, a, a, a, a, a, a);
  0.052552 seconds (196.87 k allocations: 10.068 MiB, 99.53% compilation time) # nightly
  0.043401 seconds (69.13 k allocations: 3.454 MiB, 99.34% compilation time) # this PR
```
For Cartesian indexing,
```julia
julia> a = zeros(2,2);

julia> v = view(a, 1:2, 1:2);

julia> @time eachindex(a, v);
  0.051333 seconds (171.34 k allocations: 8.921 MiB, 99.94% compilation time) # nightly
  0.016340 seconds (26.95 k allocations: 1.405 MiB, 99.79% compilation time) # this PR

julia> @btime eachindex($a, $v, $a, $v, $a, $v, $a, $v);
  9.339 ns (0 allocations: 0 bytes) # nightly
  9.357 ns (0 allocations: 0 bytes) # this PR
```

0 of 10 new or added lines in 3 files covered. (0.0%)

957 existing lines in 13 files now uncovered.

9861 of 49441 relevant lines covered (19.94%)

98617.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.39
/base/reinterpretarray.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
Gives a reinterpreted view (of element type T) of the underlying array (of element type S).
5
If the size of `T` differs from the size of `S`, the array will be compressed/expanded in
6
the first dimension. The variant `reinterpret(reshape, T, a)` instead adds or consumes the first dimension
7
depending on the ratio of element sizes.
8
"""
9
struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T, N}
10
    parent::A
11
    readable::Bool
12
    writable::Bool
13

14
    function throwbits(S::Type, T::Type, U::Type)
×
15
        @noinline
×
16
        throw(ArgumentError(LazyString("cannot reinterpret `", S, "` as `", T, "`, type `", U, "` is not a bits type")))
×
17
    end
18
    function throwsize0(S::Type, T::Type, msg)
×
19
        @noinline
×
20
        throw(ArgumentError(LazyString("cannot reinterpret a zero-dimensional `", S, "` array to `", T,
×
21
            "` which is of a ", msg, " size")))
22
    end
23
    function throwsingleton(S::Type, T::Type)
×
24
        @noinline
×
25
        throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T, "` which is a singleton type")))
×
26
    end
27

28
    global reinterpret
29

30
    @doc """
31
        reinterpret(T::DataType, A::AbstractArray)
32

33
    Construct a view of the array with the same binary data as the given
34
    array, but with `T` as element type.
35

36
    This function also works on "lazy" array whose elements are not computed until they are explicitly retrieved.
37
    For instance, `reinterpret` on the range `1:6` works similarly as on the dense vector `collect(1:6)`:
38

39
    ```jldoctest
40
    julia> reinterpret(Float32, UInt32[1 2 3 4 5])
41
    1×5 reinterpret(Float32, ::Matrix{UInt32}):
42
     1.0f-45  3.0f-45  4.0f-45  6.0f-45  7.0f-45
43

44
    julia> reinterpret(Complex{Int}, 1:6)
45
    3-element reinterpret(Complex{$Int}, ::UnitRange{$Int}):
46
     1 + 2im
47
     3 + 4im
48
     5 + 6im
49
    ```
50

51
    If the location of padding bits does not line up between `T` and `eltype(A)`, the resulting array will be
52
    read-only or write-only, to prevent invalid bits from being written to or read from, respectively.
53

54
    ```jldoctest
55
    julia> a = reinterpret(Tuple{UInt8, UInt32}, UInt32[1, 2])
56
    1-element reinterpret(Tuple{UInt8, UInt32}, ::Vector{UInt32}):
57
     (0x01, 0x00000002)
58

59
    julia> a[1] = 3
60
    ERROR: Padding of type Tuple{UInt8, UInt32} is not compatible with type UInt32.
61

62
    julia> b = reinterpret(UInt32, Tuple{UInt8, UInt32}[(0x01, 0x00000002)]); # showing will error
63

64
    julia> b[1]
65
    ERROR: Padding of type UInt32 is not compatible with type Tuple{UInt8, UInt32}.
66
    ```
67
    """
68
    function reinterpret(::Type{T}, a::A) where {T,N,S,A<:AbstractArray{S, N}}
69
        function thrownonint(S::Type, T::Type, dim)
1✔
70
            @noinline
×
71
            throw(ArgumentError(LazyString(
×
72
                "cannot reinterpret an `", S, "` array to `", T, "` whose first dimension has size `", dim,
73
                "`. The resulting array would have a non-integral first dimension.")))
74
        end
75
        function throwaxes1(S::Type, T::Type, ax1)
1✔
76
            @noinline
77
            throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T,
78
                "` when the first axis is ", ax1, ". Try reshaping first.")))
79
        end
80
        isbitstype(T) || throwbits(S, T, T)
1✔
81
        isbitstype(S) || throwbits(S, T, S)
1✔
82
        (N != 0 || sizeof(T) == sizeof(S)) || throwsize0(S, T, "different")
1✔
83
        if N != 0 && sizeof(S) != sizeof(T)
1✔
84
            ax1 = axes(a)[1]
9✔
85
            dim = length(ax1)
1✔
86
            if issingletontype(T)
1✔
87
                issingletontype(S) || throwsingleton(S, T)
×
88
            else
89
                rem(dim*sizeof(S),sizeof(T)) == 0 || thrownonint(S, T, dim)
9✔
90
            end
91
            first(ax1) == 1 || throwaxes1(S, T, ax1)
1✔
92
        end
93
        readable = array_subpadding(T, S)
1✔
94
        writable = array_subpadding(S, T)
1✔
95
        new{T, N, S, A, false}(a, readable, writable)
9✔
96
    end
97
    reinterpret(::Type{T}, a::AbstractArray{T}) where {T} = a
×
98

99
    # With reshaping
100
    function reinterpret(::typeof(reshape), ::Type{T}, a::A) where {T,S,A<:AbstractArray{S}}
×
101
        function throwintmult(S::Type, T::Type)
×
102
            @noinline
×
103
            throw(ArgumentError(LazyString("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got ",
×
104
                sizeof(T), ") and `sizeof(eltype(a))` (got ", sizeof(S), ") be an integer multiple of the other")))
105
        end
106
        function throwsize1(a::AbstractArray, T::Type)
×
107
            @noinline
×
108
            throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", eltype(a),
×
109
                " requires that `axes(a, 1)` (got ", axes(a, 1), ") be equal to 1:",
110
                sizeof(T) ÷ sizeof(eltype(a)), " (from the ratio of element sizes)")))
111
        end
112
        function throwfromsingleton(S, T)
×
113
            @noinline
×
114
            throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", S,
×
115
                " requires that ", T, " be a singleton type, since ", S, " is one")))
116
        end
117
        isbitstype(T) || throwbits(S, T, T)
×
118
        isbitstype(S) || throwbits(S, T, S)
×
119
        if sizeof(S) == sizeof(T)
×
120
            N = ndims(a)
×
121
        elseif sizeof(S) > sizeof(T)
×
122
            issingletontype(T) && throwsingleton(S, T)
×
123
            rem(sizeof(S), sizeof(T)) == 0 || throwintmult(S, T)
×
124
            N = ndims(a) + 1
×
125
        else
126
            issingletontype(S) && throwfromsingleton(S, T)
×
127
            rem(sizeof(T), sizeof(S)) == 0 || throwintmult(S, T)
×
128
            N = ndims(a) - 1
×
129
            N > -1 || throwsize0(S, T, "larger")
×
130
            axes(a, 1) == OneTo(sizeof(T) ÷ sizeof(S)) || throwsize1(a, T)
×
131
        end
132
        readable = array_subpadding(T, S)
×
133
        writable = array_subpadding(S, T)
×
134
        new{T, N, S, A, true}(a, readable, writable)
×
135
    end
136
    reinterpret(::typeof(reshape), ::Type{T}, a::AbstractArray{T}) where {T} = a
×
137
end
138

139
ReshapedReinterpretArray{T,N,S,A<:AbstractArray{S}} = ReinterpretArray{T,N,S,A,true}
140
NonReshapedReinterpretArray{T,N,S,A<:AbstractArray{S, N}} = ReinterpretArray{T,N,S,A,false}
141

142
"""
143
    reinterpret(reshape, T, A::AbstractArray{S}) -> B
144

145
Change the type-interpretation of `A` while consuming or adding a "channel dimension."
146

147
If `sizeof(T) = n*sizeof(S)` for `n>1`, `A`'s first dimension must be
148
of size `n` and `B` lacks `A`'s first dimension. Conversely, if `sizeof(S) = n*sizeof(T)` for `n>1`,
149
`B` gets a new first dimension of size `n`. The dimensionality is unchanged if `sizeof(T) == sizeof(S)`.
150

151
!!! compat "Julia 1.6"
152
    This method requires at least Julia 1.6.
153

154
# Examples
155

156
```jldoctest
157
julia> A = [1 2; 3 4]
158
2×2 Matrix{$Int}:
159
 1  2
160
 3  4
161

162
julia> reinterpret(reshape, Complex{Int}, A)    # the result is a vector
163
2-element reinterpret(reshape, Complex{$Int}, ::Matrix{$Int}) with eltype Complex{$Int}:
164
 1 + 3im
165
 2 + 4im
166

167
julia> a = [(1,2,3), (4,5,6)]
168
2-element Vector{Tuple{$Int, $Int, $Int}}:
169
 (1, 2, 3)
170
 (4, 5, 6)
171

172
julia> reinterpret(reshape, Int, a)             # the result is a matrix
173
3×2 reinterpret(reshape, $Int, ::Vector{Tuple{$Int, $Int, $Int}}) with eltype $Int:
174
 1  4
175
 2  5
176
 3  6
177
```
178
"""
179
reinterpret(::typeof(reshape), T::Type, a::AbstractArray)
180

181
reinterpret(::Type{T}, a::NonReshapedReinterpretArray) where {T} = reinterpret(T, a.parent)
×
182
reinterpret(::typeof(reshape), ::Type{T}, a::ReshapedReinterpretArray) where {T} = reinterpret(reshape, T, a.parent)
×
183

184
# Definition of StridedArray
185
StridedFastContiguousSubArray{T,N,A<:DenseArray} = FastContiguousSubArray{T,N,A}
186
StridedReinterpretArray{T,N,A<:Union{DenseArray,StridedFastContiguousSubArray},IsReshaped} = ReinterpretArray{T,N,S,A,IsReshaped} where S
187
StridedReshapedArray{T,N,A<:Union{DenseArray,StridedFastContiguousSubArray,StridedReinterpretArray}} = ReshapedArray{T,N,A}
188
StridedSubArray{T,N,A<:Union{DenseArray,StridedReshapedArray,StridedReinterpretArray},
189
    I<:Tuple{Vararg{Union{RangeIndex, ReshapedUnitRange, AbstractCartesianIndex}}}} = SubArray{T,N,A,I}
190
StridedArray{T,N} = Union{DenseArray{T,N}, StridedSubArray{T,N}, StridedReshapedArray{T,N}, StridedReinterpretArray{T,N}}
191
StridedVector{T} = StridedArray{T,1}
192
StridedMatrix{T} = StridedArray{T,2}
193
StridedVecOrMat{T} = Union{StridedVector{T}, StridedMatrix{T}}
194

195
strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)
×
196
stride(A::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, k::Integer) =
×
197
    k ≤ ndims(A) ? strides(A)[k] : length(A)
198

199
function strides(a::ReinterpretArray{T,<:Any,S,<:AbstractArray{S},IsReshaped}) where {T,S,IsReshaped}
×
200
    _checkcontiguous(Bool, a) && return size_to_strides(1, size(a)...)
×
201
    stp = strides(parent(a))
×
202
    els, elp = sizeof(T), sizeof(S)
×
203
    els == elp && return stp # 0dim parent is also handled here.
×
204
    IsReshaped && els < elp && return (1, _checked_strides(stp, els, elp)...)
×
205
    stp[1] == 1 || throw(ArgumentError("Parent must be contiguous in the 1st dimension!"))
×
206
    st′ = _checked_strides(tail(stp), els, elp)
×
207
    return IsReshaped ? st′ : (1, st′...)
×
208
end
209

210
@inline function _checked_strides(stp::Tuple, els::Integer, elp::Integer)
×
211
    if elp > els && rem(elp, els) == 0
×
212
        N = div(elp, els)
×
213
        return map(i -> N * i, stp)
×
214
    end
215
    drs = map(i -> divrem(elp * i, els), stp)
×
216
    all(i->iszero(i[2]), drs) ||
×
217
        throw(ArgumentError("Parent's strides could not be exactly divided!"))
218
    map(first, drs)
×
219
end
220

221
_checkcontiguous(::Type{Bool}, A::ReinterpretArray) = _checkcontiguous(Bool, parent(A))
×
222

223
similar(a::ReinterpretArray, T::Type, d::Dims) = similar(a.parent, T, d)
8✔
224

225
function check_readable(a::ReinterpretArray{T, N, S} where N) where {T,S}
226
    # See comment in check_writable
227
    if !a.readable && !array_subpadding(T, S)
4✔
228
        throw(PaddingError(T, S))
×
229
    end
230
end
231

232
function check_writable(a::ReinterpretArray{T, N, S} where N) where {T,S}
×
233
    # `array_subpadding` is relatively expensive (compared to a simple arrayref),
234
    # so it is cached in the array. However, it is computable at compile time if,
235
    # inference has the types available. By using this form of the check, we can
236
    # get the best of both worlds for the success case. If the types were not
237
    # available to inference, we simply need to check the field (relatively cheap)
238
    # and if they were we should be able to fold this check away entirely.
239
    if !a.writable && !array_subpadding(S, T)
×
240
        throw(PaddingError(T, S))
×
241
    end
242
end
243

244
## IndexStyle specializations
245

246
# For `reinterpret(reshape, T, a)` where we're adding a channel dimension and with
247
# `IndexStyle(a) == IndexLinear()`, it's advantageous to retain pseudo-linear indexing.
248
struct IndexSCartesian2{K} <: IndexStyle end   # K = sizeof(S) ÷ sizeof(T), a static-sized 2d cartesian iterator
249

250
IndexStyle(::Type{ReinterpretArray{T,N,S,A,false}}) where {T,N,S,A<:AbstractArray{S,N}} = IndexStyle(A)
×
251
function IndexStyle(::Type{ReinterpretArray{T,N,S,A,true}}) where {T,N,S,A<:AbstractArray{S}}
×
252
    if sizeof(T) < sizeof(S)
×
253
        IndexStyle(A) === IndexLinear() && return IndexSCartesian2{sizeof(S) ÷ sizeof(T)}()
×
254
        return IndexCartesian()
×
255
    end
256
    return IndexStyle(A)
×
257
end
258
IndexStyle(::IndexSCartesian2{K}, ::IndexSCartesian2{K}) where {K} = IndexSCartesian2{K}()
×
259

260
struct SCartesianIndex2{K}   # can't make <:AbstractCartesianIndex without N, and 2 would be a bit misleading
261
    i::Int
262
    j::Int
263
end
264
to_index(i::SCartesianIndex2) = i
×
265

266
struct SCartesianIndices2{K,R<:AbstractUnitRange{Int}} <: AbstractMatrix{SCartesianIndex2{K}}
267
    indices2::R
268
end
269
SCartesianIndices2{K}(indices2::AbstractUnitRange{Int}) where {K} = (@assert K::Int > 1; SCartesianIndices2{K,typeof(indices2)}(indices2))
×
270

271
eachindex(::IndexSCartesian2{K}, A::ReshapedReinterpretArray) where {K} = SCartesianIndices2{K}(eachindex(IndexLinear(), parent(A)))
×
272
@inline function eachindex(style::IndexSCartesian2{K}, A::AbstractArray, B::AbstractArray...) where {K}
×
273
    iter = eachindex(style, A)
×
NEW
274
    itersBs = map(C->eachindex(style, C), B)
×
NEW
275
    all(==(iter), itersBs) || throw_eachindex_mismatch_indices("axes", axes(A), map(axes, B)...)
×
UNCOV
276
    return iter
×
277
end
278

279
size(iter::SCartesianIndices2{K}) where K = (K, length(iter.indices2))
×
280
axes(iter::SCartesianIndices2{K}) where K = (OneTo(K), iter.indices2)
×
281

282
first(iter::SCartesianIndices2{K}) where {K} = SCartesianIndex2{K}(1, first(iter.indices2))
×
283
last(iter::SCartesianIndices2{K}) where {K}  = SCartesianIndex2{K}(K, last(iter.indices2))
×
284

285
@inline function getindex(iter::SCartesianIndices2{K}, i::Int, j::Int) where {K}
×
286
    @boundscheck checkbounds(iter, i, j)
×
287
    return SCartesianIndex2{K}(i, iter.indices2[j])
×
288
end
289

290
function iterate(iter::SCartesianIndices2{K}) where {K}
×
291
    ret = iterate(iter.indices2)
×
292
    ret === nothing && return nothing
×
293
    item2, state2 = ret
×
294
    return SCartesianIndex2{K}(1, item2), (1, item2, state2)
×
295
end
296

297
function iterate(iter::SCartesianIndices2{K}, (state1, item2, state2)) where {K}
×
298
    if state1 < K
×
299
        item1 = state1 + 1
×
300
        return SCartesianIndex2{K}(item1, item2), (item1, item2, state2)
×
301
    end
302
    ret = iterate(iter.indices2, state2)
×
303
    ret === nothing && return nothing
×
304
    item2, state2 = ret
×
305
    return SCartesianIndex2{K}(1, item2), (1, item2, state2)
×
306
end
307

308
SimdLoop.simd_outer_range(iter::SCartesianIndices2) = iter.indices2
×
309
SimdLoop.simd_inner_length(::SCartesianIndices2{K}, ::Any) where K = K
×
310
@inline function SimdLoop.simd_index(::SCartesianIndices2{K}, Ilast::Int, I1::Int) where {K}
×
311
    SCartesianIndex2{K}(I1+1, Ilast)
×
312
end
313

314
_maybe_reshape(::IndexSCartesian2, A::ReshapedReinterpretArray, I...) = A
×
315

316
# fallbacks
317
function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, I::Vararg{Int, N}) where {T,N}
×
318
    @_propagate_inbounds_meta
×
319
    getindex(A, I...)
×
320
end
321
function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, I::Vararg{Int, N}) where {T,N}
×
322
    @_propagate_inbounds_meta
×
323
    setindex!(A, v, I...)
×
324
end
325
# fallbacks for array types that use "pass-through" indexing (e.g., `IndexStyle(A) = IndexStyle(parent(A))`)
326
# but which don't handle SCartesianIndex2
327
function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, ind::SCartesianIndex2) where {T,N}
×
328
    @_propagate_inbounds_meta
×
329
    J = _ind2sub(tail(axes(A)), ind.j)
×
330
    getindex(A, ind.i, J...)
×
331
end
332
function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, ind::SCartesianIndex2) where {T,N}
×
333
    @_propagate_inbounds_meta
×
334
    J = _ind2sub(tail(axes(A)), ind.j)
×
335
    setindex!(A, v, ind.i, J...)
×
336
end
337
eachindex(style::IndexSCartesian2, A::AbstractArray) = eachindex(style, parent(A))
×
338

339
## AbstractArray interface
340

341
parent(a::ReinterpretArray) = a.parent
4✔
342
dataids(a::ReinterpretArray) = dataids(a.parent)
×
343
unaliascopy(a::NonReshapedReinterpretArray{T}) where {T} = reinterpret(T, unaliascopy(a.parent))
×
344
unaliascopy(a::ReshapedReinterpretArray{T}) where {T} = reinterpret(reshape, T, unaliascopy(a.parent))
×
345

346
function size(a::NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
347
    psize = size(a.parent)
1✔
348
    size1 = issingletontype(T) ? psize[1] : div(psize[1]*sizeof(S), sizeof(T))
1✔
349
    tuple(size1, tail(psize)...)
1✔
350
end
351
function size(a::ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
×
352
    psize = size(a.parent)
×
353
    sizeof(S) > sizeof(T) && return (div(sizeof(S), sizeof(T)), psize...)
×
354
    sizeof(S) < sizeof(T) && return tail(psize)
×
355
    return psize
×
356
end
357
size(a::NonReshapedReinterpretArray{T,0}) where {T} = ()
×
358

359
function axes(a::NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
360
    paxs = axes(a.parent)
1,227✔
361
    f, l = first(paxs[1]), length(paxs[1])
5✔
362
    size1 = issingletontype(T) ? l : div(l*sizeof(S), sizeof(T))
1,227✔
363
    tuple(oftype(paxs[1], f:f+size1-1), tail(paxs)...)
1,227✔
364
end
365
function axes(a::ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
×
366
    paxs = axes(a.parent)
×
367
    sizeof(S) > sizeof(T) && return (OneTo(div(sizeof(S), sizeof(T))), paxs...)
×
368
    sizeof(S) < sizeof(T) && return tail(paxs)
×
369
    return paxs
×
370
end
371
axes(a::NonReshapedReinterpretArray{T,0}) where {T} = ()
×
372

373
has_offset_axes(a::ReinterpretArray) = has_offset_axes(a.parent)
×
374

375
elsize(::Type{<:ReinterpretArray{T}}) where {T} = sizeof(T)
4✔
376
cconvert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = cconvert(Ptr{S}, a.parent)
1,210✔
377
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent))
×
378

379
@propagate_inbounds function getindex(a::NonReshapedReinterpretArray{T,0,S}) where {T,S}
×
380
    if isprimitivetype(T) && isprimitivetype(S)
×
381
        reinterpret(T, a.parent[])
×
382
    else
383
        a[firstindex(a)]
×
384
    end
385
end
386

387
check_ptr_indexable(a::ReinterpretArray, sz = elsize(a)) = check_ptr_indexable(parent(a), sz)
8✔
388
check_ptr_indexable(a::ReshapedArray, sz) = check_ptr_indexable(parent(a), sz)
×
389
check_ptr_indexable(a::FastContiguousSubArray, sz) = check_ptr_indexable(parent(a), sz)
×
390
check_ptr_indexable(a::Array, sz) = sizeof(eltype(a)) !== sz
×
391
check_ptr_indexable(a::Memory, sz) = true
×
392
check_ptr_indexable(a::AbstractArray, sz) = false
×
393

394
@propagate_inbounds getindex(a::ReinterpretArray) = a[firstindex(a)]
×
395

396
@propagate_inbounds isassigned(a::ReinterpretArray, inds::Integer...) = checkbounds(Bool, a, inds...) && (check_ptr_indexable(a) || _isassigned_ra(a, inds...))
×
397
@propagate_inbounds isassigned(a::ReinterpretArray, inds::SCartesianIndex2) = isassigned(a.parent, inds.j)
×
398
@propagate_inbounds _isassigned_ra(a::ReinterpretArray, inds...) = true # that is not entirely true, but computing exactly which indexes will be accessed in the parent requires a lot of duplication from the _getindex_ra code
×
399

400
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, inds::Vararg{Int, N}) where {T,N,S}
401
    check_readable(a)
4✔
402
    check_ptr_indexable(a) && return _getindex_ptr(a, inds...)
1,210✔
403
    _getindex_ra(a, inds[1], tail(inds))
×
404
end
405

406
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, i::Int) where {T,N,S}
×
407
    check_readable(a)
×
408
    check_ptr_indexable(a) && return _getindex_ptr(a, i)
×
409
    if isa(IndexStyle(a), IndexLinear)
×
410
        return _getindex_ra(a, i, ())
×
411
    end
412
    # Convert to full indices here, to avoid needing multiple conversions in
413
    # the loop in _getindex_ra
414
    inds = _to_subscript_indices(a, i)
×
415
    isempty(inds) ? _getindex_ra(a, 1, ()) : _getindex_ra(a, inds[1], tail(inds))
×
416
end
417

418
@propagate_inbounds function getindex(a::ReshapedReinterpretArray{T,N,S}, ind::SCartesianIndex2) where {T,N,S}
×
419
    check_readable(a)
×
420
    s = Ref{S}(a.parent[ind.j])
×
421
    tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
×
422
    GC.@preserve s return unsafe_load(tptr, ind.i)
×
423
end
424

425
@inline function _getindex_ptr(a::ReinterpretArray{T}, inds...) where {T}
426
    @boundscheck checkbounds(a, inds...)
1,210✔
427
    li = _to_linear_index(a, inds...)
4✔
428
    ap = cconvert(Ptr{T}, a)
1,210✔
429
    p = unsafe_convert(Ptr{T}, ap) + sizeof(T) * (li - 1)
1,210✔
430
    GC.@preserve ap return unsafe_load(p)
1,210✔
431
end
432

433
@propagate_inbounds function _getindex_ra(a::NonReshapedReinterpretArray{T,N,S}, i1::Int, tailinds::TT) where {T,N,S,TT}
×
434
    # Make sure to match the scalar reinterpret if that is applicable
435
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
×
436
        if issingletontype(T) # singleton types
×
437
            @boundscheck checkbounds(a, i1, tailinds...)
×
438
            return T.instance
×
439
        end
440
        return reinterpret(T, a.parent[i1, tailinds...])
×
441
    else
442
        @boundscheck checkbounds(a, i1, tailinds...)
×
443
        ind_start, sidx = divrem((i1-1)*sizeof(T), sizeof(S))
×
444
        # Optimizations that avoid branches
445
        if sizeof(T) % sizeof(S) == 0
×
446
            # T is bigger than S and contains an integer number of them
447
            n = sizeof(T) ÷ sizeof(S)
×
448
            t = Ref{T}()
×
449
            GC.@preserve t begin
×
450
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
×
451
                for i = 1:n
×
452
                     s = a.parent[ind_start + i, tailinds...]
×
453
                     unsafe_store!(sptr, s, i)
×
454
                end
×
455
            end
456
            return t[]
×
457
        elseif sizeof(S) % sizeof(T) == 0
×
458
            # S is bigger than T and contains an integer number of them
459
            s = Ref{S}(a.parent[ind_start + 1, tailinds...])
×
460
            GC.@preserve s begin
×
461
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
×
462
                return unsafe_load(tptr + sidx)
×
463
            end
464
        else
465
            i = 1
×
466
            nbytes_copied = 0
×
467
            # This is a bit complicated to deal with partial elements
468
            # at both the start and the end. LLVM will fold as appropriate,
469
            # once it knows the data layout
470
            s = Ref{S}()
×
471
            t = Ref{T}()
×
472
            GC.@preserve s t begin
×
473
                sptr = Ptr{S}(unsafe_convert(Ref{S}, s))
×
474
                tptr = Ptr{T}(unsafe_convert(Ref{T}, t))
×
475
                while nbytes_copied < sizeof(T)
×
476
                    s[] = a.parent[ind_start + i, tailinds...]
×
477
                    nb = min(sizeof(S) - sidx, sizeof(T)-nbytes_copied)
×
478
                    memcpy(tptr + nbytes_copied, sptr + sidx, nb)
×
479
                    nbytes_copied += nb
×
480
                    sidx = 0
×
481
                    i += 1
×
482
                end
×
483
            end
484
            return t[]
×
485
        end
486
    end
487
end
488

489
@propagate_inbounds function _getindex_ra(a::ReshapedReinterpretArray{T,N,S}, i1::Int, tailinds::TT) where {T,N,S,TT}
×
490
    # Make sure to match the scalar reinterpret if that is applicable
491
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
×
492
        if issingletontype(T) # singleton types
×
493
            @boundscheck checkbounds(a, i1, tailinds...)
×
494
            return T.instance
×
495
        end
496
        return reinterpret(T, a.parent[i1, tailinds...])
×
497
    end
498
    @boundscheck checkbounds(a, i1, tailinds...)
×
499
    if sizeof(T) >= sizeof(S)
×
500
        t = Ref{T}()
×
501
        GC.@preserve t begin
×
502
            sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
×
503
            if sizeof(T) > sizeof(S)
×
504
                # Extra dimension in the parent array
505
                n = sizeof(T) ÷ sizeof(S)
×
506
                if isempty(tailinds) && IndexStyle(a.parent) === IndexLinear()
×
507
                    offset = n * (i1 - firstindex(a))
×
508
                    for i = 1:n
×
509
                        s = a.parent[i + offset]
×
510
                        unsafe_store!(sptr, s, i)
×
511
                    end
×
512
                else
513
                    for i = 1:n
×
514
                        s = a.parent[i, i1, tailinds...]
×
515
                        unsafe_store!(sptr, s, i)
×
516
                    end
×
517
                end
518
            else
519
                # No extra dimension
520
                s = a.parent[i1, tailinds...]
×
521
                unsafe_store!(sptr, s)
×
522
            end
523
        end
524
        return t[]
×
525
    end
526
    # S is bigger than T and contains an integer number of them
527
    # n = sizeof(S) ÷ sizeof(T)
528
    s = Ref{S}()
×
529
    GC.@preserve s begin
×
530
        tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
×
531
        s[] = a.parent[tailinds...]
×
532
        return unsafe_load(tptr, i1)
×
533
    end
534
end
535

536
@propagate_inbounds function setindex!(a::NonReshapedReinterpretArray{T,0,S}, v) where {T,S}
×
537
    if isprimitivetype(S) && isprimitivetype(T)
×
538
        a.parent[] = reinterpret(S, v)
×
539
        return a
×
540
    end
541
    setindex!(a, v, firstindex(a))
×
542
end
543

544
@propagate_inbounds setindex!(a::ReinterpretArray, v) = setindex!(a, v, firstindex(a))
×
545

546
@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, inds::Vararg{Int, N}) where {T,N,S}
×
547
    check_writable(a)
×
548
    check_ptr_indexable(a) && return _setindex_ptr!(a, v, inds...)
×
549
    _setindex_ra!(a, v, inds[1], tail(inds))
×
550
end
551

552
@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, i::Int) where {T,N,S}
×
553
    check_writable(a)
×
554
    check_ptr_indexable(a) && return _setindex_ptr!(a, v, i)
×
555
    if isa(IndexStyle(a), IndexLinear)
×
556
        return _setindex_ra!(a, v, i, ())
×
557
    end
558
    inds = _to_subscript_indices(a, i)
×
559
    _setindex_ra!(a, v, inds[1], tail(inds))
×
560
end
561

562
@propagate_inbounds function setindex!(a::ReshapedReinterpretArray{T,N,S}, v, ind::SCartesianIndex2) where {T,N,S}
×
563
    check_writable(a)
×
564
    v = convert(T, v)::T
×
565
    s = Ref{S}(a.parent[ind.j])
×
566
    GC.@preserve s begin
×
567
        tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
×
568
        unsafe_store!(tptr, v, ind.i)
×
569
    end
570
    a.parent[ind.j] = s[]
×
571
    return a
×
572
end
573

574
@inline function _setindex_ptr!(a::ReinterpretArray{T}, v, inds...) where {T}
×
575
    @boundscheck checkbounds(a, inds...)
×
576
    li = _to_linear_index(a, inds...)
×
577
    ap = cconvert(Ptr{T}, a)
×
578
    p = unsafe_convert(Ptr{T}, ap) + sizeof(T) * (li - 1)
×
579
    GC.@preserve ap unsafe_store!(p, v)
×
580
    return a
×
581
end
582

583
@propagate_inbounds function _setindex_ra!(a::NonReshapedReinterpretArray{T,N,S}, v, i1::Int, tailinds::TT) where {T,N,S,TT}
×
584
    v = convert(T, v)::T
×
585
    # Make sure to match the scalar reinterpret if that is applicable
586
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
×
587
        if issingletontype(T) # singleton types
×
588
            @boundscheck checkbounds(a, i1, tailinds...)
×
589
            # setindex! is a noop except for the index check
590
        else
591
            setindex!(a.parent, reinterpret(S, v), i1, tailinds...)
×
592
        end
593
    else
594
        @boundscheck checkbounds(a, i1, tailinds...)
×
595
        ind_start, sidx = divrem((i1-1)*sizeof(T), sizeof(S))
×
596
        # Optimizations that avoid branches
597
        if sizeof(T) % sizeof(S) == 0
×
598
            # T is bigger than S and contains an integer number of them
599
            t = Ref{T}(v)
×
600
            GC.@preserve t begin
×
601
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
×
602
                n = sizeof(T) ÷ sizeof(S)
×
603
                for i = 1:n
×
604
                    s = unsafe_load(sptr, i)
×
605
                    a.parent[ind_start + i, tailinds...] = s
×
606
                end
×
607
            end
608
        elseif sizeof(S) % sizeof(T) == 0
×
609
            # S is bigger than T and contains an integer number of them
610
            s = Ref{S}(a.parent[ind_start + 1, tailinds...])
×
611
            GC.@preserve s begin
×
612
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
×
613
                unsafe_store!(tptr + sidx, v)
×
614
                a.parent[ind_start + 1, tailinds...] = s[]
×
615
            end
616
        else
617
            t = Ref{T}(v)
×
618
            s = Ref{S}()
×
619
            GC.@preserve t s begin
×
620
                tptr = Ptr{UInt8}(unsafe_convert(Ref{T}, t))
×
621
                sptr = Ptr{UInt8}(unsafe_convert(Ref{S}, s))
×
622
                nbytes_copied = 0
×
623
                i = 1
×
624
                # Deal with any partial elements at the start. We'll have to copy in the
625
                # element from the original array and overwrite the relevant parts
626
                if sidx != 0
×
627
                    s[] = a.parent[ind_start + i, tailinds...]
×
628
                    nb = min((sizeof(S) - sidx) % UInt, sizeof(T) % UInt)
×
629
                    memcpy(sptr + sidx, tptr, nb)
×
630
                    nbytes_copied += nb
×
631
                    a.parent[ind_start + i, tailinds...] = s[]
×
632
                    i += 1
×
633
                    sidx = 0
×
634
                end
635
                # Deal with the main body of elements
636
                while nbytes_copied < sizeof(T) && (sizeof(T) - nbytes_copied) > sizeof(S)
×
637
                    nb = min(sizeof(S), sizeof(T) - nbytes_copied)
×
638
                    memcpy(sptr, tptr + nbytes_copied, nb)
×
639
                    nbytes_copied += nb
×
640
                    a.parent[ind_start + i, tailinds...] = s[]
×
641
                    i += 1
×
642
                end
×
643
                # Deal with trailing partial elements
644
                if nbytes_copied < sizeof(T)
×
645
                    s[] = a.parent[ind_start + i, tailinds...]
×
646
                    nb = min(sizeof(S), sizeof(T) - nbytes_copied)
×
647
                    memcpy(sptr, tptr + nbytes_copied, nb)
×
648
                    a.parent[ind_start + i, tailinds...] = s[]
×
649
                end
650
            end
651
        end
652
    end
653
    return a
×
654
end
655

656
@propagate_inbounds function _setindex_ra!(a::ReshapedReinterpretArray{T,N,S}, v, i1::Int, tailinds::TT) where {T,N,S,TT}
×
657
    v = convert(T, v)::T
×
658
    # Make sure to match the scalar reinterpret if that is applicable
659
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
×
660
        if issingletontype(T) # singleton types
×
661
            @boundscheck checkbounds(a, i1, tailinds...)
×
662
            # setindex! is a noop except for the index check
663
        else
664
            setindex!(a.parent, reinterpret(S, v), i1, tailinds...)
×
665
        end
666
    end
667
    @boundscheck checkbounds(a, i1, tailinds...)
×
668
    if sizeof(T) >= sizeof(S)
×
669
        t = Ref{T}(v)
×
670
        GC.@preserve t begin
×
671
            sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
×
672
            if sizeof(T) > sizeof(S)
×
673
                # Extra dimension in the parent array
674
                n = sizeof(T) ÷ sizeof(S)
×
675
                if isempty(tailinds) && IndexStyle(a.parent) === IndexLinear()
×
676
                    offset = n * (i1 - firstindex(a))
×
677
                    for i = 1:n
×
678
                        s = unsafe_load(sptr, i)
×
679
                        a.parent[i + offset] = s
×
680
                    end
×
681
                else
682
                    for i = 1:n
×
683
                        s = unsafe_load(sptr, i)
×
684
                        a.parent[i, i1, tailinds...] = s
×
685
                    end
×
686
                end
687
            else # sizeof(T) == sizeof(S)
688
                # No extra dimension
689
                s = unsafe_load(sptr)
×
690
                a.parent[i1, tailinds...] = s
×
691
            end
692
        end
693
    else
694
        # S is bigger than T and contains an integer number of them
695
        s = Ref{S}()
×
696
        GC.@preserve s begin
×
697
            tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
×
698
            s[] = a.parent[tailinds...]
×
699
            unsafe_store!(tptr, v, i1)
×
700
            a.parent[tailinds...] = s[]
×
701
        end
702
    end
703
    return a
×
704
end
705

706
# Padding
707
struct Padding
708
    offset::Int # 0-indexed offset of the next valid byte; sizeof(T) indicates trailing padding
709
    size::Int   # bytes of padding before a valid byte
710
end
711
function intersect(p1::Padding, p2::Padding)
×
712
    start = max(p1.offset, p2.offset)
×
713
    stop = min(p1.offset + p1.size, p2.offset + p2.size)
×
714
    Padding(start, max(0, stop-start))
×
715
end
716

717
struct PaddingError <: Exception
718
    S::Type
719
    T::Type
720
end
721

722
function showerror(io::IO, p::PaddingError)
×
723
    print(io, "Padding of type $(p.S) is not compatible with type $(p.T).")
×
724
end
725

726
"""
727
    CyclePadding(padding, total_size)
728

729
Cycles an iterator of `Padding` structs, restarting the padding at `total_size`.
730
E.g. if `padding` is all the padding in a struct and `total_size` is the total
731
aligned size of that array, `CyclePadding` will correspond to the padding in an
732
infinite vector of such structs.
733
"""
734
struct CyclePadding{P}
735
    padding::P
736
    total_size::Int
737
end
738
eltype(::Type{<:CyclePadding}) = Padding
×
739
IteratorSize(::Type{<:CyclePadding}) = IsInfinite()
×
740
isempty(cp::CyclePadding) = isempty(cp.padding)
×
741
function iterate(cp::CyclePadding)
×
742
    y = iterate(cp.padding)
×
743
    y === nothing && return nothing
×
744
    y[1], (0, y[2])
×
745
end
746
function iterate(cp::CyclePadding, state::Tuple)
×
747
    y = iterate(cp.padding, tail(state)...)
×
748
    y === nothing && return iterate(cp, (state[1]+cp.total_size,))
×
749
    Padding(y[1].offset+state[1], y[1].size), (state[1], tail(y)...)
×
750
end
751

752
"""
753
    Compute the location of padding in an isbits datatype. Recursive over the fields of that type.
754
"""
755
@assume_effects :foldable function padding(T::DataType, baseoffset::Int = 0)
×
756
    pads = Padding[]
×
757
    last_end::Int = baseoffset
×
758
    for i = 1:fieldcount(T)
×
759
        offset = baseoffset + Int(fieldoffset(T, i))
×
760
        fT = fieldtype(T, i)
×
761
        append!(pads, padding(fT, offset))
×
762
        if offset != last_end
×
763
            push!(pads, Padding(offset, offset-last_end))
×
764
        end
765
        last_end = offset + sizeof(fT)
×
766
    end
×
767
    if 0 < last_end - baseoffset < sizeof(T)
×
768
        push!(pads, Padding(baseoffset + sizeof(T), sizeof(T) - last_end + baseoffset))
×
769
    end
770
    return Core.svec(pads...)
×
771
end
772

773
function CyclePadding(T::DataType)
×
774
    a, s = datatype_alignment(T), sizeof(T)
×
775
    as = s + (a - (s % a)) % a
×
776
    pad = padding(T)
×
777
    if s != as
×
778
        pad = Core.svec(pad..., Padding(s, as - s))
×
779
    end
780
    CyclePadding(pad, as)
×
781
end
782

783
@assume_effects :total function array_subpadding(S, T)
×
784
    lcm_size = lcm(sizeof(S), sizeof(T))
×
785
    s, t = CyclePadding(S), CyclePadding(T)
×
786
    checked_size = 0
×
787
    # use of Stateful harms inference and makes this vulnerable to invalidation
788
    (pad, tstate) = let
×
789
        it = iterate(t)
×
790
        it === nothing && return true
×
791
        it
×
792
    end
793
    (ps, sstate) = let
×
794
        it = iterate(s)
×
795
        it === nothing && return false
×
796
        it
×
797
    end
798
    while checked_size < lcm_size
×
799
        while true
×
800
            # See if there's corresponding padding in S
801
            ps.offset > pad.offset && return false
×
802
            intersect(ps, pad) == pad && break
×
803
            ps, sstate = iterate(s, sstate)
×
804
        end
×
805
        checked_size = pad.offset + pad.size
×
806
        pad, tstate = iterate(t, tstate)
×
807
    end
×
808
    return true
×
809
end
810

811
@assume_effects :foldable function struct_subpadding(::Type{Out}, ::Type{In}) where {Out, In}
×
812
    padding(Out) == padding(In)
×
813
end
814

815
@assume_effects :foldable function packedsize(::Type{T}) where T
×
816
    pads = padding(T)
×
817
    return sizeof(T) - sum((p.size for p ∈ pads), init = 0)
×
818
end
819

820
@assume_effects :foldable ispacked(::Type{T}) where T = isempty(padding(T))
×
821

822
function _copytopacked!(ptr_out::Ptr{Out}, ptr_in::Ptr{In}) where {Out, In}
×
823
    writeoffset = 0
×
824
    for i ∈ 1:fieldcount(In)
×
825
        readoffset = fieldoffset(In, i)
×
826
        fT = fieldtype(In, i)
×
827
        if ispacked(fT)
×
828
            readsize = sizeof(fT)
×
829
            memcpy(ptr_out + writeoffset, ptr_in + readoffset, readsize)
×
830
            writeoffset += readsize
×
831
        else # nested padded type
832
            _copytopacked!(ptr_out + writeoffset, Ptr{fT}(ptr_in + readoffset))
×
833
            writeoffset += packedsize(fT)
×
834
        end
835
    end
×
836
end
837

838
function _copyfrompacked!(ptr_out::Ptr{Out}, ptr_in::Ptr{In}) where {Out, In}
×
839
    readoffset = 0
×
840
    for i ∈ 1:fieldcount(Out)
×
841
        writeoffset = fieldoffset(Out, i)
×
842
        fT = fieldtype(Out, i)
×
843
        if ispacked(fT)
×
844
            writesize = sizeof(fT)
×
845
            memcpy(ptr_out + writeoffset, ptr_in + readoffset, writesize)
×
846
            readoffset += writesize
×
847
        else # nested padded type
848
            _copyfrompacked!(Ptr{fT}(ptr_out + writeoffset), ptr_in + readoffset)
×
849
            readoffset += packedsize(fT)
×
850
        end
851
    end
×
852
end
853

854
@inline function _reinterpret(::Type{Out}, x::In) where {Out, In}
×
855
    # handle non-primitive types
856
    isbitstype(Out) || throw(ArgumentError("Target type for `reinterpret` must be isbits"))
×
857
    isbitstype(In) || throw(ArgumentError("Source type for `reinterpret` must be isbits"))
×
858
    inpackedsize = packedsize(In)
×
859
    outpackedsize = packedsize(Out)
×
860
    inpackedsize == outpackedsize ||
×
861
        throw(ArgumentError(LazyString("Packed sizes of types ", Out, " and ", In,
862
            " do not match; got ", outpackedsize, " and ", inpackedsize, ", respectively.")))
863
    in = Ref{In}(x)
×
864
    out = Ref{Out}()
×
865
    if struct_subpadding(Out, In)
×
866
        # if packed the same, just copy
867
        GC.@preserve in out begin
×
868
            ptr_in = unsafe_convert(Ptr{In}, in)
×
869
            ptr_out = unsafe_convert(Ptr{Out}, out)
×
870
            memcpy(ptr_out, ptr_in, sizeof(Out))
×
871
        end
872
        return out[]
×
873
    else
874
        # mismatched padding
875
        return _reinterpret_padding(Out, x)
×
876
    end
877
end
878

879
# If the code reaches this part, it needs to handle padding and is unlikely
880
# to compile to a noop. Therefore, we don't forcibly inline it.
881
function _reinterpret_padding(::Type{Out}, x::In) where {Out, In}
×
882
    inpackedsize = packedsize(In)
×
883
    in = Ref{In}(x)
×
884
    out = Ref{Out}()
×
885
    GC.@preserve in out begin
×
886
        ptr_in = unsafe_convert(Ptr{In}, in)
×
887
        ptr_out = unsafe_convert(Ptr{Out}, out)
×
888

889
        if fieldcount(In) > 0 && ispacked(Out)
×
890
            _copytopacked!(ptr_out, ptr_in)
×
891
        elseif fieldcount(Out) > 0 && ispacked(In)
×
892
            _copyfrompacked!(ptr_out, ptr_in)
×
893
        else
894
            packed = Ref{NTuple{inpackedsize, UInt8}}()
×
895
            GC.@preserve packed begin
×
896
                ptr_packed = unsafe_convert(Ptr{NTuple{inpackedsize, UInt8}}, packed)
×
897
                _copytopacked!(ptr_packed, ptr_in)
×
898
                _copyfrompacked!(ptr_out, ptr_packed)
×
899
            end
900
        end
901
    end
902
    return out[]
×
903
end
904

905

906
# Reductions with IndexSCartesian2
907

908
function _mapreduce(f::F, op::OP, style::IndexSCartesian2{K}, A::AbstractArrayOrBroadcasted) where {F,OP,K}
×
909
    inds = eachindex(style, A)
×
910
    n = size(inds)[2]
×
911
    if n == 0
×
912
        return mapreduce_empty_iter(f, op, A, IteratorEltype(A))
×
913
    else
914
        return mapreduce_impl(f, op, A, first(inds), last(inds))
×
915
    end
916
end
917

918
@noinline function mapreduce_impl(f::F, op::OP, A::AbstractArrayOrBroadcasted,
×
919
                                  ifirst::SCI, ilast::SCI, blksize::Int) where {F,OP,SCI<:SCartesianIndex2{K}} where K
920
    if ilast.j - ifirst.j < blksize
×
921
        # sequential portion
922
        @inbounds a1 = A[ifirst]
×
923
        @inbounds a2 = A[SCI(2,ifirst.j)]
×
924
        v = op(f(a1), f(a2))
×
925
        @simd for i = ifirst.i + 2 : K
×
926
            @inbounds ai = A[SCI(i,ifirst.j)]
×
927
            v = op(v, f(ai))
×
928
        end
×
929
        # Remaining columns
930
        for j = ifirst.j+1 : ilast.j
×
931
            @simd for i = 1:K
×
932
                @inbounds ai = A[SCI(i,j)]
×
933
                v = op(v, f(ai))
×
934
            end
×
935
        end
×
936
        return v
×
937
    else
938
        # pairwise portion
939
        jmid = ifirst.j + (ilast.j - ifirst.j) >> 1
×
940
        v1 = mapreduce_impl(f, op, A, ifirst, SCI(K,jmid), blksize)
×
941
        v2 = mapreduce_impl(f, op, A, SCI(1,jmid+1), ilast, blksize)
×
942
        return op(v1, v2)
×
943
    end
944
end
945

946
mapreduce_impl(f::F, op::OP, A::AbstractArrayOrBroadcasted, ifirst::SCartesianIndex2, ilast::SCartesianIndex2) where {F,OP} =
×
947
    mapreduce_impl(f, op, A, ifirst, ilast, pairwise_blocksize(f, op))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc