From c8d1ae8c3a7db8b2a21ed35ca2a0a063ccf874de Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Mon, 16 Jun 2025 14:26:57 +0200 Subject: [PATCH 1/2] help bounds checking to be eliminated for `getindex(::Memory, ::Int)` --- base/essentials.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/base/essentials.jl b/base/essentials.jl index 607ce44b39361..ce285c94d804f 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -382,8 +382,19 @@ 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 + 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) """ From a526580b607ee73d645fc2e021d1f296800c7734 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Mon, 16 Jun 2025 14:36:09 +0200 Subject: [PATCH 2/2] add `effect_free` --- base/essentials.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/essentials.jl b/base/essentials.jl index ce285c94d804f..5ee35325b0832 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -384,6 +384,7 @@ default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic 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)