Hi,
I noticed that the simple code example_1! below allocates memory (lots of it when this is embedded in a large loop) whereas example_2! does not.
-
example_1!takes the 3-dimenional arrayconn[iel, :, :]and, within a look, assigns its value to the scalarm. -
example_2!takes the same array except that that array is an argument from a previously defined struct argument (mesh.conn[iel, :,:]) and is also assigned to the scalarm.
I can’t understand why example_1! allocates 2 orders of magnitude less than example_2!.
function example_1!(conn, iel)
for l = 1:ngl, i = 1:mesh.ngl
m = conn[iel, i, l]
end
end
40.873 μs (400 allocations: 6.25 KiB)
whereas:
function example_2!(st_mesh, iel)
for l = 1:ngl, i = 1:mesh.ngl
m = st_mesh.conn[iel, i, l]
end
end
1.348 ms (41825 allocations: 653.52 KiB)
The minimal version of the struct is:
Base.@kwdef mutable struct St_mesh{TInt, TFloat}
conn = Array{Int64}(undef, 0)
ngl::Union{TInt, Missing} = 4 #this is used defined and is changed later
end
where conn is allocated with the call:
mesh.conn = Array{Int64}(undef, mesh.nelem, mesh.ngl, mesh.ngl)
where
mesh = St_mesh{TInt,TFloat}(ngl)