Splat on a vector uses allocations and not on a tuple


In the following example, I am passing a set of subscripts i, j, k of a 3-dimensional array A in the form of vector v = [i,j,k] or tuple t = (i,j,k) using a splat .... I find that A[v...] uses 2 allocations whereas A[t...] doesn’t. This leads to much slower execution of A[v...] compared to A[t...]:

julia> VERSION

julia> using BenchmarkTools

julia> v = [1,2,3]; t = (1,2,3); A = rand(3,3,3);

julia> @btime $A[$v...]
  81.003 ns (2 allocations: 32 bytes)

julia> @btime $A[$t...]
  2.266 ns (0 allocations: 0 bytes)

Why does v... need to use allocations? Is this something that can be fixed, or there is something fundamental in vectors that requires allocations?


You don’t know how long they are.