# Allocations (again...)

**URL:** https://discourse.julialang.org/t/allocations-again/90780
**Category:** Performance
**Tags:** question, allocations
**Created:** [November 24, 2022, 8:58pm UTC](https://discourse.julialang.org/t/allocations-again/90780 "2022-11-24T20:58:35Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [November 24, 2022, 8:58pm UTC](https://discourse.julialang.org/t/allocations-again/90780/1 "2022-11-24T20:58:35Z")

</div>

When I run

```julia
using BenchmarkTools

function test(x, Y)
    sum!(x, Y')
    return nothing
end

x = rand(Float64, 100)
Y = rand(Float64, 1, 100)
@btime(test(x, Y))

```

I get the following:

```julia
367.952 ns (2 allocations: 96 bytes)

```

I don’t think I should get any allocations. Can someone please explain what’s going on? And how can I avoid such allocations? (In my real application I have to do a similar computation millions of times.)

Thank you in advance.

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [November 24, 2022, 9:05pm UTC](https://discourse.julialang.org/t/allocations-again/90780/2 "2022-11-24T21:05:52Z")

</div>

that’s quite nothing

```julia
julia> @btime test($x,$Y);
  372.976 ns (2 allocations: 96 bytes)

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 24, 2022, 9:14pm UTC](https://discourse.julialang.org/t/allocations-again/90780/3 "2022-11-24T21:14:56Z")

</div>

Where and how is `test()` defined? We would need to know to tell where the allocations are coming from.

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [November 24, 2022, 9:16pm UTC](https://discourse.julialang.org/t/allocations-again/90780/4 "2022-11-24T21:16:21Z")

</div>

The code I have shown was fully typed on a .jl file

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [November 24, 2022, 9:18pm UTC](https://discourse.julialang.org/t/allocations-again/90780/5 "2022-11-24T21:18:03Z")

</div>

It’s a lot if you cycle it through tens of thousands of times

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 24, 2022, 9:20pm UTC](https://discourse.julialang.org/t/allocations-again/90780/6 "2022-11-24T21:20:50Z")

</div>

If you restart Julia and run that code you’ll see it error out. In the shown snippet you only define a two argument method, and then run one with zero arguments. Possibly you’ve defined that one before, and it contains something that allocates.

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [November 24, 2022, 9:23pm UTC](https://discourse.julialang.org/t/allocations-again/90780/7 "2022-11-24T21:23:21Z")

</div>

Sorry, I’ve pasted the wrong code. I’ll update it.

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [November 24, 2022, 9:25pm UTC](https://discourse.julialang.org/t/allocations-again/90780/8 "2022-11-24T21:25:13Z")

</div>

I have updated it and I still get 2 allocations

---

<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: [November 24, 2022, 11:44pm UTC](https://discourse.julialang.org/t/allocations-again/90780/9 "2022-11-24T23:44:02Z")

</div>

I think that `Y'` allocates (write a loop instead, for example).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 25, 2022, 8:08am UTC](https://discourse.julialang.org/t/allocations-again/90780/10 "2022-11-25T08:08:14Z")

</div>

What? When did that start? Adjoints have never allocated as far as I know.

Anyway:

```julia
julia> @btime test($x, $Y)
  124.499 ns (0 allocations: 0 bytes)

```

No allocations here, @aaraujo71

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [November 25, 2022, 8:26am UTC](https://discourse.julialang.org/t/allocations-again/90780/11 "2022-11-25T08:26:44Z")

</div>

> [@aaraujo71](#):
>
> ```julia
> using BenchmarkTools
> 
> function test(x, Y)
> sum!(x, Y')
> return nothing
> end
> 
> x = rand(Float64, 100)
> Y = rand(Float64, 1, 100)
> @btime(test(x, Y))
> 
> ```

I do see the reported allocations in v1.8.2 under macos (arm)

```julia
julia> @btime(test($x, $Y))
  358.333 ns (2 allocations: 96 bytes)

```

EDIT: the issue is the adjoint. Without it, we’re back to zero allocations. I think it is indeed a bug.  
EDIT2: no allocations on v1.8.1 on linux! 2 allocations in macos v1.9.0.alpha1

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 25, 2022, 8:32am UTC](https://discourse.julialang.org/t/allocations-again/90780/12 "2022-11-25T08:32:12Z")

</div>

> [@pablosanjose](#):
>
> EDIT: the issue is the adjoint. Without it, we’re back to zero allocations. I think it is indeed a bug.  
> EDIT2: no allocations on v1.8.1 on linux! 2 allocations in macos v1.9.0.alpha1

Zero allocations on 1.8.0 on Windows, that is _with_ the adjoint.

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [November 25, 2022, 8:38am UTC](https://discourse.julialang.org/t/allocations-again/90780/13 "2022-11-25T08:38:50Z")

</div>

And again 2 allocation in 1.8.3 under linux. It seems there is a regression that was introduced in 1.8.2 or 1.8.3

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [November 25, 2022, 8:44am UTC](https://discourse.julialang.org/t/allocations-again/90780/14 "2022-11-25T08:44:58Z")

</div>

Opened an issue

> <https://github.com/JuliaLang/julia/issues/47703>
>
> Ref: https://discourse.julialang.org/t/allocations-again/90780/13
> 
> António Ara…újo noted the following, which started happening sometime between v1.8.1 and v1.8.3
> 
> \`\`\`julia
> using BenchmarkTools
> 
> function test(x, Y)
> sum!(x, Y')
> return nothing
> end
> 
> x = rand(Float64, 100)
> Y = rand(Float64, 1, 100)
> \`\`\`
> \`\`\`julia
> julia\> @btime(test($x, $Y))
> 301.619 ns (2 allocations: 96 bytes)
> \`\`\`
> In v1.8.0 and v1.8.1 we have instead
> \`\`\`julia
> julia\> @btime(test($x, $Y))
> 68.719 ns (0 allocations: 0 bytes)
> \`\`\`

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [November 25, 2022, 12:28pm UTC](https://discourse.julialang.org/t/allocations-again/90780/15 "2022-11-25T12:28:13Z")

</div>

Thank you all. I am reprogramming may whole functions using loops.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 25, 2022, 2:26pm UTC](https://discourse.julialang.org/t/allocations-again/90780/16 "2022-11-25T14:26:34Z")

</div>

It shouldn’t really be necessary. I mean, loops are fine, but it’s too bad to have to use them over clean simple function calls like `sum!`. This here seems like a spurious allocation, which will possibly be fixed in a patch release in not too long.
