Insert value with fixed size/gc time

This pattern allocates the array [@view array_val ...; current_val] before it fills it in and copies it to array_val. If you instead write it as a loop there will be no allocations

for i in 2:ii
   array_val[i-i] = array_val[i]
end
array_val[ii] = current_val

And copyto! is not safe for shifting an array in this way, it says in the docs: “Behavior can be unexpected when any mutated argument shares memory with any other argument.”

2 Likes