# Allocations MVectors

**URL:** https://discourse.julialang.org/t/allocations-mvectors/121818
**Category:** Performance
**Tags:** staticarrays
**Created:** [October 27, 2024, 9:12am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818 "2024-10-27T09:12:22Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)
#### Post date: [October 27, 2024, 9:12am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/1 "2024-10-27T09:12:22Z")

</div>

Why does this:

```julia
m! = factorial(3)
@btime MVector{$m!, Float64}(undef);
576.677 ns (10 allocations: 512 bytes)

```

allocate ten times as much as this:

```julia
@btime MVector{6, Float64}(undef);
4.148 ns (1 allocation: 64 bytes)

```

I would like to have the dimension of the MVector as a variable in my function. Yet I will only create these MVectors once.

---

<div class="post-metadata">

### Author: ![Zentrik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zentrik/32/35409_2.png) [@Zentrik](https://discourse.julialang.org/u/Zentrik)
#### Post date: [October 27, 2024, 9:17am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/2 "2024-10-27T09:17:29Z")

</div>

Probably because the `$m!` acts to make it so the size of the `MVector` is unknown at compile time.

---

<div class="post-metadata">

### Author: ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)
#### Post date: [October 27, 2024, 9:18am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/3 "2024-10-27T09:18:36Z")

</div>

But the problem remains also if I set

```julia
m!::Int = factorial(3)
@btime MVector{$m!, Float64}(undef);

```

---

<div class="post-metadata">

### Author: ![Zentrik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zentrik/32/35409_2.png) [@Zentrik](https://discourse.julialang.org/u/Zentrik)
#### Post date: [October 27, 2024, 9:19am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/4 "2024-10-27T09:19:32Z")

</div>

Right because the value of `m!` is what’s important, not it’s type. If you remove the `$` in front of `m!` you should see the problem go away, as then the compiler can see that `m!` has constant value `6`.

EDIT: actually my suggestion probably won’t work if you’re not doing this in a function.

---

<div class="post-metadata">

### Author: ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)
#### Post date: [October 27, 2024, 9:20am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/5 "2024-10-27T09:20:29Z")

</div>

Unfortunately, it still remains. Can you please write the solution you have in mind.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [October 27, 2024, 9:58am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/6 "2024-10-27T09:58:30Z")

</div>

> [@AdaemmerP](#):
>
> the dimension of the MVector as a variable in my function

Can you get away with the dimension being a method parameter instead? If the dimension varies a lot at runtime this would only shuttle the same inefficiency outside the method.

---

<div class="post-metadata">

### Author: ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)
#### Post date: [October 27, 2024, 10:08am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/7 "2024-10-27T10:08:49Z")

</div>

Can you please elaborate on this. I am not sure whether I fully grasped the question. `m` ideally shall be an integer that enters my function “foo(m, …)”. Within that function for a particular method I would like to use

```julia
m!::Int = factorial(m)
MVector{m!, Float64}(undef)

```

Now I see the problem that “m” will only be known at runtime. For my particular problem I can also use “normal” vectors as the speed benefit of MVectors in my particular case is not that big.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [October 27, 2024, 10:14am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/8 "2024-10-27T10:14:24Z")

</div>

I meant something like `foo(::Val{m!}) where m! = ...` but that function barrier really only helps if you can compute a `m!` value before doing most of the work in a `foo` call compiled specifically for said value.

---

<div class="post-metadata">

### Author: ![AdaemmerP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adaemmerp/32/52351_2.png) [@AdaemmerP](https://discourse.julialang.org/u/AdaemmerP)
#### Post date: [October 27, 2024, 10:15am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/9 "2024-10-27T10:15:43Z")

</div>

Ah ok, thanks! I think about that.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [October 27, 2024, 10:17am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/10 "2024-10-27T10:17:45Z")

</div>

There’s a section in the Performance Tips about [function barriers](https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions) with a working example, maybe that’ll help as well. It’s not exactly the integer-valued parameter in your `MVector` case, but it does involve a type parameter computed at runtime. In that example however, the array is still type-unstable where it is instantiated outside the kernel function, rather than being stably instantiated inside the kernel function given a method parameter.

---

<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: [October 27, 2024, 10:38am UTC](https://discourse.julialang.org/t/allocations-mvectors/121818/11 "2024-10-27T10:38:56Z")

</div>

Also see this section in the Performance Tips: [The dangers of abusing multiple dispatch (aka, more on types with values-as-parameters)](https://docs.julialang.org/en/v1/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters))
