Skip to content

better effects for iterate for Memory and Array #58755

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
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ end

## Iteration ##

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

## Indexing: getindex ##

Expand Down
7 changes: 6 additions & 1 deletion base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ Memory{T}(x::AbstractArray{S,1}) where {T,S} = copyto_axcheck!(Memory{T}(undef,

## Iteration ##

iterate(A::Memory, i=1) = (@inline; (i - 1)%UInt < length(A)%UInt ? (@inbounds A[i], i + 1) : nothing)
function _iterate_array(A::Union{Memory, Array}, i::Int)
@inline
(i - 1)%UInt < length(A)%UInt ? (A[i], i + 1) : nothing
Copy link
Member

@mbauman mbauman Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could (should?) this also become _checkbounds_array(Bool, A, i)?

end

iterate(A::Memory, i=1) = (@inline; _iterate_array(A, i))

## Indexing: getindex ##

Expand Down
14 changes: 14 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,20 @@ end
end
end

@testset "effect inference for `iterate` for `Array` and for `Memory`" begin
for El (Float32, Real, Any)
for Arr (Memory{El}, Array{El, 0}, Vector{El}, Matrix{El}, Array{El, 3})
effects = Base.infer_effects(iterate, Tuple{Arr, Int})
@test Base.Compiler.is_effect_free(effects)
@test Base.Compiler.is_terminates(effects)
@test Base.Compiler.is_notaskstate(effects)
@test Base.Compiler.is_noub(effects)
@test Base.Compiler.is_nonoverlayed(effects)
@test Base.Compiler.is_nortcall(effects)
end
end
end

@testset "iterate for linear indexing" begin
A = [1 2; 3 4]
v = view(A, :)
Expand Down
Loading