# Understanding Allocations and Views

**URL:** <https://discourse.julialang.org/t/understanding-allocations-and-views/74967>\
**Category:** General Usage\
**Tags:** memory-allocation, generator\
**Created:** [January 21, 2022, 3:56am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967 "2022-01-21T03:56:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [January 21, 2022, 3:56am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967/1 "2022-01-21T03:56:42Z")

</div>

Hello,

I am trying to improve some code I have, specifically to reduce the allocations it makes in a hot inner loop. I’ve been using `Profile` and `julia --track-allocation=user` to inspect on what lines the bulk of the allocations are occurring. I initially cut down on quite a few by sprinkling `@view` throughout my code. But the following line is puzzling me, this line of code is in the hot inner loop in question and has **a lot** of allocations associated with it.

```julia
tmp = @view pre_allocated_array_1[indices] # [3 element view]
pre_allocated_array_2[i] = sum(tmp .* pre_allocated_tuple) # tuple also has 3 elements

```

I tried `sum(sum, tmp .* pre_allocated_tuple)` based on this post [here](https://discourse.julialang.org/t/summing-arrays-efficiently/15338/8), but it didn’t make a difference.

Any hints on what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 21, 2022, 4:10am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967/2 "2022-01-21T04:10:36Z")

</div>

Does it need to allocate to hold `tmp .* pre_allocated_tuple`? How about a generator?

```julia
julia> using BenchmarkTools

julia> a = rand(3); b = rand(3)                                                                           
                                                                                               
julia> @btime sum($a .* $b)                                                                               
  24.900 ns (1 allocation: 80 bytes)                                                                      
6.25852e-01                                                                                               
                                                                                                         
julia> @btime sum($a[i] * $b[i] for i in 1:length($a))                                                    
  5.900 ns (0 allocations: 0 bytes)                                                                       
6.25852e-01                                                                                               

```

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [January 21, 2022, 4:19am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967/3 "2022-01-21T04:19:46Z")

</div>

Whoa! That helps! Thank you.

Maybe a stupid question - why doesn’t the generator allocate? How does it solve the problem.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 21, 2022, 4:28am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967/4 "2022-01-21T04:28:20Z")

</div>

[Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions) may help.

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [January 21, 2022, 4:31am UTC](https://discourse.julialang.org/t/understanding-allocations-and-views/74967/5 "2022-01-21T04:31:50Z")

</div>

Thank you
