# Unexpected allocation at each loop iteration

**URL:** <https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600>\
**Category:** Performance\
**Tags:** question\
**Created:** [June 11, 2022, 1:26pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600 "2022-06-11T13:26:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon79](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simon79](https://discourse.julialang.org/u/simon79)\
**Post date:** [June 11, 2022, 1:26pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/1 "2022-06-11T13:26:27Z")

</div>

Hi,  
The following overly-simplified extraction from my code shows a section of the code which is allocating memory at every loop iteration.

I think this should not happen and I’d like to have advise and suggestion on how to optimize/correct it.

```julia
cache_unique_edges = array_cache(conn_unique_edges) # allocation done here to prevent further allocations later.
for iel = 1:nelem    
    for e = 1:E
        ...
        for g = 1:G
            ...
            ai = getindex!(cache_unique_edges, conn_unique_edges, g)

            if (ai[2] == ai[1]) 
                # THIS if STATEMENT CAUSES OVER-ALLOCATION but I want to avoid it!
            end
        end
    end
end

```

When the `if` statement is commented out, the allocation and timing are:  
`[ Info: 17.088092 seconds (37.64 k allocations: 2.114 MiB)`

However when the the code executes `if (ai[2] == ai[1])`, then allocation and timing are:  
`[ Info: 35.950601 seconds (676.71 M allocations: 10.085 GiB, 1.46% gc time)`

---

<div class="post-metadata">

**Author:** ![simon79](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simon79](https://discourse.julialang.org/u/simon79)\
**Post date:** [June 11, 2022, 1:28pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/2 "2022-06-11T13:28:15Z")

</div>

NOTE on `getindex!` and `array_cache`: these are functions that use my own definition of tables. This being said, the same exact behavior is observed if I use julia native Arrays.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 11, 2022, 1:45pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/3 "2022-06-11T13:45:07Z")

</div>

Without knowing what `ai` ultimately is, it’s quite impossible to diagnose from afar. Do you have a minimal, self contained example people could run to debug on their machine?

---

<div class="post-metadata">

**Author:** ![simon79](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simon79](https://discourse.julialang.org/u/simon79)\
**Post date:** [June 11, 2022, 2:02pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/4 "2022-06-11T14:02:03Z")

</div>

hi @Sukera thanks for replying. I am extracting a working code for you to test. Because it is part of a major code that I am developing, you will need to run it from within its own --project=. and add a couple libraries.  
I hope that is ok. I’ll post a github link shortly

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 11, 2022, 2:23pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/5 "2022-06-11T14:23:45Z")

</div>

Is the cache array a global variable? If so, that’s your problem.

---

<div class="post-metadata">

**Author:** ![simon79](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simon79](https://discourse.julialang.org/u/simon79)\
**Post date:** [June 11, 2022, 2:30pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/6 "2022-06-11T14:30:30Z")

</div>

It is not. This being said, reducing the code to a minimal working code for this forum seems to have helped found the culprit. More soon. Still assessing this statement.

---

<div class="post-metadata">

**Author:** ![simon79](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simon79](https://discourse.julialang.org/u/simon79)\
**Post date:** [June 11, 2022, 4:00pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/7 "2022-06-11T16:00:12Z")

</div>

Ok, I pushed the minimal code that allows you to run it.

```julia
git clone --branch discourse/julia git@github.com:smarras79/jexpresso.git

```

To run it simply do:

```julia
>> cd jexpresso
>> julia --project=.
>> include("./src/jexpresso.jl")

```

(It will ask you to add some libraries. Let me know if that is giving you issues.)

The function of interest is:

> <https://github.com/smarras79/jexpresso/blob/discourse/julia/src/mesh/mod_mesh_minimal.jl#L141>

Increase/decrease NEL at line 145 of the code  
OR

comment/uncomment the IF statement at line 166 of the code

to see how the code is behaving.

Thanks for willing to help.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 11, 2022, 5:04pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/8 "2022-06-11T17:04:22Z")

</div>

> <https://github.com/smarras79/jexpresso/blob/7b786c2fbac66de8b6d726883517d0a8840cfba2/src/mesh/mod_mesh_minimal.jl#L153>

I don’t know where `array_cache` comes from, but my guess is that the function allocates a new array internally?

> <https://github.com/smarras79/jexpresso/blob/7b786c2fbac66de8b6d726883517d0a8840cfba2/src/mesh/mod_mesh_minimal.jl#L166>

This creates two new arrays per iteration, together with the surrounding loops that’s a total of `NLOCAL * NEL * NGLOBAL * 2` allocations. Either use a tuple (so `(ai[1], ai[2])` etc) or write the comparison exiplicitly.

I do not have a local MPI setup, so I can’t really run your code sorry. I also don’t know where `getindex!` is coming from - github code search does not show any hits in your repository, so I don’t know what it’s type would be and thus can’t really figure out which `getindex` method on `ai` would be called. However, since you report the same behavior with standard arrays, I’m assuming the `getindex` itself does not allocate.

---

<div class="post-metadata">

**Author:** ![simon79](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simon79](https://discourse.julialang.org/u/simon79)\
**Post date:** [June 11, 2022, 5:19pm UTC](https://discourse.julialang.org/t/unexpected-allocation-at-each-loop-iteration/82600/9 "2022-06-11T17:19:53Z")

</div>

right, getindex does NOT allocate. You can find how getindex is defined here: [Gridap.Arrays · Gridap.jl](https://gridap.github.io/Gridap.jl/v0.7/Arrays/#Gridap.Arrays.getindex!-Tuple%7BAny,AbstractArray,Vararg%7BAny,N%7D%20where%20N)}
