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

JuliaLang / julia / #37476

pending completion
#37476

push

local

web-flow
Ensure accurate invalidation logging data (#48982)

* Ensure accurate invalidation logging data

This modifies #48841 to restore the required logging data.
By collecting at least one additional match, we retain the possibility
of identifying at least one trigger of invalidation.

* Bump number of invalidation causes

* Update src/staticdata_utils.c

Co-authored-by: Jameson Nash <vtjnash@gmail.com>

---------

Co-authored-by: Jameson Nash <vtjnash@gmail.com>

71283 of 82556 relevant lines covered (86.35%)

34551491.63 hits per line

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

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

3
abstract type AbstractCartesianIndex{N} end # This is a hacky forward declaration for CartesianIndex
4
const ViewIndex = Union{Real, AbstractArray}
5
const ScalarIndex = Real
6

7
"""
8
    SubArray{T,N,P,I,L} <: AbstractArray{T,N}
9

10
`N`-dimensional view into a parent array (of type `P`) with an element type `T`, restricted by a tuple of indices (of type `I`). `L` is true for types that support fast linear indexing, and `false` otherwise.
11

12
Construct `SubArray`s using the [`view`](@ref) function.
13
"""
14
struct SubArray{T,N,P,I,L} <: AbstractArray{T,N}
15
    parent::P
16
    indices::I
17
    offset1::Int       # for linear indexing and pointer, only valid when L==true
18
    stride1::Int       # used only for linear indexing
19
    function SubArray{T,N,P,I,L}(parent, indices, offset1, stride1) where {T,N,P,I,L}
1,229,743✔
20
        @inline
646,368✔
21
        check_parent_index_match(parent, indices)
646,368✔
22
        new(parent, indices, offset1, stride1)
4,062,048✔
23
    end
24
end
25
# Compute the linear indexability of the indices, and combine it with the linear indexing of the parent
26
function SubArray(parent::AbstractArray, indices::Tuple)
1,229,190✔
27
    @inline
1,229,190✔
28
    SubArray(IndexStyle(viewindexing(indices), IndexStyle(parent)), parent, ensure_indexable(indices), index_dimsum(indices...))
4,061,742✔
29
end
30
function SubArray(::IndexCartesian, parent::P, indices::I, ::NTuple{N,Any}) where {P,I,N}
247,892✔
31
    @inline
247,885✔
32
    SubArray{eltype(P), N, P, I, false}(parent, indices, 0, 0)
247,949✔
33
end
34
function SubArray(::IndexLinear, parent::P, indices::I, ::NTuple{N,Any}) where {P,I,N}
981,298✔
35
    @inline
981,298✔
36
    # Compute the stride and offset
37
    stride1 = compute_stride1(parent, indices)
981,388✔
38
    SubArray{eltype(P), N, P, I, true}(parent, indices, compute_offset1(parent, stride1, indices), stride1)
3,813,793✔
39
end
40

41
check_parent_index_match(parent, indices) = check_parent_index_match(parent, index_ndims(indices...))
646,361✔
42
check_parent_index_match(parent::AbstractArray{T,N}, ::NTuple{N, Bool}) where {T,N} = nothing
531,381✔
43
check_parent_index_match(parent, ::NTuple{N, Bool}) where {N} =
×
44
    throw(ArgumentError("number of indices ($N) must match the parent dimensionality ($(ndims(parent)))"))
45

46
# This computes the linear indexing compatibility for a given tuple of indices
47
viewindexing(I::Tuple{}) = IndexLinear()
50✔
48
# Leading scalar indices simply increase the stride
49
viewindexing(I::Tuple{ScalarIndex, Vararg{Any}}) = (@inline; viewindexing(tail(I)))
7,430✔
50
# Slices may begin a section which may be followed by any number of Slices
51
viewindexing(I::Tuple{Slice, Slice, Vararg{Any}}) = (@inline; viewindexing(tail(I)))
2,114✔
52
# A UnitRange can follow Slices, but only if all other indices are scalar
53
viewindexing(I::Tuple{Slice, AbstractUnitRange, Vararg{ScalarIndex}}) = IndexLinear()
440✔
54
viewindexing(I::Tuple{Slice, Slice, Vararg{ScalarIndex}}) = IndexLinear() # disambiguate
1,362✔
55
# In general, ranges are only fast if all other indices are scalar
56
viewindexing(I::Tuple{AbstractRange, Vararg{ScalarIndex}}) = IndexLinear()
180,705✔
57
# All other index combinations are slow
58
viewindexing(I::Tuple{Vararg{Any}}) = IndexCartesian()
×
59
# Of course, all other array types are slow
60
viewindexing(I::Tuple{AbstractArray, Vararg{Any}}) = IndexCartesian()
19,487✔
61

62
# Simple utilities
63
size(V::SubArray) = (@inline; map(length, axes(V)))
14,254,269✔
64

65
similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
6,748✔
66

67
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
2✔
68
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)
261,776✔
69

