# Minimize Allocations

**URL:** <https://discourse.julialang.org/t/minimize-allocations/62362>\
**Category:** Performance\
**Created:** [June 3, 2021, 10:04pm UTC](https://discourse.julialang.org/t/minimize-allocations/62362 "2021-06-03T22:04:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)\
**Post date:** [June 3, 2021, 10:04pm UTC](https://discourse.julialang.org/t/minimize-allocations/62362/1 "2021-06-03T22:04:01Z")

</div>

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

```julia
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

```julia
 ──────────────────────────────────────────────────────────────────
                           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
 ──────────────────────────────────────────────────────────────────

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 3, 2021, 10:12pm UTC](https://discourse.julialang.org/t/minimize-allocations/62362/2 "2021-06-03T22:12:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)\
**Post date:** [June 3, 2021, 10:59pm UTC](https://discourse.julialang.org/t/minimize-allocations/62362/3 "2021-06-03T22:59:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 3, 2021, 11:04pm UTC](https://discourse.julialang.org/t/minimize-allocations/62362/4 "2021-06-03T23:04:01Z")

</div>

```julia
@code_warntype

```

is always your friend
