Does norm() allocate?

I have the following code:

using KiteModels, StaticArrays, LinearAlgebra, BenchmarkTools
function test(vec)
    KiteModels.norm(vec)
end
vec=MVector{3}([1.0,2,3])

test(vec)
bytes=@allocated test(vec)
println("Allocate $bytes bytes!")
@benchmark test($vec)

The macro @allocated tells me my function would allocate 16 bytes, BenchmarkTools tells me I have zero allocations.

Which answer is correct?

Using LinearAlgebra.norm gives the same result, but it is slower (and more accurate).

The benchmark tools one.

To elaborate a little bit more, the reason is vec is a global non-constant variable:

const vec2 = vec
@allocated test(vec2) # should be zero
3 Likes