# Static Arrays Memory & Allocations

**URL:** <https://discourse.julialang.org/t/static-arrays-memory-allocations/106110>\
**Category:** Performance\
**Tags:** package, staticarrays\
**Created:** [November 12, 2023, 10:45am UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110 "2023-11-12T10:45:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 12, 2023, 10:45am UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/1 "2023-11-12T10:45:46Z")

</div>

Below two functions

> function generateLocalMatrixWorks(element::Element)  
> h = element.area  
> Iloc = SVector(element.e1, element.e1, element.e2, element.e2)  
> Jloc = SVector(element.e1, element.e2, element.e1, element.e2)  
> Aloc = SVector(1/h, -1/h, -1/h, 1/h)  
> return Iloc, Jloc, Aloc  
> end

> function generateLocalMatrix(element::Element)  
> h = element.area  
> aux1d = SVector(element.e1, element.e2)  
> Iloc = SVector{4}(repeat(aux1d,2,1))  
> Jloc = Iloc;  
> Aloc = SVector(1/h, -1/h, -1/h, 1/h)  
> return Iloc, Jloc, Aloc  
> end

The first does not allocate. The second one does. I fail to understand what I am doing wrong. Thanks.

Edit: this seems to be related to having to create static matrices in one go as referred to, e.g., here [Replacing normal arrays with static arrays - #2 by foobar\_lv2](https://discourse.julialang.org/t/replacing-normal-arrays-with-static-arrays/28230/2)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [November 12, 2023, 12:00pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/2 "2023-11-12T12:00:40Z")

</div>

Please format your code so we could read it easily. See here for more: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 12, 2023, 12:01pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/3 "2023-11-12T12:01:28Z")

</div>

Probably the `repeat` call is returning a vector, and thus allocating.

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 12, 2023, 12:11pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/4 "2023-11-12T12:11:52Z")

</div>

Thanks. I agree that the repeat function is likely the bad guy. I verified that comprehension does allocate as well. What alternative do you see?

---

<div class="post-metadata">

