# Strange allocations in broadcasting (with \`^\`?)

**URL:** https://discourse.julialang.org/t/strange-allocations-in-broadcasting-with/72229
**Category:** Performance
**Tags:** performance, potential-bug
**Created:** [November 29, 2021, 1:35pm UTC](https://discourse.julialang.org/t/strange-allocations-in-broadcasting-with/72229 "2021-11-29T13:35:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)
#### Post date: [November 29, 2021, 1:35pm UTC](https://discourse.julialang.org/t/strange-allocations-in-broadcasting-with/72229/1 "2021-11-29T13:35:29Z")

</div>

I think `d ^ 2` should be optimized into just `d * d` ([here](https://github.com/JuliaLang/julia/blob/bbf762da72f1f7285e21a2bfa01d61b408c5a8b6/base/intfuncs.jl#L320)), so the following two operations should be identical, but the form with `^` allocates with the one with `*` does not:

```julia
julia> using BenchmarkTools

julia> const a = rand(3); const b = rand(3); const c = rand(3); const d = rand(3);

julia> @btime @. $a = $b + $c * $d ^ 2;
  23.668 ns (2 allocations: 16 bytes)

julia> @btime @. $a = $b + $c * $d * $d;
  17.883 ns (0 allocations: 0 bytes)

```

Also if the `$b + ` is removed then there are no allocations

```julia
julia> @btime @. $a = $c * $d ^ 2;
  8.064 ns (0 allocations: 0 bytes)

```

Is this a bug?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [November 29, 2021, 2:00pm UTC](https://discourse.julialang.org/t/strange-allocations-in-broadcasting-with/72229/2 "2021-11-29T14:00:59Z")

</div>

Looks similar to the issue noted in [https://github.com/JuliaLang/julia/issues/41565](https://github.com/JuliaLang/julia/issues/41565).

---

<div class="post-metadata">

### Author: ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)
#### Post date: [November 29, 2021, 2:26pm UTC](https://discourse.julialang.org/t/strange-allocations-in-broadcasting-with/72229/3 "2021-11-29T14:26:13Z")

</div>

Thanks @kristoffer.carlsson! It does look like the same issue, and goes away with the latest `master`. Does anyone know if the fix will make it into julia-1.7? I do still get allocations with this example using 1.70-rc3, but they are gone if I use the current `master` (Version 1.8.0-DEV.1081 (2021-11-29) Commit 43bc48b1f8 (0 days old master).

---

<div class="post-metadata">

### Author: ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)
#### Post date: [December 1, 2021, 11:51am UTC](https://discourse.julialang.org/t/strange-allocations-in-broadcasting-with/72229/4 "2021-12-01T11:51:16Z")

</div>

> [@johnomotani](#):
>
> Does anyone know if the fix will make it into julia-1.7?

Looks like the answer was no, at least for 1.7.0. The allocations are still there on the version released today.

```julia
julia> @btime @. $a = $b + $c * $d ^ 2;
  25.734 ns (2 allocations: 16 bytes)

```
