# Allocations while using static arrays in a struct

**URL:** <https://discourse.julialang.org/t/allocations-while-using-static-arrays-in-a-struct/18596>\
**Category:** General Usage\
**Tags:** memory-allocation\
**Created:** [December 12, 2018, 2:10pm UTC](https://discourse.julialang.org/t/allocations-while-using-static-arrays-in-a-struct/18596 "2018-12-12T14:10:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bender9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bender9000/32/9464_2.png) [@bender9000](https://discourse.julialang.org/u/bender9000)\
**Post date:** [December 12, 2018, 2:10pm UTC](https://discourse.julialang.org/t/allocations-while-using-static-arrays-in-a-struct/18596/1 "2018-12-12T14:10:53Z")

</div>

It looks like Matrix operations while using StaticArrays in a struct is causing allocations. I am using StaticArrays.jl for Julia 0.6. I created a foobar example to illustrate what I encountered. Hopefully, someone can shed some light on what is happening and how to make the code allocation free.

```julia
using StaticArrays

struct Foo{N,T}
    x::SMatrix{N,N,T}
    y::SVector{N,T}
    Foo{N,T}(x::SMatrix{N,N,T}, y::SVector{N,T}) where {N,T} = new(x, y)
end

function mult{N,T}(x::Foo{N,T})
    z = x.x * x.y
    z
end

function measure_mult(x)
    @allocated mult(x)
end

function profilemult()
    x = SMatrix{2,2,Float32}(rand(2,2))
    y = SVector{2,Float32}(rand(2))

    f = Foo{2,Float32}(x,y)
    println("mult", '\t', measure_mult(f))
end

profilemult() # prints "mult 32"

function mult1{N,T}(x::SMatrix{N,N,T}, y::SVector{N,T})
    z = x * y
    z
end

function measure_mult1(x, y)
    @allocated mult1(x, y)
end

function profilemult1()
    x = SMatrix{2,2,Float32}(rand(2,2))
    y = SVector{2,Float32}(rand(2))
    
    println("mult1", '\t', measure_mult1(x,y))
end

profilemult1() # prints "mult1 0"

```

I am new to Julia but using `@code_warntype` on `mult` seems to indicate some kind of type stability.

```julia
Variables:
  #self# <optimized out>
  x::Foo{2,Float32}
  z::Any

Body:
  begin
      SSAValue(0) = (Core.getfield)(x::Foo{2,Float32}, :x)::StaticArrays.SArray{Tuple{2,2},Float32,2,L} where L
      z::Any = (StaticArrays._mul)((StaticArrays.Size)(SSAValue(0))::StaticArrays.Size{(2, 2)}, $(QuoteNode(Size(2,))), SSAValue(0), (Core.getfield)(x::Foo{2,Float32}, :y)::StaticArrays.SArray{Tuple{2},Float32,1,2})::Any # line 99:
      return z::Any
  end::Any

```

Any inputs are appreciated. I am working on an application that requires to keep state using static arrays which cannot have any allocations.

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [December 12, 2018, 2:35pm UTC](https://discourse.julialang.org/t/allocations-while-using-static-arrays-in-a-struct/18596/2 "2018-12-12T14:35:23Z")

</div>

The problem is that your `SMatrix` ist nof fully typed. The correct type is `SMatrix{N,M,T,L}` where `L=M*N`. The problem is that we currently cannot do arithmetic in the type domain. Therefore you have to manually specify the total number of entries in your matrix.

So changing things to (I also think that you don’t need the inner constructor)

```julia
struct Foo{N,T, L}
    x::SMatrix{N,N,T, L}
    y::SVector{N,T}
end

```

and fixing things at the other places should resolve your problems.

---

<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:** [December 12, 2018, 2:42pm UTC](https://discourse.julialang.org/t/allocations-while-using-static-arrays-in-a-struct/18596/3 "2018-12-12T14:42:46Z")

</div>

In situations like this you can check whether your types are concrete, with `isconcretetype`:

```julia
julia> isconcretetype(SMatrix{3,3,Float64})
false

julia> isconcretetype(SMatrix{3,3,Float64,9})
true

julia> isconcretetype(SVector{3,Float64})
true

```

---

<div class="post-metadata">

**Author:** ![bender9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bender9000/32/9464_2.png) [@bender9000](https://discourse.julialang.org/u/bender9000)\
**Post date:** [December 12, 2018, 2:56pm UTC](https://discourse.julialang.org/t/allocations-while-using-static-arrays-in-a-struct/18596/4 "2018-12-12T14:56:33Z")

</div>

Thanks. That fixed allocations.