70
function Base.copy(V::SubArray)
1,074✔
71
    v = V.parent[V.indices...]
1,266✔
72
    ndims(V) == 0 || return v
1,074✔
73
    x = similar(V) # ensure proper type of x
×
74
    x[] = v
×
75
    return x
×
76
end
77

78
parent(V::SubArray) = V.parent
186,536✔
79
parentindices(V::SubArray) = V.indices
183,453✔
80

81
"""
82
    parentindices(A)
83

84
Return the indices in the [`parent`](@ref) which correspond to the array view `A`.
85

86
# Examples
87
```jldoctest
88
julia> A = [1 2; 3 4];
89

90
julia> V = view(A, 1, :)
91
2-element view(::Matrix{Int64}, 1, :) with eltype Int64:
92
 1
93
 2
94

95
julia> parentindices(V)
96
(1, Base.Slice(Base.OneTo(2)))
97
```
98
"""
99
parentindices(a::AbstractArray) = map(oneto, size(a))
×
100

101
## Aliasing detection
102
dataids(A::SubArray) = (dataids(A.parent)..., _splatmap(dataids, A.indices)...)
2,641,506✔
103
_splatmap(f, ::Tuple{}) = ()
×
104
_splatmap(f, t::Tuple) = (f(t[1])..., _splatmap(f, tail(t))...)
16,154✔
105
unaliascopy(A::SubArray) = typeof(A)(unaliascopy(A.parent), map(unaliascopy, A.indices), A.offset1, A.stride1)
4✔
106

107
# When the parent is an Array we can trim the size down a bit. In the future this
108
# could possibly be extended to any mutable array.
109
function unaliascopy(V::SubArray{T,N,A,I,LD}) where {T,N,A<:Array,I<:Tuple{Vararg{Union{Real,AbstractRange,Array}}},LD}
558✔
110
    dest = Array{T}(undef, index_lengths(V.indices...))
558✔
111
    copyto!(dest, V)
558✔
112
    SubArray{T,N,A,I,LD}(dest, map(_trimmedindex, V.indices), 0, Int(LD))
558✔
113
end
114
# Transform indices to be "dense"
115
_trimmedindex(i::Real) = oftype(i, 1)
540✔
116
_trimmedindex(i::AbstractUnitRange) = oftype(i, oneto(length(i)))
559✔
117
_trimmedindex(i::AbstractArray) = oftype(i, reshape(eachindex(IndexLinear(), i), axes(i)))
×
118

