diff --git a/base/essentials.jl b/base/essentials.jl index 607ce44b39361..5ee35325b0832 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -382,8 +382,20 @@ 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 + @_effect_free_terminates_locally_meta + elt = eltype(A) + if (Any <: elt) || (isconcretetype(elt) && isbitstype(elt)) + @boundscheck checkbounds(A, i) + let ptr = pointer(A), ord = Base.default_access_order(A) + GC.@preserve A unsafe_load(ptr, i, ord) + end + else + memoryrefget(memoryrefnew(memoryrefnew(A), i, @_boundscheck), default_access_order(A), false) + end +end + getindex(A::GenericMemoryRef) = memoryrefget(A, default_access_order(A), @_boundscheck) """