# Addition of floats allocates memory in not fully qualified struct

**URL:** <https://discourse.julialang.org/t/addition-of-floats-allocates-memory-in-not-fully-qualified-struct/77583>\
**Category:** New to Julia\
**Tags:** question, memory-allocation, parametric-types, struct, allocations\
**Created:** [March 8, 2022, 12:48pm UTC](https://discourse.julialang.org/t/addition-of-floats-allocates-memory-in-not-fully-qualified-struct/77583 "2022-03-08T12:48:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ncxst](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@ncxst](https://discourse.julialang.org/u/ncxst)\
**Post date:** [March 8, 2022, 12:48pm UTC](https://discourse.julialang.org/t/addition-of-floats-allocates-memory-in-not-fully-qualified-struct/77583/1 "2022-03-08T12:48:01Z")

</div>

Hi,  
I see allocations when adding floats in a not fully qualified struct. Can you explain why the function `func` allocates memory when passing a `BarSlow` type into it? Below you find a minimal working example.  
Thank you,  
ncxst

```julia
using BenchmarkTools

mutable struct Foo{T}
    u::Float64
    t::T
end

mutable struct BarFast{T}
    foo::Foo{T}
    u::Float64
end
mutable struct BarSlow
    foo::Foo
    u::Float64
end

function func(bar)
    bar.foo.u += bar.u
    return nothing
end

f() = 0.0
foo = Foo(0.0, (f,))
barFast = BarFast(foo, 0.1)
barSlow = BarSlow(foo, 0.1)

@btime func($barFast)
  3.127 ns (0 allocations: 0 bytes)

@btime func($barSlow)
  53.306 ns (2 allocations: 32 bytes)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 8, 2022, 1:20pm UTC](https://discourse.julialang.org/t/addition-of-floats-allocates-memory-in-not-fully-qualified-struct/77583/2 "2022-03-08T13:20:17Z")

</div>

This is a type instability. The compiler doesn’t know the type of `bar.foo` at compile time, so it has store it in a heap-allocated “box” with a type tag that is inspected at runtime.

See [Performance Tips: Avoid fields with abstract type](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type).
