# Allocations in construction of composite type with StaticArray field

**URL:** <https://discourse.julialang.org/t/allocations-in-construction-of-composite-type-with-staticarray-field/12461>\
**Category:** Performance\
**Tags:** question\
**Created:** [July 18, 2018, 12:01pm UTC](https://discourse.julialang.org/t/allocations-in-construction-of-composite-type-with-staticarray-field/12461 "2018-07-18T12:01:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)\
**Post date:** [July 18, 2018, 12:01pm UTC](https://discourse.julialang.org/t/allocations-in-construction-of-composite-type-with-staticarray-field/12461/1 "2018-07-18T12:01:33Z")

</div>

Hello,

I’m tracking down allocations in a performance critical part of a code, where I use composite types with `StaticArray`s fields. I’ve noticed that initializing instances of such types triggers some allocations that I would like to eliminate (if possible).

Here is a minimal example that illustrate the issue I’m referring to.

```julia
using StaticArrays, BenchmarkTools

struct MyStaticVector{N}
    data::StaticVector{N, Int}
end

struct MyNTuple{N}
    data::NTuple{N, Int}
end

foo() = MyStaticVector(@SVector [1, 2])
bar() = MyNTuple((1, 2))

@btime foo()
@btime bar()

```

Running this script yields the following

```bash
$ julia alloc.jl
  9.713 ns (2 allocations: 48 bytes)
  1.452 ns (0 allocations: 0 bytes)

```

My understanding of statically-sized arrays is that they allow alleviating allocations altogether (do not hesitate correcting me if I’m wrong). Am I missing something?

Is there a way to rewrite `foo` to achieve 0 allocation? I do not know much about constructors julia, but could it be related?

Any tips will be very much appreciated. Thanks!

V.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 18, 2018, 12:04pm UTC](https://discourse.julialang.org/t/allocations-in-construction-of-composite-type-with-staticarray-field/12461/2 "2018-07-18T12:04:28Z")

</div>

StaticVector is an abstract type.  
Try replacing it by the concrete type SVector in the type definition.

---

<div class="post-metadata">

**Author:** ![vlc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vlc/32/4705_2.png) [@vlc](https://discourse.julialang.org/u/vlc)\
**Post date:** [July 18, 2018, 2:13pm UTC](https://discourse.julialang.org/t/allocations-in-construction-of-composite-type-with-staticarray-field/12461/3 "2018-07-18T14:13:40Z")

</div>

That did it, thanks a lot!

(Just found a related issue [here](https://github.com/JuliaArrays/StaticArrays.jl/issues/352).)
