How to avoid allocations when multiplying numbers with missings

I don’t know… For some reason, these two functions allocate in 1.5.3:

f(x) = x[1] * x[2] * x[3]
g(x) = x[1] + x[2] + x[3]

julia> @btime f($[missing, 1.0, 2.0])
  28.780 ns (2 allocations: 32 bytes)

julia> @btime g($[missing, 1.0, 2.0])
  28.334 ns (2 allocations: 32 bytes)

but the following doesn’t:

h(x) = x[1] * x[2] + x[3]

julia> @btime h($[missing, 1.0, 2.0])
  4.558 ns (0 allocations: 0 bytes)

I also don’t know what has changed exactly in 1.6 to avoid these allocations.

2 Likes