# Unexpected Memory Allocation of Broadcasting Copy

**URL:** https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634
**Category:** Performance
**Tags:** question
**Created:** [June 14, 2024, 4:04am UTC](https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634 "2024-06-14T04:04:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![0samuraiE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0samuraie/32/209825_2.png) [@0samuraiE](https://discourse.julialang.org/u/0samuraiE)
#### Post date: [June 14, 2024, 4:04am UTC](https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634/1 "2024-06-14T04:04:32Z")

</div>

Hi all. I encounted the problem as topic saids.

```julia
julia> a = zeros(4, 4, 3)

julia> @btime @views $a[:, :, 2] .= $a[:, :, 2]
  38.862 ns (1 allocation: 192 bytes)

julia> @btime @views for i in eachindex($a[:, :, 2])
        $a[:, :, 2][i] = $a[:, :, 2][i]
    end
  7.744 ns (0 allocations: 0 bytes)

```

I thought these two benchmarks do the exactly same thing.  
Is this problem a bug or a specification or my wrong?  
Can someone please tell me what’s going on?

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [June 14, 2024, 4:10am UTC](https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634/2 "2024-06-14T04:10:27Z")

</div>

I believe the allocations are occurring because `@views` recognises that you are assigning from `a` to `a` which refer to the same part of memory.

For example:

```julia
a = zeros(4, 4, 3)
b = zeros(4, 4, 3)
f(a, b) = (@views a[:, :, 2] .= b[:, :, 2]; return nothing)
@btime f(a, b) # no allocations
@btime f(a, a) # 1 allocation

```

---

<div class="post-metadata">

### Author: ![0samuraiE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0samuraie/32/209825_2.png) [@0samuraiE](https://discourse.julialang.org/u/0samuraiE)
#### Post date: [June 14, 2024, 4:15am UTC](https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634/3 "2024-06-14T04:15:40Z")

</div>

Thank you for fast answering.

> [screw\_dog](https://discourse.julialang.org/u/screw_dog)  
> you are assigning from a to a which refer to the same part of memory.

I agree with you. But my second benchmark also assigns by the same way.  
I wonder what the difference is.

---

<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 14, 2024, 4:22pm UTC](https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634/4 "2024-06-14T16:22:00Z")

</div>

IIRC, broadcasting can conservatively insert a copy if the source & target arrays alias, but I’m only like 85% sure on that.

---

<div class="post-metadata">

### Author: ![0samuraiE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0samuraie/32/209825_2.png) [@0samuraiE](https://discourse.julialang.org/u/0samuraiE)
#### Post date: [June 15, 2024, 2:59am UTC](https://discourse.julialang.org/t/unexpected-memory-allocation-of-broadcasting-copy/115634/5 "2024-06-15T02:59:47Z")

</div>

Thus, this is a specification.  
Thank you for answering.
