How to inspect fused loop?

Yes

That’s how I do it:

julia> function f1()
       x = randn(5)
       y = randn(5)
       i = [1, 3]
       y[i] .= x[i] .+ 1.0
       y
       end
julia> function f2()
       x = randn(5)
       y = randn(5)
       i = [1, 3]
       y[i] .= view(x, i) .+ 1.0
       y
       end
julia> @code_lowered f1()
CodeInfo(
1 ─       x = Main.randn(5)
│         y = Main.randn(5)
│         i = Base.vect(1, 3)
│   %4  = y
│   %5  = i
│   %6  = Base.dotview(%4, %5)
│   %7  = Main.:+
│   %8  = x
│   %9  = i
│   %10 = Base.getindex(%8, %9)
│   %11 = Base.broadcasted(%7, %10, 1.0)
│         Base.materialize!(%6, %11)
│   %13 = y
└──       return %13
)
julia> @code_lowered f2()
CodeInfo(
1 ─       x = Main.randn(5)
│         y = Main.randn(5)
│         i = Base.vect(1, 3)
│   %4  = y
│   %5  = i
│   %6  = Base.dotview(%4, %5)
│   %7  = Main.:+
│   %8  = x
│   %9  = i
│   %10 = Main.view(%8, %9)
│   %11 = Base.broadcasted(%7, %10, 1.0)
│         Base.materialize!(%6, %11)
│   %13 = y
└──       return %13
)

Both function are identical except for:

%10 = Base.getindex(%8, %9)
%10 = Main.view(%8, %9)

where you see the copy/allocation.

You can see it with benchmarking too:

julia> using BenchmarkTools
julia> @benchmark f1()
...
Memory estimate: 352 bytes, allocs estimate: 8.

VS

julia> @benchmark f2()
...
Memory estimate: 272 bytes, allocs estimate: 6.
6 Likes