# Extra allocation in @thread loop in v1.2

**URL:** <https://discourse.julialang.org/t/extra-allocation-in-thread-loop-in-v1-2/27775>\
**Category:** Performance\
**Tags:** question\
**Created:** [August 20, 2019, 8:32pm UTC](https://discourse.julialang.org/t/extra-allocation-in-thread-loop-in-v1-2/27775 "2019-08-20T20:32:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)\
**Post date:** [August 20, 2019, 8:32pm UTC](https://discourse.julialang.org/t/extra-allocation-in-thread-loop-in-v1-2/27775/1 "2019-08-20T20:32:53Z")

</div>

Consider the following MWE of threaded element-wise computation:

```julia
using Base.Threads
using LinearAlgebra
using BenchmarkTools

const N = 100000
a = rand(N)
b = rand(N)

function foo(a, b)
    @threads for i ∈ eachindex(a)
        a[i] = (a[i] + b[i]) ^ (a[i] - b[i])
    end
end

@btime foo($a, $b)

```

I tested it under both Julia 1.1.1 and Julia 1.2.0, both of which are official binaries:

```nohighlight
~/codes » /home/opt/julia-1.1.1/bin/julia test.jl pshi@discover
  75.924 μs (1 allocation: 32 bytes)
---------------------------------------------------------------------------------------------------------
~/codes » /home/opt/julia-1.2.0/bin/julia test.jl pshi@discover
  114.931 μs (133 allocations: 13.53 KiB)

```

And the computer is:

```julia
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) Gold 6136 CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 20

```

There is significant more allocation and worse performance in 1.2 if I do so. Is there anything I should modify in the new version (seems not according to the changelog)? Or could you guys reproduce/confirm this?

Thanks!

---

<div class="post-metadata">

**Author:** ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)\
**Post date:** [August 21, 2019, 4:49pm UTC](https://discourse.julialang.org/t/extra-allocation-in-thread-loop-in-v1-2/27775/2 "2019-08-21T16:49:36Z")

</div>

This maybe related to [this issue](https://github.com/JuliaLang/julia/issues/32701).

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 21, 2019, 5:39pm UTC](https://discourse.julialang.org/t/extra-allocation-in-thread-loop-in-v1-2/27775/3 "2019-08-21T17:39:19Z")

</div>

Just guessing, but it could have something to do with the use of `invokelatest` here:

[https://github.com/JuliaLang/julia/blob/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/threadingconstructs.jl#L71](https://github.com/JuliaLang/julia/blob/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/threadingconstructs.jl#L71)

which was introduced in [https://github.com/JuliaLang/julia/pull/30838](https://github.com/JuliaLang/julia/pull/30838).
