# Status of immutable variable-size arrays?

**URL:** <https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942>\
**Category:** General Usage\
**Created:** [January 18, 2024, 8:45am UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942 "2024-01-18T08:45:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [January 18, 2024, 8:45am UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/1 "2024-01-18T08:45:02Z")

</div>

Hello, which is the situation for arrays that are immutable but their size is known only at run time ?

I see there is an [old discussion here](https://discourse.julialang.org/t/making-an-immutable-array-type/31129), and then some “experimental” PR that has been rebased several times ([here](https://github.com/JuliaLang/julia/pull/31630) and [here](https://github.com/JuliaLang/julia/pull/41777))… but as of today, is there a way I can “gain” performances if I “promise” that once I create an array I don’t modify it further ?

---

<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:** [January 18, 2024, 10:06am UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/3 "2024-01-18T10:06:44Z")

</div>

Somewhat related: what about mutable, statically sized arrays, like `std::array` in C++? They are not as limited in size as `StaticArrays.MArray`s, which are not particularly great beyond roughly 100 elements.

Having recently started with C++, I quite like how `std::array` and `std::vector` compose with the `const` keyword, to create static/dynamic, mutable/immutable arrays.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 18, 2024, 11:15am UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/4 "2024-01-18T11:15:08Z")

</div>

> [@DNF](#):
>
> Somewhat related: what about mutable, statically sized arrays, like `std::array` in C++? They are not as limited in size as `StaticArrays.MArray`s, which are not particularly great beyond roughly 100 elements.

What about [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api/#StaticArraysCore.SizedArray)

> [@DNF](#):
>
> Having recently started with C++, I quite like how `std::array` and `std::vector` compose with the `const` keyword, to create static/dynamic, mutable/immutable arrays.

Indeed, it would be nice to able to do that.

But this is playing with fire! [UB](https://stackoverflow.com/questions/25209838/is-this-undefined-behavior-with-const-cast) is nearby (and it is unavoidable – the undefined behavior is the point of declaring an array `const`, this is what permits more compiler optimizations)

Current appetite seems to be low for even more ways to create very hard to debug UB in order to eke out some performance in some cases?

Otherwise CUDA.jl `unsafe_free!` would be in Base, and would be extensively used for temporaries that are not supposed to escape (I’d be heavily in favor of this one!). And more extensive noalias annotations would also exist.

(also we don’t have UBsan for julia. That’s OK because UB-caused bugs are relatively rare in julia; if it became common then we’d need more tools)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 18, 2024, 11:24am UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/5 "2024-01-18T11:24:06Z")

</div>

> [@DNF](#):
>
> Somewhat related: what about mutable, statically sized arrays, like `std::array` in C++?

That should be the same as `Memory{T}` [https://github.com/JuliaLang/julia/pull/51319](https://github.com/JuliaLang/julia/pull/51319), no?

---

<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:** [January 18, 2024, 11:45am UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/6 "2024-01-18T11:45:23Z")

</div>

> [@DNF](#):
>
> what about mutable, statically sized arrays, like `std::array` in C++? They are not as limited in size as `StaticArrays.MArray`s, which are not particularly great beyond roughly 100 elements.

What is the reason exactly for `MArray`s being less performant at that size? Is it any different from statically sized, “mutable” `std::array`s on the stack, I’ve also read that large ones are avoided because of stack overflows? (Mutable in quotes because variable mutability is semantically different from Julia’s instance mutability, though I imagine another language could compose immutability with default-mutable types, or more likely vice versa, like `const` does with variables).

---

<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:** [January 18, 2024, 12:25pm UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/7 "2024-01-18T12:25:21Z")

</div>

I didn’t consider `StaticArrays` (even `MArray`) because I assumed they weren’t useful except for small sizes. But, perhaps I’m wrong?

> [@Sukera](#):
>
> That should be the same as `Memory{T}`

I thought that was just a new backing for `Array`, with dynamic size…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 18, 2024, 12:59pm UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/8 "2024-01-18T12:59:04Z")

</div>

> [@DNF](#):
>
> I thought that was just a new backing for `Array`, with dynamic size…

It is the new backing for `Array`, but crucially, the size of a `Memory` itself is _fixed_. You can’t change that after its creation. When a `Vector` is resized, what used to happen under the hood is that a new block of memory would be allocated and the existing data would be copied over (all happening in C internals, and handwaving some details of when this resizing doesn’t actually lead to a new allocation). This `Memory` basically takes that job over, and when a `Vector` is now resized, a new `Memory` object is allocated. All the resizing logic of `Vector` now lives in Julia instead of C, allowing (in principle) the compiler to be smarter about the allocation of that `Memory` (potentially reusing the existing one entirely, just making the initial allocation of that bigger, etc… lots of room for optimizations there).

Of course, you’re free to use `Memory` in your own code for things that don’t need to be able to change size. If you don’t ever `push!` or similar into your `Vector`, you should be able to more or less use `Memory` instead. It’s a low-level-ish building block.

---

<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:** [January 18, 2024, 2:21pm UTC](https://discourse.julialang.org/t/status-of-immutable-variable-size-arrays/108942/9 "2024-01-18T14:21:15Z")

</div>

Interesting! Do you think this could also be used in immutable data structures in that the compiler could make optimizations? Could this replace StaticArrays?
