I have this bit of code. I managed to avoid all allocations.
function compute!(csmatout, XYZ, tangents, feid, qpid)
center = (0.0, 0.0, 0.0)
xyz = (XYZ[1], XYZ[2], XYZ[3])
csmatout[:, 1] .= xyz .- center
csmatout[3, 1] = 0.0
# csmatout[:, 1] ./= norm(@view(csmatout[:, 1]))
nm = norm(@view(csmatout[:, 1]))
for k in 1:3; csmatout[k, 1] /= nm; end
csmatout[:, 3] .= (0.0, 0.0, 1.0)
cross3!(@view(csmatout[:, 2]), @view(csmatout[:, 3]), @view(csmatout[:, 1]))
# csmatout[:, 2] ./= norm(@view(csmatout[:, 2]))
nm = norm(@view(csmatout[:, 2]))
for k in 1:3; csmatout[k, 2] /= nm; end
return csmatout
end
But, I had to replace the lines that are commented out. They would allocate. In other words, this allocates.
function compute!(csmatout, XYZ, tangents, feid, qpid)
center = (0.0, 0.0, 0.0)
xyz = (XYZ[1], XYZ[2], XYZ[3])
csmatout[:, 1] .= xyz .- center
csmatout[3, 1] = 0.0
csmatout[:, 1] ./= norm(@view(csmatout[:, 1]))
# nm = norm(@view(csmatout[:, 1]))
# for k in 1:3; csmatout[k, 1] /= nm; end
csmatout[:, 3] .= (0.0, 0.0, 1.0)
cross3!(@view(csmatout[:, 2]), @view(csmatout[:, 3]), @view(csmatout[:, 1]))
csmatout[:, 2] ./= norm(@view(csmatout[:, 2]))
# nm = norm(@view(csmatout[:, 2]))
# for k in 1:3; csmatout[k, 2] /= nm; end
return csmatout
end
I thought that the compiler would be able to divide the column without allocating. What is it that I’m misunderstanding here?
Thanks!