# Weird allocation issue for fill! with ArrayPartition in Julia \< 1.6

**URL:** https://discourse.julialang.org/t/weird-allocation-issue-for-fill-with-arraypartition-in-julia-1-6/61296
**Category:** Performance
**Tags:** array, memory-allocation
**Created:** [May 17, 2021, 11:21am UTC](https://discourse.julialang.org/t/weird-allocation-issue-for-fill-with-arraypartition-in-julia-1-6/61296 "2021-05-17T11:21:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [May 17, 2021, 11:21am UTC](https://discourse.julialang.org/t/weird-allocation-issue-for-fill-with-arraypartition-in-julia-1-6/61296/1 "2021-05-17T11:21:24Z")

</div>

Hello Julians,  
I have an issue with allocations that I encountered while running my code on a cluster which uses Julia 1.5.4. Basically, once my partition array gets reasonably large, I encounter many allocations when calling the fill! function. (It also occurs if I iterate over this array explicitly).  
Consider the following MWE:

```julia
using RecursiveArrayTools, BenchmarkTools

function test1()
    PartArray = ArrayPartition(
        zeros(2),
        zeros(200,200,30),
    )
    @btime fill!($PartArray,0.)
end
    
function test2()
    PartArray = ArrayPartition(
        zeros(2),
        zeros(200,200,30), 
        zeros(40,40,20,30),
    )
    @btime fill!($PartArray,0.)
end
test1();
test2();

```

On Julia 1.6.0 this returns:

```julia
julia> test1();
  2.449 ms (0 allocations: 0 bytes)

julia> test2();
  5.693 ms (0 allocations: 0 bytes)

```

Which is what I would expect. On Julia 1.5.4, however, the result is:

```julia
julia> test1();
  2.649 ms (0 allocations: 0 bytes)

julia> test2();
  74.203 ms (4318980 allocations: 65.90 MiB)

```

These allocations cause a lot of garbage collector time, so I’d like to get rid of them.  
Is there a solution other than getting the admins to update the Julia version on the Cluster to 1.6?  
Another interesting hint to the possible issue is that in **both** Julia versions the type of the partition Array seems to be unstable:

```julia
julia> isconcretetype( ArrayPartition(zeros(2)) )
false

```

Does anyone know how I can construct an ArrayPartition such that the result is a concrete type?  
Best wishes,  
Salmon

\*Edit: Formatting

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [May 17, 2021, 3:31pm UTC](https://discourse.julialang.org/t/weird-allocation-issue-for-fill-with-arraypartition-in-julia-1-6/61296/2 "2021-05-17T15:31:10Z")

</div>

Ok, I figured out a workaround:

Even though one can iterate over a eachindex of an ArrayPartition, it is probably better to “unpack” its elements when doing this, since an ArrayPartition can contain arrays of different types.

This seems to work:

```julia
using RecursiveArrayTools,BenchmarkTools

function setZero!(PartArr::ArrayPartition)
    for arr in PartArr.x
        fill!(arr,0.)
    end
end
    
function test2()
    PartArray = ArrayPartition(
        zeros(2),
        zeros(200,200,30), 
        zeros(40,40,20,30),
    )
    @btime fill!($PartArray,0.) #old version
    @btime setZero!($PartArray)
end

julia> test2()
  72.663 ms (4318980 allocations: 65.90 MiB) #using fill!
  1.574 ms (0 allocations: 0 bytes) #using setzero!

```

Its should also be possible to overload Base.fill! for an ArrayPartition to allow the efficient use of fill!.
