# How do I convert a large Array to a StaticArray

**URL:** <https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454>\
**Category:** General Usage\
**Tags:** question, staticarrays\
**Created:** [August 21, 2024, 1:54pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454 "2024-08-21T13:54:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupemba](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@lupemba](https://discourse.julialang.org/u/lupemba)\
**Post date:** [August 21, 2024, 1:54pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/1 "2024-08-21T13:54:51Z")

</div>

What is the best way to convert a larger array to a StaticArray. I have tried the following which works fine for a small array but not for a medium size.

```julia
using StaticArrays

# this works
A1 = zeros(UInt16, (3, 4, 5));
@time S1 = SArray{Tuple{3, 4, 5}, UInt16, 3, length(A1)}(A1);

# this hangs and does not finish in 10 min
A2 = zeros(UInt16, (64, 64, 30));
@time S2 = SArray{Tuple{64, 64, 30}, UInt16, 3, length(A2)}(A2)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 21, 2024, 2:01pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/2 "2024-08-21T14:01:44Z")

</div>

From the docs:

Note that in the current implementation, working with large `StaticArray` s puts a lot of stress on the compiler, and becomes slower than `Base.Array` as the size increases. A very rough rule of thumb is that you should consider using a normal `Array` for arrays larger than 100 elements.

---

<div class="post-metadata">

**Author:** ![lupemba](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@lupemba](https://discourse.julialang.org/u/lupemba)\
**Post date:** [August 21, 2024, 2:04pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/3 "2024-08-21T14:04:14Z")

</div>

Thank you for the answer. I will have to refactor some code then. I liked to use the static arrays because I could have the size as part of the type but I will just find another way to handle it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 21, 2024, 2:05pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/4 "2024-08-21T14:05:39Z")

</div>

> [@lupemba](#):
>
> I liked to use the static arrays because I could have the size as part of the type

See also [What's the deal with StaticArrays? - #2 by stevengj](https://discourse.julialang.org/t/whats-the-deal-with-staticarrays/115816/2)

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 21, 2024, 2:42pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/5 "2024-08-21T14:42:36Z")

</div>

In case it’s relevant to your use case, it’s okay to have large `Arrays` of small-ish `StaticArrays`. For example, `zeros(SVector{64, UInt16}, 64, 30)` (i.e., a `Matrix{<:SVector{64}}`) is still within the usable size for `SVector`. A common example of this pattern is storing large sets of 3D coordinates as a `Vector{SVector{3,Float64}}`.

Although `zeros(SMatrix{64, 64, UInt16}, 30)` is too large a `SMatrix` to be useful, so this pattern only goes so far. It would probably compile in a mostly-usable amount of time, but its performance would be similar to or worse than a similarly-sized `Matrix`.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [August 21, 2024, 2:56pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/6 "2024-08-21T14:56:47Z")

</div>

> [@lupemba](#):
>
> I will have to refactor some code then. I liked to use the static arrays because I could have the size as part of the type but I will just find another way to handle it.

Did you consider to use a SizedArray? [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/stable/api/#StaticArraysCore.SizedArray)

---

<div class="post-metadata">

**Author:** ![lupemba](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@lupemba](https://discourse.julialang.org/u/lupemba)\
**Post date:** [August 21, 2024, 3:01pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/7 "2024-08-21T15:01:10Z")

</div>

Thanks for the answers. For context I am implementing a data reader [EUMETlab / Cross-Cutting Tools / MetopNative.jl · GitLab](https://gitlab.eumetsat.int/eumetlab/cross-cutting-tools/MetopNative.jl)

I was autogenerating structs for each record type and many of the fields are fixed sized Arrays. I was using StaticArrays for these fields so the record type contained all necessary information to read the record from disk.

> [@ufechner7](#):
>
> Did you consider to use a SizedArray? [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/stable/api/#StaticArraysCore.SizedArray)

Sized Array could be the answer for this. I will try it out. Alternatively I will just switch to regular arrays and pipe the array sizes around separately.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 21, 2024, 3:11pm UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/8 "2024-08-21T15:11:16Z")

</div>

> [@lupemba](#):
>
> Alternatively I will just switch to regular arrays and pipe the array sizes around separately.

A regular array has a size attached, just not as part of the type — just call [`size(A)`](https://docs.julialang.org/en/v1/base/arrays/#Base.size). You don’t need to pass the array size separately.

Why do you want the size to be part of the _type_, as opposed to being stored in the object?

---

<div class="post-metadata">

**Author:** ![lupemba](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@lupemba](https://discourse.julialang.org/u/lupemba)\
**Post date:** [August 22, 2024, 8:39am UTC](https://discourse.julialang.org/t/how-do-i-convert-a-large-array-to-a-staticarray/118454/9 "2024-08-22T08:39:10Z")

</div>

> [@ufechner7](#):
>
> Did you consider to use a SizedArray? [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/stable/api/#StaticArraysCore.SizedArray)

I have just tried SizedArray. It is easy to construct large SizedArrays but I quick run into compile issues again when I try to use the large SizedArrays. I am sure I can find workaround for many of the things but right now it is cleaner just to switch to regular Arrays. Here is a dummy example:

```julia
using StaticArrays

# Construction works fine now 
A = zeros(UInt16, (10, 10, 10));
T = SizedArray{Tuple{size(A)...}, eltype(A), ndims(A), ndims(A), typeof(A)}
@time S = T(A);
S2 = T(copy(A));

# using the SizedArray uses a lot of compile time 
@time all(S .== S2) # 8.2 seconds, 100 % compile time
@time all(A .== S2) # 13.12 seconds, 100 % compile time

```

> [@stevengj](#):
>
> A regular array has a size attached, just not as part of the type — just call [`size(A)`](https://docs.julialang.org/en/v1/base/arrays/#Base.size). You don’t need to pass the array size separately.
> 
> Why do you want the size to be part of the _type_, as opposed to being stored in the object?

I am reading several fixed sized array fields from a binary file format. Reading these fields I need to know that they are arrays, the element type and the size of the array. It was therefore convenient to have the size as part of the type.
