# Allocation depending on value of typed field in struct

**URL:** <https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317>\
**Category:** New to Julia\
**Tags:** question, performance, memory-allocation\
**Created:** [March 23, 2022, 5:19am UTC](https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317 "2022-03-23T05:19:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![slocook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slocook/32/34808_2.png) [@slocook](https://discourse.julialang.org/u/slocook)\
**Post date:** [March 23, 2022, 5:19am UTC](https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317/1 "2022-03-23T05:19:37Z")

</div>

I’m seeing some behavior with accessing data members of structs that I don’t understand - depending on the value of typed field in a struct, I’m getting an allocation with `getfield`.

```julia
struct MyStruct
   v::Int
end

ms0 = MyStruct(0)
ms512 = MyStruct(512)

@btime getfield(ms0, :v)
  15.415 ns (0 allocations: 0 bytes)

@btime getfield(ms512, :v)
  20.361 ns (1 allocation: 16 bytes)

```

It seems that for values less than 512, there is no allocation. I also do not see any allocations for any size number if I instead define the field `v` without a type. Is there a reason for this?

Also, why does accessing the member directly with `.` cause more allocations and why is it so much slower?

```julia
@btime ms0.v
  43.088 ns (1 allocation: 16 bytes)

@btime ms512.v
  51.874 ns (3 allocations: 48 bytes)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 23, 2022, 5:49am UTC](https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317/2 "2022-03-23T05:49:00Z")

</div>

You’re using non constant global variable

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 23, 2022, 6:05am UTC](https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317/3 "2022-03-23T06:05:54Z")

</div>

Try

> [@slocook](#):
>
> ```julia
> @btime getfield($ms0, :v)
> @btime getfield($ms512, :v)
> 
> ```

These should both be expected to use zero time.

---

<div class="post-metadata">

**Author:** ![slocook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slocook/32/34808_2.png) [@slocook](https://discourse.julialang.org/u/slocook)\
**Post date:** [March 23, 2022, 6:16am UTC](https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317/4 "2022-03-23T06:16:58Z")

</div>

Interesting:

1. Making them `const` is basically free (0.001 ns and no allocations).
2. Putting the creation and `getfield` into a function also made it free
3. Trying @DNF’s suggestion, resulted in a time of 1.100 ns with 0 allocations

I’m assuming (1) and (2) are due to the whole thing being compiled out and essentially returning the value? How/why does (3) behave differently than these?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 23, 2022, 6:39am UTC](https://discourse.julialang.org/t/allocation-depending-on-value-of-typed-field-in-struct/78317/5 "2022-03-23T06:39:07Z")

</div>

It’s probably just benchmarking artefacts. It’s difficult to get good, accurate timings of operations that are essentially free. In statically inferred code this should not take any time at all.
