Consider the following simple program in Julia:
function foo_time(x)
@time x.^2
return nothing
end
n = 1000;
foo_time(collect(1:n));
If I run that in my console, then @time reports 1 allocation, which is what I expect. However, if I change n to 10000, then @time reports 2 allocations.
What is more, if I chain together functions without syntactic loop fusion (in other words, without dots) then I seem to get double the expected allocations. For example, writing (x + x).^2 + x instead of x.^2 yields 3 allocations with n = 1000, but it yields 6 allocations with n = 10000. (The pattern does not strictly continue though: for instance, (x + x + x).^2 only yields 5 allocations for n = 10000.)
Why should the size of the vector affect how many allocations occur? What is going on under the hood here?
This occurs both in the JupyterLab console and in the normal Julia REPL.