# Why does SMatrix allocate in this case?

**URL:** https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808
**Category:** Performance
**Tags:** staticarrays
**Created:** [April 7, 2025, 4:06pm UTC](https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808 "2025-04-07T16:06:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [April 7, 2025, 4:06pm UTC](https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808/1 "2025-04-07T16:06:05Z")

</div>

Hello everyone,

Suppose I have:

```julia
using StaticArrays

using Chairmarks

N = 10
Dimensions = 2
DimensionsPlus = Dimensions + 1
FloatType = Float64

SVectors = rand(SVector{DimensionsPlus, FloatType}, N)
MVectors = rand(SMatrix{DimensionsPlus, DimensionsPlus, FloatType}, N)

function UpdateVectors(Vectors)
	tmp = rand(eltype(Vectors))
	for i in eachindex(Vectors)
		Vectors[i] += Vectors[i]
	end
end

@b UpdateSVector($SVectors)
@b UpdateSVector($MVectors)

```

Running this code gives:

```julia
julia> @b UpdateSVector($SVectors)
7.061 ns

julia> @b UpdateSVector($MVectors)
479.167 ns (20 allocs: 1.562 KiB)

```

In theory, everything is known at compile time as far as I know? If I split the SMatrix into 3 different SVectors, then I would get zero allocs. Is there a way I can get the SMatrix to work with zero allocations or a way to make a “view” using multiple vectors of SVector to represent a 3x3 matrix?

Kind regards

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 7, 2025, 4:13pm UTC](https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808/2 "2025-04-07T16:13:24Z")

</div>

The `SMatrix` type is abstract, you’re missing the last type parameter that is the total length. Try this instead `SMatrix{DimensionsPlus, DimensionsPlus, FloatType, DimensionsPlus*DimensionsPlus}`

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [April 7, 2025, 7:24pm UTC](https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808/3 "2025-04-07T19:24:54Z")

</div>

> [@baggepinnen](#):
>
> DimensionsPlus\*DimensionsPlus

I confirm, thanks a lot!

```julia
#Working code, some mistakes in post

using StaticArrays

using Chairmarks

N = 10
Dimensions = 2
DimensionsPlus = Dimensions + 1
FloatType = Float64

SVectors = rand(SVector{DimensionsPlus, FloatType}, N)
MVectors = rand(SMatrix{DimensionsPlus, DimensionsPlus, FloatType, DimensionsPlus*DimensionsPlus}, N)

function UpdateVectors(Vectors)
	tmp = rand(eltype(Vectors))
	for i in eachindex(Vectors)
		Vectors[i] += Vectors[i]
	end
end

@b UpdateVectors($SVectors)
@b UpdateVectors($MVectors)

```

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [April 8, 2025, 2:12pm UTC](https://discourse.julialang.org/t/why-does-smatrix-allocate-in-this-case/127808/4 "2025-04-08T14:12:34Z")

</div>

It seems that this shouldn’t be necessary. New issue opened [`zeros`, `rand`, `randn` could infer the length parameter of incompletely parameterized `StaticArray`s · Issue #1303 · JuliaArrays/StaticArrays.jl · GitHub](https://github.com/JuliaArrays/StaticArrays.jl/issues/1303).