**Author:** ![tverho](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tverho](https://discourse.julialang.org/u/tverho)\
**Post date:** [November 12, 2023, 12:43pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/5 "2023-11-12T12:43:40Z")

</div>

Indeed `repeat` returns a regular array. You could do

```julia
Iloc = [aux1d; aux1d]

```

To get an SVector. The bracket syntax has a specialization for SVectors.

---

<div class="post-metadata">

**Author:** ![ConnectedSystems](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/connectedsystems/32/16815_2.png) [@ConnectedSystems](https://discourse.julialang.org/u/ConnectedSystems)\
**Post date:** [November 12, 2023, 12:45pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/6 "2023-11-12T12:45:21Z")

</div>

FYI I prefer `fill` over repeat in 99% of cases as it does not allocate. IIRC it doesn’t create an intermediate array, but may not be appropriate in this case.

---

<div class="post-metadata">

**Author:** ![ConnectedSystems](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/connectedsystems/32/16815_2.png) [@ConnectedSystems](https://discourse.julialang.org/u/ConnectedSystems)\
**Post date:** [November 12, 2023, 12:45pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/7 "2023-11-12T12:45:42Z")

</div>

This is an elegant answer!

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 12, 2023, 1:05pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/8 "2023-11-12T13:05:34Z")

</div>

Thanks! How do I find the other operations that have a specialization for SVectors?

---

<div class="post-metadata">

**Author:** ![tverho](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tverho](https://discourse.julialang.org/u/tverho)\
**Post date:** [November 12, 2023, 1:56pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/9 "2023-11-12T13:56:20Z")

</div>

I rely on experimentation. Just check the type of the output for various functions. Sometimes it matters how you call certain functions, for example `map(x->2*x, v)` returns an SVector if `v` is an SVector but `map(i->2*v[i], 1:length(v))` will return a Vector (not a surprise if you think about it). If you are curious, you can also check what code is being called with `@less`, for example try `@less [aux1d; aux1d]`.

---

<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:** [November 12, 2023, 2:39pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/10 "2023-11-12T14:39:21Z")

</div>

> [@lmiq](#):
>
> Probably the `repeat` call is returning a vector, and thus allocating.

> [@Domenico\_Lahaye](#):
>
> repeat(aux1d,2,1)

It probably returns a matrix. To get a vector it should be `repeat(aux1d, 2)` instead.

@Domenico_Lahaye, here you don’t want an `Array` at all, but in general you should be aware that Julia (unlike e.g. Matlab) distinguishes vectors from Nx1 matrices. Therefore, you use `zeros(N)`, `ones(N)`, `rand(N)` and `repeat(x, N)` instead of `zeros(N, 1)`, `ones(N,1)`, `rand(N, 1)` and `repeat(x, N, 1)`, etc.

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 12, 2023, 4:46pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/11 "2023-11-12T16:46:52Z")

</div>

@tverho Thx! How should I make sense of the output that @less generate?

@DNF Thx! Clear. Matlab habbits are sometimes hard to leave behind.

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 12, 2023, 4:59pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/12 "2023-11-12T16:59:16Z")

</div>

I would like to add local variables to my function. The first variant works fine. The second variant fails. I fail to understand this behavior.

```julia
function generateLocalMatrix(element::Element)
    h = element.area 
    e1 = element.e1
    e2 = element.e2
    Iloc = SVector(e1, e1, e2, e2) 
    Jloc = SVector(e1, e2, e1, e2) 
    # Kloc: local variable to be used in the future: goes fine - *no* memory allocations
    Kloc = SVector(1., 2., 3., 4.) 
    Aloc = SVector(1/h, -1/h, -1/h, 1/h) 
    return Iloc, Jloc, Aloc
end

```

```julia
function generateLocalMatrix(element::Element)
    h = element.area 
    e1 = element.e1
    e2 = element.e2
    Iloc = SVector(e1, e1, e2, e2) 
    Jloc = SVector(e1, e2, e1, e2) 
    # Kloc: local variable to be used in the future: goes fine - *no* memory allocations
    Kloc = SVector(1., 2., 3., 4.)
    # Lloc: another local variable to be used in the future: fials - causes memory allocations
    Lloc = SMatrix{2,2}([1. 2.;3. 4.])  
    Aloc = SVector(1/h, -1/h, -1/h, 1/h) 
    return Iloc, Jloc, Aloc
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 12, 2023, 5:20pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/13 "2023-11-12T17:20:58Z")

</div>

> [@Domenico\_Lahaye](#):
>
> `[1. 2.;3. 4.]`

This defined a matrix, explicitly, and causes the allocation.

---

<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:** [November 12, 2023, 5:38pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/14 "2023-11-12T17:38:19Z")

</div>

> [@Domenico\_Lahaye](#):
>
> `SMatrix{2,2}([1. 2.;3. 4.])`

Try

```julia
SMatrix{2,2}(1.0, 3.0, 2.0, 4.0)

```

or

```julia
@SMatrix [1.0 2.0; 3.0 4.0]

```

or

```julia
SA[1.0 2.0; 3.0 4.0] 

```

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 12, 2023, 6:04pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/15 "2023-11-12T18:04:23Z")

</div>

Thx again! Amazing stuff!

---

<div class="post-metadata">

**Author:** ![tverho](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tverho](https://discourse.julialang.org/u/tverho)\
**Post date:** [November 12, 2023, 6:56pm UTC](https://discourse.julialang.org/t/static-arrays-memory-allocations/106110/16 "2023-11-12T18:56:15Z")

</div>

`@less` shows the source file where the function is defined, at the function definition. It’s maybe more clear if you use it with a regular function. `[aux1d; aux1d]` is translated by Julia directly into `vcat(aux1d, aux1d)` so that’s why you see the definition of (one method for) `vcat`.
