Skip to content

Commit 67c7a2b

Browse files
authored
allow constant-prop through iterate (#158)
* allow constant-prop through `iterate` Allows inference to unroll `iterate` for a `FixedSizeArray` with statically known size * fixup * Apply suggestions from code review * Update test/runtests.jl * fix `iterate` overload
1 parent ff29b83 commit 67c7a2b

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/FixedSizeArray.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,9 @@ function Base.reshape(a::FixedSizeArray, size::(NTuple{N,Int} where {N}))
404404
new_fixed_size_array(parent(a), size)
405405
end
406406

407-
# `iterate`: the `AbstractArray` fallback doesn't perform well, so add our own methods
407+
# `iterate`: the `AbstractArray` fallback doesn't perform well, so add our own methods (copied from `Base.Array`)
408408

409-
function Base.iterate(a::FixedSizeArray)
410-
iterate(parent(a))
411-
end
412-
function Base.iterate(a::FixedSizeArray, state)
413-
iterate(parent(a), state)
414-
end
409+
Base.iterate(A::FixedSizeArray, i=1) = (@inline; (i - 1)%UInt < length(A)%UInt ? (@inbounds A[i], i + 1) : nothing)
415410

416411
"""
417412
FixedSizeArrayDefault{T,N}(undef, size::NTuple{N,Int})

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,14 @@ end
698698
@test farr isa Transpose
699699
@test parent(farr) isa FixedSizeArray
700700
end
701+
702+
@testset "Constant-prop through iterate" begin
703+
@test Base.return_types() do
704+
m = FixedSizeArray{Int}(undef, 3)
705+
m[1] = 1
706+
m[2] = 2
707+
m[3] = 3
708+
tuple(m...)
709+
end[] == NTuple{3, Int}
710+
end
701711
end

0 commit comments

Comments
 (0)