# Why is accessing an SMatrix from a struct allocating?

**URL:** <https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016>\
**Category:** Performance\
**Created:** [July 24, 2025, 5:30pm UTC](https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016 "2025-07-24T17:30:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![r0uv3n](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r0uv3n/32/212257_2.png) [@r0uv3n](https://discourse.julialang.org/u/r0uv3n)\
**Post date:** [July 24, 2025, 5:30pm UTC](https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016/1 "2025-07-24T17:30:38Z")

</div>

Considering the following simple MWE:

```julia
using StaticArrays, BenchmarkTools
struct SomeStruct
    matrix::SMatrix{3,3,Float64}
end

function testing_allocation(g::MMatrix{3,3,Float64}, struct_matrix::SomeStruct)
  fill!(g, zero(Float64))
  g .+= struct_matrix.matrix
end

@btime testing_allocation(g, struct_matrix) setup = (g = MMatrix{3,3,Float64}(undef); struct_matrix = SomeStruct(rand(SMatrix{3,3,Float64})))

```

Output:

```julia
  458.675 ns (4 allocations: 304 bytes)
3×3 MMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
 0.981097 0.90833 0.795176
 0.915934 0.482159 0.0795738
 0.250249 0.906489 0.864033

```

Removing the intermediate struct of using vectors instead of matrices fixes this issue. Am I missing something obvious?

---

<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:** [July 24, 2025, 5:32pm UTC](https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016/2 "2025-07-24T17:32:15Z")

</div>

> [@r0uv3n](#):
>
> `matrix::SMatrix{3,3,Float64}`

This is an abstract type. The full concrete type is `matrix::SMatrix{3,3,Float64,9}`.

See also many previous discussions, such as [SMatrix{2, 2, Float64} is not a concrete type?](https://discourse.julialang.org/t/smatrix-2-2-float64-is-not-a-concrete-type/104638) and [Computing Inverse of a stack of matrices - #19 by tchr](https://discourse.julialang.org/t/computing-inverse-of-a-stack-of-matrices/45580/19)

---

<div class="post-metadata">

**Author:** ![r0uv3n](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r0uv3n/32/212257_2.png) [@r0uv3n](https://discourse.julialang.org/u/r0uv3n)\
**Post date:** [July 24, 2025, 5:33pm UTC](https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016/3 "2025-07-24T17:33:57Z")

</div>

Oh my god, thank you, of course. I assume there is no way to define `Some_Struct{n}` in such a way that `matrix` is always a `SMatrix{n,n,Float64,n*n}`? I just need to add a second type parameter and propagate that up through any structures that include this one?

---

<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:** [July 24, 2025, 5:48pm UTC](https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016/4 "2025-07-24T17:48:11Z")

</div>

> [@r0uv3n](#):
>
> I just need to add a second type parameter and propagate that up through any structures that include this one?

Unfortunately yes, because Julia’s type system cannot do arithmetic in type parameters.

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [July 24, 2025, 7:04pm UTC](https://discourse.julialang.org/t/why-is-accessing-an-smatrix-from-a-struct-allocating/131016/5 "2025-07-24T19:04:08Z")

</div>

Depending on what you want to do, it might be possible to make the struct a bit more convenient for your purposes. For example, defining

```julia
using StaticArrays
struct SomeStruct{N, T<:SMatrix{N,N,Float64}}
        matrix::T
end

```

allows you to do the following:

```julia
a = @SMatrix rand(3,3)
b = @SMatrix rand(4,4)
c = @SMatrix rand(4,3)
sa = SomeStruct(a) # Works fine
sb = SomeStruct(b) # Works fine
sc = SomeStruct(c) # Errors

```

This may or may not be easier to propagate up your structures depending on your use case.
