# Will return allocate memory?

**URL:** <https://discourse.julialang.org/t/will-return-allocate-memory/112987>\
**Category:** Performance\
**Tags:** question, performance\
**Created:** [April 16, 2024, 12:39am UTC](https://discourse.julialang.org/t/will-return-allocate-memory/112987 "2024-04-16T00:39:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Strange\_Xue](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@Strange\_Xue](https://discourse.julialang.org/u/Strange_Xue)\
**Post date:** [April 16, 2024, 12:39am UTC](https://discourse.julialang.org/t/will-return-allocate-memory/112987/1 "2024-04-16T00:39:34Z")

</div>

Hi guys, looking at the following two versions of codes. I found more allocations in version 2 perhaps because b is returned in version 2. However, `b = [a]` is also operated in both versions. Why is there a big difference in allocation?

BTW, `b` is stored in heap or stack and why? it seems `b[1] === a` so `b[1]` is a reference., but `b` is mutable structure and should be stored in the heap I guess.

### Version 1

```Julia
using BenchmarkTools
using StaticArrays
function m2()
    a = SVector(1:32...)
    b = [a]
    a === b[1]
end
@btime m2() # 0 allocations

```

```julia-auto
  2.200 ns (0 allocations: 0 bytes)
true

```

### Version 2

```Julia
using BenchmarkTools
using StaticArrays
function m2()
    a = SVector(1:32...)
    b = [a]
    #a === b[1]
end
@btime m2() # 0 allocations

```

```Julia
34.511 ns (1 allocation: 336 bytes)
1-element Vector{SVector{32, Int64}}:
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 … 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 16, 2024, 12:54am UTC](https://discourse.julialang.org/t/will-return-allocate-memory/112987/2 "2024-04-16T00:54:58Z")

</div>

Return doesn’t allocate, but it can prevent an allocation from being removed. Consider the following pair of examples

```julia
f1(n) = rand(n)
f2(n) = (rand(n); return nothing)

```

`f1` obviously has to allocate a length `n` vector since that is what the function returns. `f2` can be optimized to a simple `return nothing` because although, the function as written creates the same vector as `f1`, it provably doesn’t let anyone observe that the vector was created.

---

<div class="post-metadata">

**Author:** ![Strange\_Xue](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@Strange\_Xue](https://discourse.julialang.org/u/Strange_Xue)\
**Post date:** [April 16, 2024, 1:03am UTC](https://discourse.julialang.org/t/will-return-allocate-memory/112987/3 "2024-04-16T01:03:40Z")

</div>

so you mean Julia compiler has done something in stage of compiling to prevent computing when running?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 16, 2024, 1:19am UTC](https://discourse.julialang.org/t/will-return-allocate-memory/112987/4 "2024-04-16T01:19:00Z")

</div>

That is correct. If Julia can conclude it does not need to compute, it will not.

```julia
julia> f(n) = n + 3
f (generic function with 1 method)

julia> g(n) = (n + 3; nothing)
g (generic function with 1 method)

julia> @code_llvm f(5)
; @ REPL[1]:1 within `f`
define i64 @julia_f_136(i64 signext %0) #0 {
top:
; ┌ @ int.jl:87 within `+`
   %1 = add i64 %0, 3
; └
  ret i64 %1
}

julia> @code_llvm g(5)
; @ REPL[2]:1 within `g`
define void @julia_g_147(i64 signext %0) #0 {
top:
  ret void
}

```

`rand` is a bad example though. `rand` does have an effect. It will change the next call to `rand`.
