Skip to content

Commit d55a60e

Browse files
committed
help bounds checking to be eliminated for getindex(::Memory, ::Int)
Second try for PR #58741. This moves the `getindex(::Memory, ::Int)` bounds check to Julia, which is how it's already done for `getindex(::Array, ::Int)`, so I guess it's correct. Also deduplicate the bounds checking code while at it.
1 parent 36a4616 commit d55a60e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

base/essentials.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,24 @@ macro _nospecializeinfer_meta()
377377
return Expr(:meta, :nospecializeinfer)
378378
end
379379

380+
function _checkbounds_array(::Type{Bool}, A::Union{Array, GenericMemory}, i::Int)
381+
ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A)))
382+
end
383+
function _checkbounds_array(A::Union{Array, GenericMemory}, i::Int)
384+
(@inline _checkbounds_array(Bool, A, i)) || throw_boundserror(A, (i,))
385+
end
386+
380387
default_access_order(a::GenericMemory{:not_atomic}) = :not_atomic
381388
default_access_order(a::GenericMemory{:atomic}) = :monotonic
382389
default_access_order(a::GenericMemoryRef{:not_atomic}) = :not_atomic
383390
default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic
384391

385-
getindex(A::GenericMemory, i::Int) = (@_noub_if_noinbounds_meta;
386-
memoryrefget(memoryrefnew(memoryrefnew(A), i, @_boundscheck), default_access_order(A), false))
392+
function getindex(A::GenericMemory, i::Int)
393+
@_noub_if_noinbounds_meta
394+
@boundscheck @inline _checkbounds_array(A, i)
395+
memoryrefget(memoryrefnew(memoryrefnew(A), i, false), default_access_order(A), false)
396+
end
397+
387398
getindex(A::GenericMemoryRef) = memoryrefget(A, default_access_order(A), @_boundscheck)
388399

389400
"""
@@ -949,13 +960,13 @@ end
949960
# linear indexing
950961
function getindex(A::Array, i::Int)
951962
@_noub_if_noinbounds_meta
952-
@boundscheck ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
963+
@boundscheck @inline _checkbounds_array(A, i)
953964
memoryrefget(memoryrefnew(getfield(A, :ref), i, false), :not_atomic, false)
954965
end
955966
# simple Array{Any} operations needed for bootstrap
956967
function setindex!(A::Array{Any}, @nospecialize(x), i::Int)
957968
@_noub_if_noinbounds_meta
958-
@boundscheck ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
969+
@boundscheck @inline _checkbounds_array(A, i)
959970
memoryrefset!(memoryrefnew(getfield(A, :ref), i, false), x, :not_atomic, false)
960971
return A
961972
end

0 commit comments

Comments
 (0)