# Minimize Allocations

Hi! How do I minimize allocations here? I thought that since all variables involved here are scalars, there should be minimal allocations?

``````function ϕfield!(
ϕn::Array{FT}, ϕ::Array{FT}, u::Array{FT}, v::Array{FT},
G::Grid{FT}, D::Domain{FT}, S::Simulation{FT}, Q::Array{FT}
) where FT <: Real

for jj = 1 : G.ny, ii = 1 : G.nx

@timeit "Line 1" vy = (v[ii,jj+1] - v[ii,jj]) / G.δy

if ii != 1
@timeit "Line 2" ux = (u[ii,jj] - u[ii-1,jj]) / G.δx
else
@timeit "Line 3" ux = (u[1,jj] - u[G.nx,jj]) / G.δx
end

@timeit "Line 4" ϕn[ii,jj] = ϕ[ii,jj] - S.δt * (D.α * ϕ[ii,jj] + D.g * D.H * (ux+vy) + Q[ii,jj])

end

return

end
``````

Returns

`````` ──────────────────────────────────────────────────────────────────
Time                   Allocations
──────────────────────   ───────────────────────
Tot / % measured:      58.1s / 1.43%           1.48GiB / 27.2%

Section   ncalls     time   %tot     avg     alloc   %tot      avg
──────────────────────────────────────────────────────────────────
Line 4     1.23M    350ms  42.0%   285ns    225MiB  54.5%     192B
Line 1     1.23M    252ms  30.3%   205ns   93.8MiB  22.7%    80.0B
Line 2     1.22M    229ms  27.6%   188ns   93.3MiB  22.6%    80.0B
Line 3     6.40k   1.17ms  0.14%   183ns    500KiB  0.12%    80.0B
──────────────────────────────────────────────────────────────────
``````

Indeed, there should be no allocations there. Can you provide a minimal working example? It might be that some of your types are not declared in a type-stable form.

I figured out why!! `G.nx` and `G.ny` were called as `Integer` types instead of `Int`. Thanks for the hint!

1 Like
``````@code_warntype
``````