Allocation during construction of compile-time sized structure

I have a simple, immutable structure that wraps a statically sized matrix. When I construct it with an initialization value, it causes a memory allocation. I expected there to be no such allocation. Should there be an allocation, or is this an issue with the compiler? Here is the MWE:

using BenchmarkTools
using StaticArrays

struct MyMat
    m::SMatrix{3, 3, Float64}

function test()
    A = @SMatrix randn(3, 3)
    @btime B = MyMat($A)


4.750 ns (1 allocation: 80 bytes)

The SMatrix type needs a fourth type parameter (which is redundant):

1 Like