119
## SubArray creation
120
# We always assume that the dimensionality of the parent matches the number of
121
# indices that end up getting passed to it, so we store the parent as a
122
# ReshapedArray view if necessary. The trouble is that arrays of `CartesianIndex`
123
# can make the number of effective indices not equal to length(I).
124
_maybe_reshape_parent(A::AbstractArray, ::NTuple{1, Bool}) = reshape(A, Val(1))
1,116✔
125
_maybe_reshape_parent(A::AbstractArray{<:Any,1}, ::NTuple{1, Bool}) = reshape(A, Val(1))
306,775✔
126
_maybe_reshape_parent(A::AbstractArray{<:Any,N}, ::NTuple{N, Bool}) where {N} = A
202,818✔
127
_maybe_reshape_parent(A::AbstractArray, ::NTuple{N, Bool}) where {N} = reshape(A, Val(N))
338✔
128
"""
129
    view(A, inds...)
130

131
Like [`getindex`](@ref), but returns a lightweight array that lazily references
132
(or is effectively a _view_ into) the parent array `A` at the given index or indices
133
`inds` instead of eagerly extracting elements or constructing a copied subset.
134
Calling [`getindex`](@ref) or [`setindex!`](@ref) on the returned value
135
(often a [`SubArray`](@ref)) computes the indices to access or modify the
136
parent array on the fly.  The behavior is undefined if the shape of the parent array is
137
changed after `view` is called because there is no bound check for the parent array; e.g.,
138
it may cause a segmentation fault.
139

140
Some immutable parent arrays (like ranges) may choose to simply
141
recompute a new array in some circumstances instead of returning
142
a `SubArray` if doing so is efficient and provides compatible semantics.
143

144
!!! compat "Julia 1.6"
145
    In Julia 1.6 or later, `view` can be called on an `AbstractString`, returning a
146
    `SubString`.
147

148
# Examples
149
```jldoctest
150
julia> A = [1 2; 3 4]
151
2×2 Matrix{Int64}:
152
 1  2
153
 3  4
154

155
julia> b = view(A, :, 1)
156
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
157
 1
158
 3
159

160
julia> fill!(b, 0)
161
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
162
 0
163
 0
164

165
julia> A # Note A has changed even though we modified b
166
2×2 Matrix{Int64}:
167
 0  2
168
 0  4
169

170
julia> view(2:5, 2:3) # returns a range as type is immutable
171
3:4
172
```
173
"""
174
function view(A::AbstractArray{<:Any,N}, I::Vararg{Any,M}) where {N,M}
3,710,652✔
175
    @inline
1,229,355✔
176
    J = map(i->unalias(A,i), to_indices(A, I))
2,111,026✔
177
    @boundscheck checkbounds(A, J...)
4,063,754✔
178
    if length(J) > ndims(A) && J[N+1:end] isa Tuple{Vararg{Int}}
1,229,337✔
179
        # view([1,2,3], :, 1) does not need to reshape
180
        return unsafe_view(A, J[1:N]...)
20,044✔
181
    end
182
    unsafe_view(_maybe_reshape_parent(A, index_ndims(J...)), J...)
4,043,716✔
183
end
184

185
# Ranges implement getindex to return recomputed ranges; use that for views, too (when possible)
186
function view(r1::OneTo, r2::OneTo)
1✔
187
    @_propagate_inbounds_meta
1✔
188
    getindex(r1, r2)
1✔
189
end
190
function view(r1::AbstractUnitRange, r2::AbstractUnitRange{<:Integer})
284✔
191
    @_propagate_inbounds_meta
284✔
192
    getindex(r1, r2)
284✔
193
end
194
function view(r1::AbstractUnitRange, r2::StepRange{<:Integer})
1✔
195
    @_propagate_inbounds_meta
1✔
196
    getindex(r1, r2)
1✔
197
end
198
function view(r1::StepRange, r2::AbstractRange{<:Integer})
1✔
199
    @_propagate_inbounds_meta
1✔
200
    getindex(r1, r2)
1✔
201
end
202
function view(r1::StepRangeLen, r2::OrdinalRange{<:Integer})
×
203
    @_propagate_inbounds_meta
×
204
    getindex(r1, r2)
×
205
end
206
function view(r1::LinRange, r2::OrdinalRange{<:Integer})
×
207
    @_propagate_inbounds_meta
×
208
    getindex(r1, r2)
×
209
end
210

211
# getindex(r::AbstractRange, ::Colon) returns a copy of the range, and we may do the same for a view
212
function view(r1::AbstractRange, c::Colon)
2✔
213
    @_propagate_inbounds_meta
2✔
214
    getindex(r1, c)
2✔
215
end
216

217
function unsafe_view(A::AbstractArray, I::Vararg{ViewIndex,N}) where {N}
1,099,713✔
218
    @inline
1,099,564✔
219
    SubArray(A, I)
