Skip to content

Commit a8c8cae

Browse files
committed
better effects for iterate for Memory and Array
After PR JuliaLang#58754 the bounds checking is eliminated by LLVM even without asserting `inbounds`, so delete `inbounds` to allow the `noub` effect to be inferred. Also deduplicate and test for good effect inference.
1 parent 36a4616 commit a8c8cae

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

base/array.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ end
904904

905905
## Iteration ##
906906

907-
iterate(A::Array, i=1) = (@inline; (i - 1)%UInt < length(A)%UInt ? (@inbounds A[i], i + 1) : nothing)
907+
iterate(A::Array, i=1) = (@inline; _iterate_array(A, i))
908908

909909
## Indexing: getindex ##
910910

base/genericmemory.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,12 @@ Memory{T}(x::AbstractArray{S,1}) where {T,S} = copyto_axcheck!(Memory{T}(undef,
225225

226226
## Iteration ##
227227

228-
iterate(A::Memory, i=1) = (@inline; (i - 1)%UInt < length(A)%UInt ? (@inbounds A[i], i + 1) : nothing)
228+
function _iterate_array(A::Union{Memory, Array}, i::Int)
229+
@inline
230+
(i - 1)%UInt < length(A)%UInt ? (A[i], i + 1) : nothing
231+
end
232+
233+
iterate(A::Memory, i=1) = (@inline; _iterate_array(A, i))
229234

230235
## Indexing: getindex ##
231236

test/abstractarray.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,20 @@ end
22872287
end
22882288
end
22892289

2290+
@testset "effect inference for `iterate` for `Array` and for `Memory`" begin
2291+
for El (Float32, Real, Any)
2292+
for Arr (Memory{El}, Array{El, 0}, Vector{El}, Matrix{El}, Array{El, 3})
2293+
effects = Base.infer_effects(iterate, Tuple{Arr, Int})
2294+
@test Base.Compiler.is_effect_free(effects)
2295+
@test Base.Compiler.is_terminates(effects)
2296+
@test Base.Compiler.is_notaskstate(effects)
2297+
@test Base.Compiler.is_noub(effects)
2298+
@test Base.Compiler.is_nonoverlayed(effects)
2299+
@test Base.Compiler.is_nortcall(effects)
2300+
end
2301+
end
2302+
end
2303+
22902304
@testset "iterate for linear indexing" begin
22912305
A = [1 2; 3 4]
22922306
v = view(A, :)

0 commit comments

Comments
 (0)