Why A[:,1] .= x allocates?

Why the following code allocates?

function f(A,x) 
    A[:,1] .= x;
    return nothing
end

julia> A = randn(5,5); x = randn(5);

julia> @btime f($A, $x)
  30.941 ns (1 allocation: 48 bytes)
2 Likes

Because it is passing around a view and currently objects with pointers (like a view) needs to be allocated on the heap unless they can be proven to not escape which right now requires all uses to be inlined (although maybe changing soon, https://github.com/JuliaLang/julia/pull/33886).

6 Likes