3,914,546✔
220
end
221
# When we take the view of a view, it's often possible to "reindex" the parent
222
# view's indices such that we can "pop" the parent view and keep just one layer
223
# of indirection. But we can't always do this because arrays of `CartesianIndex`
224
# might span multiple parent indices, making the reindex calculation very hard.
225
# So we use _maybe_reindex to figure out if there are any arrays of
226
# `CartesianIndex`, and if so, we punt and keep two layers of indirection.
227
unsafe_view(V::SubArray, I::Vararg{ViewIndex,N}) where {N} =
129,625✔
228
    (@inline; _maybe_reindex(V, I))
147,267✔
229
_maybe_reindex(V, I) = (@inline; _maybe_reindex(V, I, I))
147,267✔
230
_maybe_reindex(V, I, ::Tuple{AbstractArray{<:AbstractCartesianIndex}, Vararg{Any}}) =
17✔
231
    (@inline; SubArray(V, I))
17✔
232
# But allow arrays of CartesianIndex{1}; they behave just like arrays of Ints
233
_maybe_reindex(V, I, A::Tuple{AbstractArray{<:AbstractCartesianIndex{1}}, Vararg{Any}}) =
×
234
    (@inline; _maybe_reindex(V, I, tail(A)))
×
235
_maybe_reindex(V, I, A::Tuple{Any, Vararg{Any}}) = (@inline; _maybe_reindex(V, I, tail(A)))
154,098✔
236
function _maybe_reindex(V, I, ::Tuple{})
129,608✔
237
    @inline
129,608✔
238
    @inbounds idxs = to_indices(V.parent, reindex(V.indices, I))
147,250✔
239
    SubArray(V.parent, idxs)
147,178✔
240
end
241

242
## Re-indexing is the heart of a view, transforming A[i, j][x, y] to A[i[x], j[y]]
243
#
244
# Recursively look through the heads of the parent- and sub-indices, considering
245
# the following cases:
246
# * Parent index is array  -> re-index that with one or more sub-indices (one per dimension)
247
# * Parent index is Colon  -> just use the sub-index as provided
248
# * Parent index is scalar -> that dimension was dropped, so skip the sub-index and use the index as is
249

250
AbstractZeroDimArray{T} = AbstractArray{T, 0}
251

252
reindex(::Tuple{}, ::Tuple{}) = ()
×
253

254
# Skip dropped scalars, so simply peel them off the parent indices and continue
255
reindex(idxs::Tuple{ScalarIndex, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) =
2,274,556✔
256
    (@_propagate_inbounds_meta; (idxs[1], reindex(tail(idxs), subidxs)...))
2,274,556✔
257

258
# Slices simply pass their subindices straight through
259
reindex(idxs::Tuple{Slice, Vararg{Any}}, subidxs::Tuple{Any, Vararg{Any}}) =
6,356,213✔
260
    (@_propagate_inbounds_meta; (subidxs[1], reindex(tail(idxs), tail(subidxs))...))
6,356,227✔
261

262
# Re-index into parent vectors with one subindex
263
reindex(idxs::Tuple{AbstractVector, Vararg{Any}}, subidxs::Tuple{Any, Vararg{Any}}) =
15,418,485✔
264
    (@_propagate_inbounds_meta; (idxs[1][subidxs[1]], reindex(tail(idxs), tail(subidxs))...))
15,436,844✔
265

266
# Parent matrices are re-indexed with two sub-indices
267
reindex(idxs::Tuple{AbstractMatrix, Vararg{Any}}, subidxs::Tuple{Any, Any, Vararg{Any}}) =
4,588✔
268
    (@_propagate_inbounds_meta; (idxs[1][subidxs[1], subidxs[2]], reindex(tail(idxs), tail(tail(subidxs)))...))
4,594✔
269

270
# In general, we index N-dimensional parent arrays with N indices
271
@generated function reindex(idxs::Tuple{AbstractArray{T,N}, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) where {T,N}
×
272
    if length(subidxs.parameters) >= N
×
273
        subs = [:(subidxs[$d]) for d in 1:N]
×
274
        tail = [:(subidxs[$d]) for d in N+1:length(subidxs.parameters)]
×
275
        :(@_propagate_inbounds_meta; (idxs[1][$(subs...)], reindex(tail(idxs), ($(tail...),))...))
×
276
    else
277
        :(throw(ArgumentError("cannot re-index SubArray with fewer indices than dimensions\nThis should not occur; please submit a bug report.")))
×
278
    end
279
end
280

281
# In general, we simply re-index the parent indices by the provided ones
282
SlowSubArray{T,N,P,I} = SubArray{T,N,P,I,false}
283
function getindex(V::SubArray{T,N}, I::Vararg{Int,N}) where {T,N}
4,307,065✔
284
    @inline
4,307,065✔
285
    @boundscheck checkbounds(V, I...)
4,307,074✔
286
    @inbounds r = V.parent[reindex(V.indices, I)...]
4,624,724✔
287
    r
4,307,040✔
288
end
289

290
# But SubArrays with fast linear indexing pre-compute a stride and offset
291
FastSubArray{T,N,P,I} = SubArray{T,N,P,I,true}
292
function getindex(V::FastSubArray, i::Int)
352✔
293
    @inline
352✔
294
    @boundscheck checkbounds(V, i)
352✔
295
    @inbounds r = V.parent[V.offset1 + V.stride1*i]
352✔
296
    r
352✔
297
end
298
# We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange,
299
# or if all the indices are scalars, i.e. the view is for a single value only
300
FastContiguousSubArray{T,N,P,I<:Union{Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}},
301
                                      Tuple{Vararg{ScalarIndex}}}} = SubArray{T,N,P,I,true}
