function preparearrays!(arrays, N, L; mode = :all)
for n in 0:(length(arrays)-1)
if mode === :all
for m in 0:(L-1) # run till length L to zero all bits
arrays[n+1][m+1] = (n & (1 << m)) >> m
end
elseif mode === :basis
arrays[n+1] .= 0;
arrays[n+1][n+1] = 1;
end
end
return arrays
end
preparearrays(N, L; mode = :all) = preparearrays!(collect(zeros(Int64, L) for i in 1:ifelse(mode==:basis, N, 2^N)), N, L; mode = mode)
My issue is that I want to set a variable at the start of my calculation, mode = :all or mode = :basis. (The above pattern appears in other places in my code.) However, calling the above function with a variable seems to allocate (and dramatically regress performance):
This looks like a benchmarking artifact. Check the documentation of BencharkTools.jl: you should interpolate the global variables that you pass to the benchmarked code:
For these macros I think to avoid these extra allocations you need to use typed globals (or constants but then the benchmark might be affected by constant propagation?)
using BenchmarkTools
function preparearrays!(arrays, N, L; mode = :all)
for n in 0:(length(arrays)-1)
if mode === :all
for m in 0:(L-1) # run till length L to zero all bits
arrays[n+1][m+1] = (n & (1 << m)) >> m
end
elseif mode === :basis
arrays[n+1] .= 0;
arrays[n+1][n+1] = 1;
end
end
return arrays
end
preparearrays(N, L; mode = :all) = preparearrays!(collect(zeros(Int64, L) for i in 1:ifelse(mode==:basis, N, 2^N)), N, L; mode = mode)
arr::Vector{Vector{Int64}} = preparearrays(8, 16; mode = :basis);
var::Symbol = :basis
using BenchmarkTools
function preparearrays!(arrays, N, L; mode = :all)
for n in 0:(length(arrays)-1)
if mode === :all
for m in 0:(L-1) # run till length L to zero all bits
arrays[n+1][m+1] = (n & (1 << m)) >> m
end
elseif mode === :basis
arrays[n+1] .= 0;
arrays[n+1][n+1] = 1;
end
end
return arrays
end
preparearrays(N, L; mode = :all) = preparearrays!(collect(zeros(Int64, L) for i in 1:ifelse(mode==:basis, N, 2^N)), N, L; mode = mode)
function get_allocated(arr, var)
@allocated preparearrays!(arr, 8, 16; mode=var)
end
arr = preparearrays(8, 16; mode = :basis);
var = :basis;
get_allocated(arr, var) # returns 0