# Splatting of an integer

**URL:** <https://discourse.julialang.org/t/splatting-of-an-integer/52160>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 21, 2020, 8:48am UTC](https://discourse.julialang.org/t/splatting-of-an-integer/52160 "2020-12-21T08:48:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mancellin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mancellin/32/13971_2.png) [@mancellin](https://discourse.julialang.org/u/mancellin)\
**Post date:** [December 21, 2020, 8:48am UTC](https://discourse.julialang.org/t/splatting-of-an-integer/52160/1 "2020-12-21T08:48:35Z")

</div>

Hello everyone,

I was trying to write some generic code in which users can provide indices either as Ints (in 1D) or as tuples of Ints (in 2D/3D). To interface with functions like `getindex`, my solution was to splat the indices.  
Splatting a tuple has no runtime cost, since the compiler knows the size of the tuple. I would have expected the same to hold for integers, but I noticed a 100x runtime increase.

```julia
using BenchmarkTools

a = [1 2 3 4; 5 6 7 8]
i = (1, 2)
@btime getindex($a, ($i)[1], ($i)[2])
@btime getindex($a, ($i)...)
#=
  2.367 ns (0 allocations: 0 bytes)
  2.073 ns (0 allocations: 0 bytes)
=#

a = [1, 2, 3, 4, 5, 6]
i = 3
@btime getindex($a, $i)
@btime getindex($a, ($i)...)
#=
  1.781 ns (0 allocations: 0 bytes)
  100.659 ns (2 allocations: 32 bytes)
=#

```

My guess is that the same compiler optimization could be used for Ints and Tuples, but the developers did not take the time to implement it for Ints since it has little practical use. Is that correct or am I missing something?

Anyway, it is always possible to go around this limitation by converting the Int to a one-element Tuple:

```julia
i = (3,)
@btime getindex($a, ($i)[1])
@btime getindex($a, ($i)...)
#=
  1.781 ns (0 allocations: 0 bytes)
  1.782 ns (0 allocations: 0 bytes)
=#

```

Related discussion: [Concerns about splatting](https://discourse.julialang.org/t/concerns-about-splatting/6664)

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [December 21, 2020, 12:05pm UTC](https://discourse.julialang.org/t/splatting-of-an-integer/52160/2 "2020-12-21T12:05:41Z")

</div>

Looks like this discrepancy is gone on version 1.6:

1.5:

```julia
julia> mancellin2(a, i) = getindex(a, i...)

julia> @btime mancellin2(a, i) setup = (a = [1 2 3 4; 5 6 7 8]; i = (3, ))
  1.971 ns (0 allocations: 0 bytes)

julia> @btime mancellin2(a, i) setup = (a = [1 2 3 4; 5 6 7 8]; i = 3)
  139.657 ns (2 allocations: 32 bytes)

```

1.6:

```julia
julia> @btime mancellin2(a, i) setup = (a = [1 2 3 4; 5 6 7 8]; i = (3, ))
  1.981 ns (0 allocations: 0 bytes)

julia> @btime mancellin2(a, i) setup = (a = [1 2 3 4; 5 6 7 8]; i = 3)
  1.972 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 21, 2020, 1:41pm UTC](https://discourse.julialang.org/t/splatting-of-an-integer/52160/3 "2020-12-21T13:41:53Z")

</div>

One thing you should consider is using `StaticArrays`, which are like tuples, but have an algebra.

---

<div class="post-metadata">

**Author:** ![mancellin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mancellin/32/13971_2.png) [@mancellin](https://discourse.julialang.org/u/mancellin)\
**Post date:** [December 21, 2020, 3:28pm UTC](https://discourse.julialang.org/t/splatting-of-an-integer/52160/4 "2020-12-21T15:28:33Z")

</div>

Good point, `StaticArrays` are awesome. But splatting is costlier than with tuples (at least in version 1.5, maybe it has also been fixed in version 1.6).

---

<div class="post-metadata">

**Author:** ![mancellin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mancellin/32/13971_2.png) [@mancellin](https://discourse.julialang.org/u/mancellin)\
**Post date:** [December 21, 2020, 3:31pm UTC](https://discourse.julialang.org/t/splatting-of-an-integer/52160/5 "2020-12-21T15:31:35Z")

</div>

Great! Thank you for trying this out. And thanks to the developers for fixing my issues before I know I have them 🙂