302
function getindex(V::FastContiguousSubArray, i::Int)
7,968✔
303
    @inline
7,968✔
304
    @boundscheck checkbounds(V, i)
7,970✔
305
    @inbounds r = V.parent[V.offset1 + i]
7,966✔
306
    r
7,966✔
307
end
308
# For vector views with linear indexing, we disambiguate to favor the stride/offset
309
# computation as that'll generally be faster than (or just as fast as) re-indexing into a range.
310
function getindex(V::FastSubArray{<:Any, 1}, i::Int)
190,641✔
311
    @inline
187,094✔
312
    @boundscheck checkbounds(V, i)
216,438✔
313
    @inbounds r = V.parent[V.offset1 + V.stride1*i]
216,432✔
314
    r
216,432✔
315
end
316
function getindex(V::FastContiguousSubArray{<:Any, 1}, i::Int)
22,000,392✔
317
    @inline
21,178,953✔
318
    @boundscheck checkbounds(V, i)
88,672,505✔
319
    @inbounds r = V.parent[V.offset1 + i]
88,672,499✔
320
    r
88,672,499✔
321
end
322

323
# Indexed assignment follows the same pattern as `getindex` above
324
function setindex!(V::SubArray{T,N}, x, I::Vararg{Int,N}) where {T,N}
8,406,901✔
325
    @inline
8,406,181✔
326
    @boundscheck checkbounds(V, I...)
8,406,911✔
327
    @inbounds V.parent[reindex(V.indices, I)...] = x
8,407,010✔
328
    V
8,406,897✔
329
end
330
function setindex!(V::FastSubArray, x, i::Int)
1,000,628✔
331
    @inline
1,000,628✔
332
    @boundscheck checkbounds(V, i)
1,000,628✔
333
    @inbounds V.parent[V.offset1 + V.stride1*i] = x
1,000,628✔
334
    V
1,000,628✔
335
end
336
function setindex!(V::FastContiguousSubArray, x, i::Int)
21,477✔
337
    @inline
21,477✔
338
    @boundscheck checkbounds(V, i)
21,477✔
339
    @inbounds V.parent[V.offset1 + i] = x
21,477✔
340
    V
21,477✔
341
end
342
function setindex!(V::FastSubArray{<:Any, 1}, x, i::Int)
30,336✔
343
    @inline
30,247✔
344
    @boundscheck checkbounds(V, i)
31,912✔
345
    @inbounds V.parent[V.offset1 + V.stride1*i] = x
31,912✔
346
    V
