# Why does this allocate?

**URL:** https://discourse.julialang.org/t/why-does-this-allocate/42298
**Category:** Performance
**Created:** [June 30, 2020, 11:08am UTC](https://discourse.julialang.org/t/why-does-this-allocate/42298 "2020-06-30T11:08:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![cafaxo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cafaxo/32/5234_2.png) [@cafaxo](https://discourse.julialang.org/u/cafaxo)
#### Post date: [June 30, 2020, 11:08am UTC](https://discourse.julialang.org/t/why-does-this-allocate/42298/1 "2020-06-30T11:08:36Z")

</div>

Consider the following code:

```julia
function foo(x)
    if x[1] > 0 
        (x, 1)
    else
        nothing
    end
end

```

It seems that this code has 1 allocation:

```julia
julia> bla = [1]
1-element Array{Int64,1}:
 1

julia> @time foo(bla)
  0.004369 seconds (1.32 k allocations: 98.988 KiB)
([1], 1)

julia> @time foo(bla)
  0.000003 seconds (1 allocation: 32 bytes)
([1], 1)

```

Why does this happen?  
I am using Julia built from commit 39c278b728.

The allocation does not happen for either

```julia
function foo1(x)
    if x[1] > 0 
        (x, 1)
    else
        (x, 2)
    end
end

```

or

```julia
function foo2(x)
    if x[1] > 0 
        x
    else
        nothing
    end
end

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 30, 2020, 11:44am UTC](https://discourse.julialang.org/t/why-does-this-allocate/42298/3 "2020-06-30T11:44:21Z")

</div>

> [@cafaxo](#):
>
> Why does this happen?

Probably because the tuple ends up in global scope. If you do something like

```julia
julia> g(bla) = (foo(bla); nothing)
g (generic function with 1 method)

julia> @time g(bla)
  0.000002 seconds

```

it doesn’t allocate.

---

<div class="post-metadata">

### Author: ![cafaxo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cafaxo/32/5234_2.png) [@cafaxo](https://discourse.julialang.org/u/cafaxo)
#### Post date: [June 30, 2020, 11:51am UTC](https://discourse.julialang.org/t/why-does-this-allocate/42298/4 "2020-06-30T11:51:29Z")

</div>

Note that your code also allocates if we add @noinline to foo and @code\_llvm foo(bla) shows that a call to jl\_gc\_pool\_alloc is being generated.  
Consider this slightly longer example:

```julia
struct Bla
    x::Int
end

@noinline function Base.iterate(iter::Bla, state)
    st, idx = state

    if idx <= length(st)
        return st[idx], (st, idx + 1)
    else
        return nothing
    end
end

@noinline function Base.iterate(iter::Bla)
    st = collect(1:iter.x)
    idx = 1

    return Base.iterate(iter, (st, idx))
end

function sumslow(bla::Bla)
    n = 0

    for x in bla
        n += x
    end

    return n
end

Base.IteratorSize(::Type{Bla}) = Base.SizeUnknown()

Base.eltype(::Type{Bla}) = Int

```

The @noinline is intentional since this is a reduced example and I do not want to force @inline for the project I am working on.  
I get the following result:

```julia
julia> @btime sumslow(Bla(2^20))
  12.555 ms (1048578 allocations: 40.00 MiB)
549756338176

```

Without the @noinline, I get

```julia
julia> @btime sumslow(Bla(2^20))
  1.950 ms (2 allocations: 8.00 MiB)
549756338176

```

Does this happen due to an unrelated reason?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 30, 2020, 12:18pm UTC](https://discourse.julialang.org/t/why-does-this-allocate/42298/5 "2020-06-30T12:18:23Z")

</div>

> [@cafaxo](#):
>
> Consider this slightly longer example:

In general, tuples that wrap heap-allocated objects might themselves have to be heap-allocated.

---

<div class="post-metadata">

### Author: ![cafaxo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cafaxo/32/5234_2.png) [@cafaxo](https://discourse.julialang.org/u/cafaxo)
#### Post date: [June 30, 2020, 12:23pm UTC](https://discourse.julialang.org/t/why-does-this-allocate/42298/6 "2020-06-30T12:23:42Z")

</div>

I thought so. I’m probably hitting a case where [https://github.com/JuliaLang/julia/pull/33886](https://github.com/JuliaLang/julia/pull/33886) does not apply, right?
