Hi,
I encountered some allocations in my code that I can pin down to the following foobar example.
When assigning a static array to a variable in a struct i get allocations, which I donβt get when the static array gets allocated to a variable outside of a struct.
julia> mutable struct Foo
m ::SMatrix{3,2,Float64,6}
v ::SVector{3,Float64}
end
julia> bar = Foo([1.,2,3,4,5,6], [1.,2,3])
Foo([1.0 4.0; 2.0 5.0; 3.0 6.0], [1.0, 2.0, 3.0])
julia> @benchmark bar.m = f()
BenchmarkTools.Trial: 10000 samples with 973 evaluations.
Range (min β¦ max): 66.960 ns β¦ 3.199 ΞΌs β GC (min β¦ max): 0.00% β¦ 96.51%
Time (median): 73.921 ns β GC (median): 0.00%
Time (mean Β± Ο): 83.220 ns Β± 132.248 ns β GC (mean Β± Ο): 8.16% Β± 5.00%
ββββββ
ββ
βββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββ β
67 ns Histogram: frequency by time 145 ns <
Memory estimate: 128 bytes, allocs estimate: 2.
julia> @benchmark m = f()
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 0.025 ns β¦ 0.265 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 0.027 ns β GC (median): 0.00%
Time (mean Β± Ο): 0.027 ns Β± 0.003 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
0.025 ns Histogram: frequency by time 0.03 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> g() = SVector(1.,2.,3.)
g (generic function with 1 method)
julia> @benchmark bar.v = g()
BenchmarkTools.Trial: 10000 samples with 963 evaluations.
Range (min β¦ max): 60.506 ns β¦ 2.470 ΞΌs β GC (min β¦ max): 0.00% β¦ 95.78%
Time (median): 67.485 ns β GC (median): 0.00%
Time (mean Β± Ο): 73.114 ns Β± 81.647 ns β GC (mean Β± Ο): 3.95% Β± 3.46%
βββ
βββββββββββ
βββββββββ βββ ββββββ β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββ β
60.5 ns Histogram: log(frequency) by time 115 ns <
Memory estimate: 64 bytes, allocs estimate: 2.
julia> @benchmark v = g()
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 0.024 ns β¦ 0.049 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 0.027 ns β GC (median): 0.00%
Time (mean Β± Ο): 0.027 ns Β± 0.001 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
0.024 ns Histogram: frequency by time 0.03 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
Can you explain me where the allocation come from and how I can get rid off them?
Thank you,
Nico