FixedSizeArrays.jl
FixedSizeArrays.jl
is a package for the Julia programming language which implements a variable-length array type (FixedSizeArray
): this is a fixed-size array, whose size is set at runtime rather than at compile time, and whose elements are mutable. This means the size of a FixedSizeArray
can not change after construction, and is amenable to be constant-propagated by the compiler when possible.
FixedSizeArray
supports the standard array interfaces, so things like broadcasting, matrix multiplication, other linear algebra operations, similar
, copyto!
or map
should just work.
Use the constructors to convert from other array types. Use collect_as
from the Collects.jl package to convert from arbitrary iterators.
Comparison with other array types
Here is a quick summary of high-level differences between some different array types:
Size set at... | Data backend | Growable | Mutable elements | |
---|---|---|---|---|
Base.Array | runtime | MemoryRef | ✅ | ✅ |
FixedSizeArray | runtime | Memory | ❌ | ✅ |
MArray | compile time | Tuple | ❌ | ✅ |
SArray | compile time | Tuple | ❌ | ❌ |
In the following sections we elaborate on the properties and strengths of the various types.
Array
from Base
While Base
's Array
is a convenient multi-dimensional container for continuous data, a fixed-size array is what is employed in most linear algebra applications, where the size of tensors typically does not change. Here is a comparison between Array
and FixedSizeArray
:
Array
is dynamic (or growable) which means the size of anArray
can be changed after construction. The size of aFixedSizeArray
cannot be changed;- the above property of
FixedSizeArray
s enables certain compiler optimizations in some cases where the size of aFixedSizeArray
is known at compile-time. The dynamic property ofArray
s limits the optimization opportunities, which makesArray
s less likely to be optimized as efficiently asFixedSizeArray
s.
You can see a showcase of performance/memory advantages of FixedSizeArray
over Array
in this evolving discussion.
MArray
from StaticArrays.jl
FixedSizeArray
is an alternative implementation to MArray
from StaticArrays.jl
.
Main differences between FixedSizeArray
and MArray
are:
FixedSizeArray
is based on theMemory
type introduced in Julia v1.11,MArray
is backed by tuples, which are not efficient for long arrays ($\gg 100$ elements);- the size of the array is part of the type parameters of
MArray
(it has to be set at compile time), this isn't the case forFixedSizeArray
, where the size is only a constant field of the data structure and can be set at runtime.
There are applications where it may be desirable to dispatch on the size of an array, in that case FixedSizeArray
would not be a good fit and you may consider using MArray
instead.
FixedSizeArray
is not a direct replacement for SArray
: in addition to having the size as part of the type parameters, SArray
is immutable, instead the elements of a FixedSizeArray
can be changed at any point.
What about the other package with same name?
This package is not related to a package with the same name by @SimonDanisch
. That, earlier, package was one of the StaticArrays.jl-like packages in the pre-v1 days of Julia: https://github.com/SimonDanisch/FixedSizeArrays.jl.