Skip to content

Commit b5fab6b

Browse files
authored
Get rid of old fixes (#214)
* _multi_check_index was the remnant of an old workaround * custom _generate_unsafe_(setindex!/getindex)_body were used to ensure that arrays still used optimized stuff here, but recent fixes for CartesianIndices inbounds propagation should fix most of this.
1 parent 1fa425d commit b5fab6b

File tree

1 file changed

+8
-61
lines changed

1 file changed

+8
-61
lines changed

src/indexing.jl

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ end
146146
return LogicalIndex{Int}(arg)
147147
end
148148
@propagate_inbounds function to_index(::IndexLinear, x, arg::AbstractArray{<:AbstractCartesianIndex})
149-
@boundscheck _multi_check_index(axes(x), arg) || throw(BoundsError(x, arg))
149+
@boundscheck Base.checkindex(Bool, axes(x), arg) || throw(BoundsError(x, arg))
150150
return arg
151151
end
152152
@propagate_inbounds function to_index(::IndexLinear, x, arg::LogicalIndex)
@@ -175,27 +175,18 @@ to_index(::IndexCartesian, x, arg::Colon) = CartesianIndices(x)
175175
to_index(::IndexCartesian, x, arg::CartesianIndices{0}) = arg
176176
to_index(::IndexCartesian, x, arg::AbstractCartesianIndex) = arg
177177
function to_index(::IndexCartesian, x, arg)
178-
@boundscheck _multi_check_index(axes(x), arg) || throw(BoundsError(x, arg))
178+
@boundscheck Base.checkindex(Bool, axes(x), arg) || throw(BoundsError(x, arg))
179179
return arg
180180
end
181-
@propagate_inbounds function to_index(::IndexCartesian, x, arg::AbstractArray{<:AbstractCartesianIndex})
182-
@boundscheck _multi_check_index(axes(x), arg) || throw(BoundsError(x, arg))
181+
function to_index(::IndexCartesian, x, arg::AbstractArray{<:AbstractCartesianIndex})
182+
@boundscheck Base.checkindex(Bool, axes(x), arg) || throw(BoundsError(x, arg))
183183
return arg
184184
end
185-
@propagate_inbounds function to_index(::IndexCartesian, x, arg::AbstractArray{Bool})
185+
function to_index(::IndexCartesian, x, arg::AbstractArray{Bool})
186186
@boundscheck checkbounds(x, arg)
187187
return LogicalIndex(arg)
188188
end
189-
190-
function _multi_check_index(axs::Tuple, arg::AbstractArray{T}) where {T<:AbstractCartesianIndex}
191-
b = true
192-
for i in arg
193-
b &= Base.checkbounds_indices(Bool, axs, (i,))
194-
end
195-
return b
196-
end
197-
198-
@propagate_inbounds function to_index(::IndexCartesian, x, arg::Union{Array{Bool}, BitArray})
189+
function to_index(::IndexCartesian, x, arg::Union{Array{Bool}, BitArray})
199190
@boundscheck checkbounds(x, arg)
200191
return LogicalIndex{Int}(arg)
201192
end
@@ -370,33 +361,12 @@ function unsafe_get_collection(A, inds)
370361
axs = to_axes(A, inds)
371362
dest = similar(A, axs)
372363
if map(Base.unsafe_length, axes(dest)) == map(Base.unsafe_length, axs)
373-
_unsafe_get_index!(dest, A, inds...) # usually a generated function, don't allow it to impact inference result
364+
Base._unsafe_getindex!(dest, A, inds...)
374365
else
375366
Base.throw_checksize_error(dest, axs)
376367
end
377368
return dest
378369
end
379-
380-
function _generate_unsafe_get_index!_body(N::Int)
381-
quote
382-
Compat.@inline()
383-
D = eachindex(dest)
384-
Dy = iterate(D)
385-
@inbounds Base.Cartesian.@nloops $N j d -> I[d] begin
386-
# This condition is never hit, but at the moment
387-
# the optimizer is not clever enough to split the union without it
388-
Dy === nothing && return dest
389-
(idx, state) = Dy
390-
dest[idx] = unsafe_getindex(src, NDIndex(Base.Cartesian.@ntuple($N, j)))
391-
Dy = iterate(D, state)
392-
end
393-
return dest
394-
end
395-
end
396-
@generated function _unsafe_get_index!(dest, src, I::Vararg{Any,N}) where {N}
397-
return _generate_unsafe_get_index!_body(N)
398-
end
399-
400370
_ints2range(x::Integer) = x:x
401371
_ints2range(x::AbstractRange) = x
402372
@inline function unsafe_get_collection(A::CartesianIndices{N}, inds) where {N}
@@ -476,28 +446,5 @@ unsafe_setindex!(a, v, i::Vararg{Any}) = unsafe_set_collection!(a, v, i)
476446
477447
Sets `inds` of `A` to `val`. `inds` is assumed to have been bounds-checked.
478448
=#
479-
@inline unsafe_set_collection!(A, v, i) = _unsafe_setindex!(A, v, i...)
480-
481-
function _generate_unsafe_setindex!_body(N::Int)
482-
quote
483-
x′ = Base.unalias(A, x)
484-
Base.Cartesian.@nexprs $N d -> (I_d = Base.unalias(A, I[d]))
485-
idxlens = Base.Cartesian.@ncall $N Base.index_lengths I
486-
Base.Cartesian.@ncall $N Base.setindex_shape_check x′ (d -> idxlens[d])
487-
Xy = iterate(x′)
488-
@inbounds Base.Cartesian.@nloops $N i d->I_d begin
489-
# This is never reached, but serves as an assumption for
490-
# the optimizer that it does not need to emit error paths
491-
Xy === nothing && break
492-
(val, state) = Xy
493-
unsafe_setindex!(A, val, NDIndex(Base.Cartesian.@ntuple($N, i)))
494-
Xy = iterate(x′, state)
495-
end
496-
A
497-
end
498-
end
499-
500-
@generated function _unsafe_setindex!(A, x, I::Vararg{Any,N}) where {N}
501-
return _generate_unsafe_setindex!_body(N)
502-
end
449+
unsafe_set_collection!(A, v, i) = Base._unsafe_setindex!(IndexStyle(A), A, v, i...)
503450

0 commit comments

Comments
 (0)