# Getproperty optimization in struct

**URL:** https://discourse.julialang.org/t/getproperty-optimization-in-struct/39990
**Category:** Performance
**Created:** [May 22, 2020, 8:25pm UTC](https://discourse.julialang.org/t/getproperty-optimization-in-struct/39990 "2020-05-22T20:25:41Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kirtsar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirtsar/32/7222_2.png) [@kirtsar](https://discourse.julialang.org/u/kirtsar)
#### Post date: [May 22, 2020, 8:25pm UTC](https://discourse.julialang.org/t/getproperty-optimization-in-struct/39990/1 "2020-05-22T20:25:41Z")

</div>

I have the following code (example):

```julia
struct Foo{N}
    t :: NTuple{N, Int}
end

foo = Foo((1,2,3))

```

Now if I try to get access to t field, I have some unnecessary allocations:

```julia
using BenchmarkTools
@benchmark getproperty(foo, :t)

```

How it can be avoided?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 22, 2020, 8:29pm UTC](https://discourse.julialang.org/t/getproperty-optimization-in-struct/39990/2 "2020-05-22T20:29:00Z")

</div>

You’re not benchmarking `getproperty`, you’re benchmarking the overhead of resolving a global variable.

Here’s a more realistic benchmark of what I think you’re trying to measure:

```julia
struct Foo{N}
    t :: NTuple{N, Int}
end

foo = Foo((1,2,3))

julia> @benchmark getproperty($(Ref(foo))[], :t)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 1.299 ns (0.00% GC)
  median time: 1.340 ns (0.00% GC)
  mean time: 1.339 ns (0.00% GC)
  maximum time: 6.780 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```

If you want to measure the case where the compiler has an opportunity to know not just the type, but the contents of `foo` at compile time then you can do

```julia
julia> @benchmark getproperty($foo, :t)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 0.020 ns (0.00% GC)
  median time: 0.020 ns (0.00% GC)
  mean time: 0.024 ns (0.00% GC)
  maximum time: 0.130 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```

which just says that the compiler was able to completely elide the operation and do nothing. (sub ns timings basically always means that it was a no-op)
