So we see 9 allocations. Could someone explain to me where these 9 allocations are in @code_typed (or @code_warntype, @code_llvm) and exactly what it means?
Sorry, I should be more clear. I am not asking why the allocations are happening, but “where” its happening in the compiled code. i.e. what are the commands in @code_typed or @code_llvm that allocate memory. I guess the example I have is a bad one but I don’t know of a good example.
I suppose I am asking someone to teach me how to read llvm and I am not at that level yet so I can revisit this question in a few months/years
which does show the jl_alloc_array_1d call. However, @code_whatever is only going to show you the given function and whatever was inlined into it. If the allocation happens to occur inside some function which was not inlined (which is very often the case), then the @code_whatever tools won’t necessarily show it:
julia> @noinline f() = rand(100)
f (generic function with 1 method)
julia> g() = f()
g (generic function with 1 method)
julia> @code_typed(g())
CodeInfo(
1 1 ─ %1 = invoke Main.f()::Array{Float64,1} │
└── return %1 │
) => Array{Float64,1}
As other people have already explained, at least some of those allocations happen in the @time macro and the rand(100) construction. The easiest way to get rid of those distractions is to use BenchmarkTools and precompute the input to the function.
julia> using BenchmarkTools
julia> function sum_of_diff_1_loop(x)
s = zero(eltype(x))
for k = 2:length(x)
s += x[k] - x[k - 1]
end
return s
end
sum_of_diff_1_loop (generic function with 1 method)
julia> x = rand(100);
julia> @btime sum_of_diff_1_loop($x);
80.965 ns (0 allocations: 0 bytes)
If this says 0 allocations you can stop looking for allocations.
As for the @code_whatever tools, if @code_warntype marks things red, frequently allocations will be needed to handle that (with the exception of some small unions in Julia 0.7 and up). In @code_llvm I only know to look for alloca, but allocations can easily be hidden in calls to non-inlined functions.
But this is relatively easy to experiment with. Write the smallest function you can that does some kind of allocation, and as little else as possible, and see what shows up in @code_whatever. If necessary, compare to what turns up in non-allocating small functions.