# Allocations in broadcasted assignment (StackOverflow question)

**URL:** https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680
**Category:** Performance
**Tags:** broadcast, memory-allocation
**Created:** [February 3, 2024, 6:35pm UTC](https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680 "2024-02-03T18:35:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bert](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@bert](https://discourse.julialang.org/u/bert)
#### Post date: [February 3, 2024, 6:35pm UTC](https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680/1 "2024-02-03T18:35:51Z")

</div>

I came across this StackOverflow post and am wondering what the solution is. I have played around with the example in this post (copied below) and, like the author, cannot get rid of the allocation in `d`’s broadcast assignment. Any ideas?

(A commenter says this is fixed in nightly, but I’m wondering about Julia 1.10.)

> <https://stackoverflow.com/questions/77929376/why-do-allocations-occur-during-broadcasting-assignment-to-a-preallocated-array>

```Julia
using BenchmarkTools
using Random

let
    k = 100
    n = 10000
    a = zeros(Int, n)
    b = zeros(Int, n)
    c = falses(n)
    d = falses(n)

    @btime rand!($a, 1:($k))
    @btime rand!($b, 1:($k))

    @btime $c .= $a .<= $b
    @btime $d .= $c .& ($b .!= 0)

    nothing
end

```

Results from the StackOverflow post:

```julia
  27.792 μs (0 allocations: 0 bytes)
  28.541 μs (0 allocations: 0 bytes)
  2.560 μs (1 allocation: 4.19 KiB)
  7.552 μs (1 allocation: 4.19 KiB)

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 4, 2024, 4:05am UTC](https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680/2 "2024-02-04T04:05:24Z")

</div>

I don’t really understand this tbh.

```julia
using BenchmarkTools
using Random
using LoopVectorization

let
    k = 100
    n = 10000
    a = zeros(Int, n)
    b = zeros(Int, n)
    c = falses(n)
    d = falses(n)

    rand!(a, 1:(k))
    rand!(b, 1:(k))

    @btime $c .= $a .<= $b    
    @btime map!(≤, $c, $a, $b)
    @btime vmap!(≤, $c, $a, $b)
    nothing
end
  3.062 μs (1 allocation: 4.19 KiB)
  22.121 μs (0 allocations: 0 bytes)
  1.236 μs (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [February 4, 2024, 7:21am UTC](https://discourse.julialang.org/t/allocations-in-broadcasted-assignment-stackoverflow-question/109680/3 "2024-02-04T07:21:49Z")

</div>

The allocation goes away if the destination is a `Vector{Bool}` instead of a `BitVector`, and given the 0 allocations from `map!`, I’d hazard a guess it’s rooted in `BitArray`-specific broadcasting.
