# Struct and memory allocation

**URL:** <https://discourse.julialang.org/t/struct-and-memory-allocation/108632>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [January 10, 2024, 4:11pm UTC](https://discourse.julialang.org/t/struct-and-memory-allocation/108632 "2024-01-10T16:11:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [January 10, 2024, 4:11pm UTC](https://discourse.julialang.org/t/struct-and-memory-allocation/108632/1 "2024-01-10T16:11:31Z")

</div>

Hello ! 🙂  
I don’t understand why there is memory allocation when I acces to a parameters of my immutable struct ?

```julia
julia> using BenchmarkTools
julia> struct S1
       a::NTuple{3,Int64}
       b::Float64
       end

julia> s = S1((1,2,3), 4.5)
S1((1, 2, 3), 4.5)

julia> @btime s.a
  31.791 ns (2 allocations: 80 bytes)
(1, 2, 3)

julia> @allocated s.a
80

```

I read the post [Mutability · JuliaNotes.jl (m3g.github.io)](https://m3g.github.io/JuliaNotes.jl/stable/immutable/) and [Common allocation mistakes - General Usage / Performance - Julia Programming Language (julialang.org)](https://discourse.julialang.org/t/common-allocation-mistakes/66127) but I don’t understand this case.

Thanks !  
fdekerm

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 10, 2024, 4:12pm UTC](https://discourse.julialang.org/t/struct-and-memory-allocation/108632/2 "2024-01-10T16:12:40Z")

</div>

You are working in global scope. Put your code in a function and try again.

---

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [January 10, 2024, 4:15pm UTC](https://discourse.julialang.org/t/struct-and-memory-allocation/108632/3 "2024-01-10T16:15:56Z")

</div>

Thanks you are right, I am wrong to do my tests in the REPL

```julia
julia> function f(s)
       s.a
       end
f (generic function with 2 methods)

julia> @btime f(s)
  14.228 ns (1 allocation: 32 bytes)
(1, 2, 3)

```

But always 1 allocation ?

---

<div class="post-metadata">

**Author:** ![Albert\_de\_montserrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_de_montserrat/32/9135_2.png) [@Albert\_de\_montserrat](https://discourse.julialang.org/u/Albert_de_montserrat)\
**Post date:** [January 10, 2024, 4:23pm UTC](https://discourse.julialang.org/t/struct-and-memory-allocation/108632/4 "2024-01-10T16:23:36Z")

</div>

As described in the [docs](https://juliaci.github.io/BenchmarkTools.jl/stable/), you need to interpolate `s` with the `$` symbol whenever benchmarking something with either `@btime` or `@benchmark`

```julia-repl
In [1]: @btime $s.a
  2.500 ns (0 allocations: 0 bytes)
(1, 2, 3)

In [2]: @btime f($s)
  2.700 ns (0 allocations: 0 bytes)
(1, 2, 3)

```

---

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [January 10, 2024, 4:29pm UTC](https://discourse.julialang.org/t/struct-and-memory-allocation/108632/5 "2024-01-10T16:29:07Z")

</div>

Thanks ! 👍 I didn’t read the documentation carefully!  
So there is no difference in terms of performance/allocation between 1) and 2):

```julia
julia> function g(t)
       t[1]
       end
g (generic function with 1 method)

```

**1)**

```julia
julia> g(s.a)
1

```

**2)**

```julia
julia> t = s.a
(1, 2, 3)

julia> g(t)
1

```