31,912✔
347
end
348
function setindex!(V::FastContiguousSubArray{<:Any, 1}, x, i::Int)
54,512,315✔
349
    @inline
54,511,853✔
350
    @boundscheck checkbounds(V, i)
85,755,308✔
351
    @inbounds V.parent[V.offset1 + i] = x
85,755,308✔
352
    V
85,755,308✔
353
end
354

355
IndexStyle(::Type{<:FastSubArray}) = IndexLinear()
31,419✔
356
IndexStyle(::Type{<:SubArray}) = IndexCartesian()
8,557,058✔
357

358
# Strides are the distance in memory between adjacent elements in a given dimension
359
# which we determine from the strides of the parent
360
strides(V::SubArray) = substrides(strides(V.parent), V.indices)
54,234✔
361

362
substrides(strds::Tuple{}, ::Tuple{}) = ()
×
363
substrides(strds::NTuple{N,Int}, I::Tuple{ScalarIndex, Vararg{Any}}) where N = (substrides(tail(strds), tail(I))...,)
3,196✔
364
substrides(strds::NTuple{N,Int}, I::Tuple{Slice, Vararg{Any}}) where N = (first(strds), substrides(tail(strds), tail(I))...)
6,740✔
365
substrides(strds::NTuple{N,Int}, I::Tuple{AbstractRange, Vararg{Any}}) where N = (first(strds)*step(I[1]), substrides(tail(strds), tail(I))...)
120,747✔
366
substrides(strds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("strides is invalid for SubArrays with indices of type $(typeof(I[1]))"))
×
367

368
stride(V::SubArray, d::Integer) = d <= ndims(V) ? strides(V)[d] : strides(V)[end] * size(V)[end]
224,936✔
369

370
compute_stride1(parent::AbstractArray, I::NTuple{N,Any}) where {N} =
981,298✔
371
    (@inline; compute_stride1(1, fill_to_length(axes(parent), OneTo(1), Val(N)), I))
398,044✔
372
compute_stride1(s, inds, I::Tuple{}) = s
1✔
373
compute_stride1(s, inds, I::Tuple{Vararg{ScalarIndex}}) = s
49✔
374
compute_stride1(s, inds, I::Tuple{ScalarIndex, Vararg{Any}}) =
5,067✔
375
    (@inline; compute_stride1(s*length(inds[1]), tail(inds), tail(I)))
5,067✔
376
compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1])
308,983✔
377
compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s
67,682✔
378
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))"))
×
379

380
elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P} = elsize(P)
27,622✔
381

382
iscontiguous(A::SubArray) = iscontiguous(typeof(A))
×
383
iscontiguous(::Type{<:SubArray}) = false
×
384
iscontiguous(::Type{<:FastContiguousSubArray}) = true
×
385

386
first_index(V::FastSubArray) = V.offset1 + V.stride1 # cached for fast linear SubArrays
×
387
function first_index(V::SubArray)
×
388
    P, I = parent(V), V.indices
×
389
    s1 = compute_stride1(P, I)
×
390
    s1 + compute_offset1(P, s1, I)
×
391
end
392

393
# Computing the first index simply steps through the indices, accumulating the
394
# sum of index each multiplied by the parent's stride.
395
# The running sum is `f`; the cumulative stride product is `s`.
396
# If the parent is a vector, then we offset the parent's own indices with parameters of I
397
compute_offset1(parent::AbstractVector, stride1::Integer, I::Tuple{AbstractRange}) =
821,093✔
398
    (@inline; first(I[1]) - stride1*first(axes1(I[1])))
2,864,467✔
399
# If the result is one-dimensional and it's a Colon, then linear
400
# indexing uses the indices along the given dimension.
401
# If the result is one-dimensional and it's a range, then linear
402
# indexing might be offset if the index itself is offset
403
# Otherwise linear indexing always matches the parent.
404
compute_offset1(parent, stride1::Integer, I::Tuple) =
160,205✔
405
    (@inline; compute_offset1(parent, stride1, find_extended_dims(1, I...), find_extended_inds(I...), I))
