Understanding Allocations and Views

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.

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, but it didn’t make a difference.

Any hints on what I am doing wrong?

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

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                                                                                               
1 Like

Whoa! That helps! Thank you.

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

Multi-dimensional Arrays · The Julia Language may help.

Thank you