# Getting Allocations with Static Arrays

**URL:** <https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744>\
**Category:** General Usage\
**Tags:** memory-allocation, staticarrays\
**Created:** [February 3, 2022, 5:48pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744 "2022-02-03T17:48:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [February 3, 2022, 5:48pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744/1 "2022-02-03T17:48:08Z")

</div>

Hello,

I am trying to optimize some code and have some questions on behavior I am observing related to static arrays. I have read that if you have lots of small arrays (less than 100 elements) then static arrays are one option to increase performance of your code. Since they are stack allocated they are faster to access (I think).

I have some code where I need to create a 3-element vector in each iteration of a hot loop. Right now I have a function that looks like this (which has zero allocations)

```julia
function compute_coefficients!(cache_array, MyListofArgs...)
    # Compute individual elements
    a1 = ...
    a2 = ...
    a3 = ...
    # Put into cache array
    cache_array[1] = a1+a2
    cache_array[2] = a2 - a3
    cache_array[3] = a3*a2-a1
end

```

I thought this would be a good use case for a static arrays since it is a small array (maybe this is not a good use case, hoping someone can correct me if so). My initial impression was that since static arrays are stack allocated returning one should be like returning an `Int` or a `Float64`. So I tried to implement this instead

```julia
function compute_coefficients(MyListofArgs...)
    # Compute individual elements
    a1 = ...
    a2 = ...
    a3 = ...
    # Put into SVector
    tmp = SVector{3,Float64}(a1+a2, a2 - a3, a3*a2-a1)
    return tmp
end

```

Now I have checked using `@allocated` and the line starting with `tmp = ...` inside `compute_coefficients` does not allocate (which is consistent with my expectations). But when I call this function from one level up (from inside my loop) I **do** get an allocation.

```julia
function outer_func()
# Lots of code in here
# Inside a hot inner loop I am calling
    tmp = compute_coeff(MyListofArgs...) # <-- @allocated says this line allocates
end

```

What I don’t understand is why in the outer function I am getting an allocation. I saw from this [post](https://discourse.julialang.org/t/staticarrays-and-allocations/62123) that static arrays do not guarantee a stack allocation. But I don’t understand why there is no heap allocation inside `compute_coeff` but there is when I call it from the outer function.

I have also tried adding type annotations to both the function output (i.e., `compute_coefficients(MyListofArgs...)::SVector{3,Float64}`) and the `tmp` in the outer function (i.e., `tmp::SVector{3,Float64}`) but it did not make a difference.

---

<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:** [February 3, 2022, 6:00pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744/2 "2022-02-03T18:00:45Z")

</div>

I think you have excluded too much of your code for us to be able to tell what is going on. Here:

> [@TI36XPro](#):
>
> ```julia
> function outer_func()
> # Lots of code in here
> # Inside a hot inner loop I am calling
> tmp = compute_coeff(MyListofArgs...) # <-- @allocated says this line allocates
> end
> 
> ```

for example, I can’t tell where `MyListofArgs` comes from. Is it a global variable? That would explain it.

You have made your example minimal, but not working. It should be a Minimal Working Example, so people can copy and run your code and analyze it. Creating an MWE can be a bit of work, but you often end up understanding your own problem better, and perhaps solve it yourself in the process.

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [February 3, 2022, 6:05pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744/3 "2022-02-03T18:05:41Z")

</div>

To answer your question - there are no global arguments.

I will try to work up an MWE later today, it may be difficult as even outer\_func() is nested within other function calls.

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [February 3, 2022, 6:15pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744/4 "2022-02-03T18:15:53Z")

</div>

Does `tmp = @SVector([a1+a2, a2 - a3, a3*a2-a1])` change anything ?

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [February 3, 2022, 6:44pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744/5 "2022-02-03T18:44:18Z")

</div>

Hm - what do you mean?  
I thought it was creating a new static vector and assigning it to a variable named `tmp`.

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [February 3, 2022, 6:49pm UTC](https://discourse.julialang.org/t/getting-allocations-with-static-arrays/75744/6 "2022-02-03T18:49:32Z")

</div>

Maybe I misread the original post. As @DNF said your best bet here is to work out a mwe reproducing the allocations.
