Here’s a MWE of the allocating code (Julia 1.11.2):
function foo()
arr = [Ref(i) for i=1:100]
iter = (i for i in arr if rand()>0)
@time begin
elem, state = iterate(iter)
while true
next = iterate(iter, state)
isnothing(next) && break
elem, state = next
end
end
return nothing
end
foo()
which will show:
0.000003 seconds (200 allocations: 6.250 KiB)
If you make the element bitstype/immutable (Ref(i)
→ i
) it becomes non-allocating, or if you remove the if rand()>0
it also becomes non-allocating. But the combination (which is unavoidable in my real code) apparently allocates every single element twice.
Any suggestions how to fix this?