# Why does b \*= A\[r\] allocate memory?

**URL:** <https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279>\
**Category:** Performance\
**Tags:** memory-allocation, allocations\
**Created:** [November 15, 2023, 7:33pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279 "2023-11-15T19:33:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_White](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_white/32/50922_2.png) [@Christopher\_White](https://discourse.julialang.org/u/Christopher_White)\
**Post date:** [November 15, 2023, 7:33pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/1 "2023-11-15T19:33:23Z")

</div>

The following code does a whole bunch of allocations:

```
N = 10000000
A = randn(Float64, N)
b = 1.0
@show typeof(b)
@time for r = 1:N
    b *= A[r]
 end

```

Why is that? How can I write this in a way that doesn’t allocate ~ a gigabyte of memory? (In this particular case the right answer is `prod`, but this is a MWE we’ve run into in a more complicated context, where it’s not completely clear how to use `prod`.)

In particular:

```
julia> N = 10000000; A = randn(Float64, N); b = 1.0;@show typeof(b); @time for r = 1:N; b = b*A[r] end
typeof(b) = Float64
 0.636152 seconds (50.00 M allocations: 915.513 MiB, 3.44% gc time, 0.94% compilation time)

julia> N = 10000000; A = randn(Float64, N); b = 1.0;@show typeof(b); @time for r = 1:N; b = b*A[r] end
typeof(b) = Float64
 0.612025 seconds (50.00 M allocations: 915.512 MiB, 2.22% gc time)

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 15, 2023, 7:39pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/2 "2023-11-15T19:39:58Z")

</div>

don’t benchmark in the global scope. You’re seeing type instability since `A` is a non-type asserted global variable.

```julia
julia> N = 1000;
julia> A = randn(Float64, N);
julia> b = 1.0;
julia> @time for r = 1:N
           b *= A[r]
        end
  0.007243 seconds (4.08 k allocations: 82.250 KiB, 98.37% compilation time)
julia> function myprod(A)
           b = 1.0
           for r = 1:length(A)
               b *= A[r]
           end
           b
       end
julia> @time myprod(A);
  0.006203 seconds (3.66 k allocations: 203.109 KiB, 99.70% compilation time)

julia> @time myprod(A);
  0.000006 seconds (1 allocation: 16 bytes)

```

---

<div class="post-metadata">

**Author:** ![Christopher\_White](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_white/32/50922_2.png) [@Christopher\_White](https://discourse.julialang.org/u/Christopher_White)\
**Post date:** [November 15, 2023, 7:54pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/3 "2023-11-15T19:54:41Z")

</div>

Hmm. Yes, right, this is a very good point about my MWE. Our actual problem isn’t in global scope (I don’t think), so it’s not completely obvious how that’s going to play out, but I suppose there could be some similar type-instability effect.

Probably we should be wrapping the inside of the inner loop in a function anyway, which will help.

In any case thank you!

---

<div class="post-metadata">

**Author:** ![Jingchen\_Zhang](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jingchen\_Zhang](https://discourse.julialang.org/u/Jingchen_Zhang)\
**Post date:** [November 15, 2023, 8:48pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/4 "2023-11-15T20:48:32Z")

</div>

Hello,

I ran the same codes as yours but didn’t get your result in the last line(i.e. 1 allocation: 16 bytes). Could you tell me why?

julia\> N = 100000000;

julia\> A = randn(Float16,N)  
100000000-element Vector{Float16}:  
0.1344  
-1.894  
0.5503  
-0.282  
0.829  
-1.03  
-0.1345  
0.0896  
0.1259  
2.053  
⋮  
-0.173  
2.73  
0.1725  
0.2515  
-0.5327  
-0.8926  
0.195  
-0.3242  
1.868

julia\> function myprod(A)  
b = 1.0  
for r = 1:N  
b \*=A[r]  
end  
b  
end  
myprod (generic function with 1 method)

julia\> @time myprod(A);  
5.137363 seconds (500.01 M allocations: 8.941 GiB, 2.59% gc time, 0.19% compilation time)

julia\> @time myprod(A);  
5.201536 seconds (500.00 M allocations: 8.941 GiB, 2.53% gc time)

Thanks!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 15, 2023, 8:50pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/5 "2023-11-15T20:50:04Z")

</div>

That would because I had a typo. Specifically, before, I had `1:N` which is slow because `N` is a global variable instead of `1:length(A)`

---

<div class="post-metadata">

**Author:** ![Jingchen\_Zhang](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jingchen\_Zhang](https://discourse.julialang.org/u/Jingchen_Zhang)\
**Post date:** [November 15, 2023, 9:01pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/6 "2023-11-15T21:01:07Z")

</div>

I see the difference! Thank you very much

---

<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 15, 2023, 9:29pm UTC](https://discourse.julialang.org/t/why-does-b-a-r-allocate-memory/106279/7 "2023-11-15T21:29:32Z")

</div>

> [@Jingchen\_Zhang](#):
>
> I ran the same codes as yours but didn’t get your result in the last line(i.e. 1 allocation: 16 bytes).

The 16 bytes are also a benchmarking artefact, btw. It should be zero, which is what you get when you interpolate the input argument:

```julia
julia> @btime myprod($A);
  142.350 ms (0 allocations: 0 bytes)

```