160,205✔
406
compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{Slice}, I::Tuple) =
67,117✔
407
    (@inline; compute_linindex(parent, I) - stride1*first(axes(parent, dims[1])))  # index-preserving case
67,117✔
408
compute_offset1(parent, stride1::Integer, dims, inds::Tuple{AbstractRange}, I::Tuple) =
91,290✔
409
    (@inline; compute_linindex(parent, I) - stride1*first(axes1(inds[1]))) # potentially index-offsetting case
91,290✔
410
compute_offset1(parent, stride1::Integer, dims, inds, I::Tuple) =
1,798✔
411
    (@inline; compute_linindex(parent, I) - stride1)
1,798✔
412
function compute_linindex(parent, I::NTuple{N,Any}) where N
160,205✔
413
    @inline
160,205✔
414
    IP = fill_to_length(axes(parent), OneTo(1), Val(N))
160,205✔
415
    compute_linindex(first(LinearIndices(parent)), 1, IP, I)
160,205✔
416
end
417
function compute_linindex(f, s, IP::Tuple, I::Tuple{ScalarIndex, Vararg{Any}})
163,421✔
418
    @inline
163,421✔
419
    Δi = I[1]-first(IP[1])
163,421✔
420
    compute_linindex(f + Δi*s, s*length(IP[1]), tail(IP), tail(I))
163,421✔
421
end
422
function compute_linindex(f, s, IP::Tuple, I::Tuple{Any, Vararg{Any}})
162,984✔
423
    @inline
162,984✔
424
    Δi = first(I[1])-first(IP[1])
162,984✔
425
    compute_linindex(f + Δi*s, s*length(IP[1]), tail(IP), tail(I))
162,984✔
426
end
427
compute_linindex(f, s, IP::Tuple, I::Tuple{}) = f
160,205✔
428

429
find_extended_dims(dim, ::ScalarIndex, I...) = (@inline; find_extended_dims(dim + 1, I...))
163,421✔
430
find_extended_dims(dim, i1, I...) = (@inline; (dim, find_extended_dims(dim + 1, I...)...))
162,984✔
431
find_extended_dims(dim) = ()
160,205✔
432
find_extended_inds(::ScalarIndex, I...) = (@inline; find_extended_inds(I...))
163,421✔
433
find_extended_inds(i1, I...) = (@inline; (i1, find_extended_inds(I...)...))
162,984✔
434
find_extended_inds() = ()
160,205✔
435

436
function unsafe_convert(::Type{Ptr{T}}, V::SubArray{T,N,P,<:Tuple{Vararg{RangeIndex}}}) where {T,N,P}
155,172✔
437
    return unsafe_convert(Ptr{T}, V.parent) + _memory_offset(V.parent, map(first, V.indices)...)
310,776✔
438
end
439

440
pointer(V::FastSubArray, i::Int) = pointer(V.parent, V.offset1 + V.stride1*i)
×
441
pointer(V::FastContiguousSubArray, i::Int) = pointer(V.parent, V.offset1 + i)
528,078✔
442

443
function pointer(V::SubArray{<:Any,<:Any,<:Array,<:Tuple{Vararg{RangeIndex}}}, is::AbstractCartesianIndex{N}) where {N}
×
444
    index = first_index(V)
×
445
    strds = strides(V)
×
446
    for d = 1:N
×
447
        index += (is[d]-1)*strds[d]
×
448
    end
×
449
    return pointer(V.parent, index)
×
450
end
451

452
# indices are taken from the range/vector
453
# Since bounds-checking is performance-critical and uses
454
# indices, it's worth optimizing these implementations thoroughly
455
axes(S::SubArray) = (@inline; _indices_sub(S.indices...))
93,784,067✔
456
_indices_sub(::Real, I...) = (@inline; _indices_sub(I...))
10,444,844✔
457
_indices_sub() = ()
×
458
function _indices_sub(i1::AbstractArray, I...)
41,471,609✔
459
    @inline
19,664,667✔
460
    (axes(i1)..., _indices_sub(I...)...)
105,017,168✔
461
end
462

463
has_offset_axes(S::SubArray) = has_offset_axes(S.indices...)
239,595✔
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