# How to get rid of allocations in this simple filtered generator example?

**URL:** <https://discourse.julialang.org/t/how-to-get-rid-of-allocations-in-this-simple-filtered-generator-example/124847>\
**Category:** General Usage\
**Tags:** memory-allocation, rng\
**Created:** [January 16, 2025, 7:46pm UTC](https://discourse.julialang.org/t/how-to-get-rid-of-allocations-in-this-simple-filtered-generator-example/124847 "2025-01-16T19:46:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [January 16, 2025, 7:46pm UTC](https://discourse.julialang.org/t/how-to-get-rid-of-allocations-in-this-simple-filtered-generator-example/124847/1 "2025-01-16T19:46:14Z")

</div>

Here’s a MWE of the allocating code (Julia 1.11.2):

```julia
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:

```julia
  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?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [January 16, 2025, 8:24pm UTC](https://discourse.julialang.org/t/how-to-get-rid-of-allocations-in-this-simple-filtered-generator-example/124847/2 "2025-01-16T20:24:15Z")

</div>

The allocations go away if you don’t use the global rng state:

```julia
using Random

function foo()
    rng = Xoshiro(0)
    arr = [Ref(i) for i=1:100]
    iter = (i for i in arr if rand(rng)>0)
    @time begin
        elem, state = iterate(iter)
        while true
            next = iterate(iter, state)
            isnothing(next) && break
            elem, state = next
        end
    end
    return nothing
end

```

```julia-repl
julia> foo()
  0.000000 seconds

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 16, 2025, 8:53pm UTC](https://discourse.julialang.org/t/how-to-get-rid-of-allocations-in-this-simple-filtered-generator-example/124847/3 "2025-01-16T20:53:41Z")

</div>

> [@CameronBieganek](#):
>
> The allocations go away if you don’t use the global rng state

Cool.

1. Is there any special method which can point this out quickly?
2. The `rng` allocations are because of per thread state? Asking because, normally one wouldn’t expect some global state to allocate.

---

<div class="post-metadata">

**Author:** ![r23j5a](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r23j5a/32/214224_2.png) [@r23j5a](https://discourse.julialang.org/u/r23j5a)\
**Post date:** [January 22, 2025, 6:45pm UTC](https://discourse.julialang.org/t/how-to-get-rid-of-allocations-in-this-simple-filtered-generator-example/124847/4 "2025-01-22T18:45:43Z")

</div>

> [@marius311](#):
>
> ` [Ref(i) for i=1:100]`

new to Julia, what’s the use case of the Ref(i)? I get it’s an MWE though, but trying to figureout Rel

Also, the version without Ref(i), but using rand() doesn’t allocate for me:

```julia
julia> function foo()
           arr = [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 (generic function with 1 method)

julia> foo()
  0.000004 seconds

julia> versioninfo()
Julia Version 1.11.2
Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i5-8400H CPU @ 2.50GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

```
