Skip to content

help bounds checking to be eliminated for getindex(::Memory, ::Int) #58754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,26 @@ macro _nospecializeinfer_meta()
return Expr(:meta, :nospecializeinfer)
end

function _checkbounds_array(::Type{Bool}, A::Union{Array, GenericMemory}, i::Int)
@inline
ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A)))
end
function _checkbounds_array(A::Union{Array, GenericMemory}, i::Int)
@inline
_checkbounds_array(Bool, A, i) || throw_boundserror(A, (i,))
end

default_access_order(a::GenericMemory{:not_atomic}) = :not_atomic
default_access_order(a::GenericMemory{:atomic}) = :monotonic
default_access_order(a::GenericMemoryRef{:not_atomic}) = :not_atomic
default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic

getindex(A::GenericMemory, i::Int) = (@_noub_if_noinbounds_meta;
memoryrefget(memoryrefnew(memoryrefnew(A), i, @_boundscheck), default_access_order(A), false))
function getindex(A::GenericMemory, i::Int)
@_noub_if_noinbounds_meta
(@_boundscheck) && _checkbounds_array(A, i)
memoryrefget(memoryrefnew(memoryrefnew(A), i, false), default_access_order(A), false)
end

getindex(A::GenericMemoryRef) = memoryrefget(A, default_access_order(A), @_boundscheck)

"""
Expand Down Expand Up @@ -949,13 +962,13 @@ end
# linear indexing
function getindex(A::Array, i::Int)
@_noub_if_noinbounds_meta
@boundscheck ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
@boundscheck _checkbounds_array(A, i)
memoryrefget(memoryrefnew(getfield(A, :ref), i, false), :not_atomic, false)
end
# simple Array{Any} operations needed for bootstrap
function setindex!(A::Array{Any}, @nospecialize(x), i::Int)
@_noub_if_noinbounds_meta
@boundscheck ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) || throw_boundserror(A, (i,))
@boundscheck _checkbounds_array(A, i)
memoryrefset!(memoryrefnew(getfield(A, :ref), i, false), x, :not_atomic, false)
return A
end
Expand Down