module somemodule
using BenchmarkTools
struct MyType
A::AbstractMatrix
B::AbstractMatrix
end
myinc(A::AbstractMatrix, B::AbstractMatrix) = A .+= B
mytypeinc(t::MyType, B::AbstractMatrix) = t.A .+= B
mytypeinc2(t::MyType, B::AbstractMatrix) = myinc(t.A, B)
function perf()
H = [1.0 0 ; 1.0 -1.0];
Q = [0.01 0 ; 0 0.01];
t = MyType(H, Q);
@btime myinc($H, $Q)
@btime mytypeinc($t, $Q)
@btime mytypeinc2($t, $Q)
end
perf()
end
This outputs
20.435 ns (0 allocations: 0 bytes)
445.258 ns (2 allocations: 64 bytes)
28.728 ns (0 allocations: 0 bytes)
Initially I had implemented in my code something like mytypeinc
, I was surprised to see that it allocates, eventually tried myinc
and that doesn’t allocate. So I tried to write an “improved” version of mytypeinc
called mytypeinc2
which just calls myinc
, and that works to avoid the allocation. But why is it that such a workaround is necessary? Why doesn’t mytypeinc
do the obvious right thing? Clearly my mental model is missing something crucial.
julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, haswell)
Environment:
JULIA_EDITOR = "/home/me/.vscode-server/bin/7db1a2b88f7557e0a43fec75b6ba7e50b3e9f77e/node"
JULIA_NUM_THREADS =