# Where does this allocation come from and how to avoid it? StaticArrays and structures

**URL:** <https://discourse.julialang.org/t/where-does-this-allocation-come-from-and-how-to-avoid-it-staticarrays-and-structures/84462>\
**Category:** Performance\
**Tags:** question, function, struct, staticarrays, allocations\
**Created:** [July 19, 2022, 2:13pm UTC](https://discourse.julialang.org/t/where-does-this-allocation-come-from-and-how-to-avoid-it-staticarrays-and-structures/84462 "2022-07-19T14:13:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coloboquito](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@coloboquito](https://discourse.julialang.org/u/coloboquito)\
**Post date:** [July 19, 2022, 2:13pm UTC](https://discourse.julialang.org/t/where-does-this-allocation-come-from-and-how-to-avoid-it-staticarrays-and-structures/84462/1 "2022-07-19T14:13:35Z")

</div>

Hello!

I’m trying to optimize my code and I’m stuck on this. Here is a minimum working example of my problem.

```julia
f(x :: T, y :: T, z :: T) = SA[x, y, z]

struct Foo{T <: Real}
    x :: T; y :: T; z :: T
end

f(foo :: Foo) = SA[foo.x, foo.y, foo.z]

```

The problem is difference in the number of allocations when this function is executed

```julia
julia> @btime f(1.0,2.0,3.0)
  0.010 ns (0 allocations: 0 bytes)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

```

```julia
julia> foo = Foo(1.0, 2.0, 3.0)
Foo{Float64}(1.0, 2.0, 3.0)

julia> @btime f(foo)
  8.464 ns (1 allocation: 32 bytes)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

```

Where do this allocation arise? Can someone explain it or point in the right direction for me to research?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 19, 2022, 2:24pm UTC](https://discourse.julialang.org/t/where-does-this-allocation-come-from-and-how-to-avoid-it-staticarrays-and-structures/84462/2 "2022-07-19T14:24:37Z")

</div>

You’re just seeing benchmarking artifacts. You need to use `$` to interpolate values when benchmarking, as discussed in more detail in the manual: [Manual · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/dev/manual/)

```julia
julia> using StaticArrays, BenchmarkTools

julia> struct Foo{T <: Real}
           x :: T; y :: T; z :: T
       end

julia> f(foo :: Foo) = SA[foo.x, foo.y, foo.z]
f (generic function with 1 method)

julia> foo = Foo(1., 2., 3.)
Foo{Float64}(1.0, 2.0, 3.0)

julia> @btime f($foo)
  1.449 ns (0 allocations: 0 bytes)

```

Furthermore, when you see a benchmark returning `0.010 ns` (or really anything less than 1ns), then that just means the compiler was smart enough to optimize your entire code away. See [Manual · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/dev/manual/#Understanding-compiler-optimizations) for more.

---

<div class="post-metadata">

**Author:** ![coloboquito](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@coloboquito](https://discourse.julialang.org/u/coloboquito)\
**Post date:** [July 19, 2022, 2:35pm UTC](https://discourse.julialang.org/t/where-does-this-allocation-come-from-and-how-to-avoid-it-staticarrays-and-structures/84462/3 "2022-07-19T14:35:59Z")

</div>

Thanks!

So is there an actual difference between the performance of two different functions?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 19, 2022, 2:50pm UTC](https://discourse.julialang.org/t/where-does-this-allocation-come-from-and-how-to-avoid-it-staticarrays-and-structures/84462/4 "2022-07-19T14:50:13Z")

</div>

Nope, I’d expect both to be about equally fast.
