Memory allocation due to assignment in a fixed size mutable array


I am writing a function that stores locally a temporary set of values saved for a later computation. During “compile” time I know the size of the array. To avoid memory allocation, I am using a mutable array from the StaticArray package. See example code below that illustrates a random assignment ( in the actual code, the assignment involves a more elaborate computation).

function test1()
function test2()
    for i in 1:10000

Timing the above results in memory allocation.

@time test2()
  0.000309 seconds (10.00 k allocations: 625.156 KiB)

Is that expected? Since I know the array size at “compile time” should not there be a way to eliminate this memory cost? I am looking for an object that behaves like std::array from c++. What is the “Julian” way?



This doesn’t allocate on latest Julia nightly (after fixing the deprecation warning).

On 0.6, it’s not the assignment that allocates, it’s creating the MVector. The rule of thumb for 0.6 is that creating non-isbits structs (structs that are either mutable or contain references to other values) always allocates on the heap. The upcoming Julia release is much better at eliminating such allocations.