# Static array or tuple allocate at construction

**URL:** <https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447>\
**Category:** Performance\
**Tags:** memory-allocation, staticarrays, ntuple\
**Created:** [March 11, 2024, 8:38am UTC](https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447 "2024-03-11T08:38:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [March 11, 2024, 8:38am UTC](https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447/1 "2024-03-11T08:38:49Z")

</div>

I have some code that allocates on the heap (and shouldn’t) while constructing a `StaticArray`. The hunt leads me to this MWE:

```julia
using StaticArrays, BenchmarkTools
@btime SVector{4,Float64}(2. *i for i=1:4) # good, no allocation
@btime ntuple(i->2. *i,4) # good too, no allocation

v = SVector{4,Float64}(1.,2.,3.,4.)
@btime SVector{4,Float64}(v[i] for i=1:4) # but why does this allocate? 
@btime ntuple(i->v[i],4) # and this too?

```

Why does accessing an array element allocate? To check that this is not a measurement artifact, I package the allocating code in a on-liner function and @btime 'd it.

Of course the above MVE is nonsensical - I could just copy `v`. Nonsensical or not, I do not understand what causes the allocation, and understanding this would probably help me correct my real code.

NB: I’m still on Julia 1.9, for no good reason… Is that an issue here?

🙂

---

<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:** [March 11, 2024, 9:00am UTC](https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447/2 "2024-03-11T09:00:26Z")

</div>

See the performance tips page in the manual: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

In particular, you’re accessing a global untyped variable (`v`), and there are possibly also issues with the way you capture it in the closure. Both issues are covered on that page. Also possibly check the BenchmarkTools.jl Readme and docs, they also cover these issues.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [March 11, 2024, 9:11am UTC](https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447/3 "2024-03-11T09:11:28Z")

</div>

Good grief, I fell for this one again 😛. Indeed, just declaring `const`

```julia
const v = SVector{4,Float64}(1.,2.,3.,4.)

```

eliminates all allocations in the above MVE.

Which also means my MVE did not capture the actual problem of my original code. I’ll be back.
