# How to create fixed-length tuple without heap allocation

**URL:** <https://discourse.julialang.org/t/how-to-create-fixed-length-tuple-without-heap-allocation/10250>\
**Category:** General Usage\
**Created:** [April 9, 2018, 5:39pm UTC](https://discourse.julialang.org/t/how-to-create-fixed-length-tuple-without-heap-allocation/10250 "2018-04-09T17:39:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [April 9, 2018, 5:39pm UTC](https://discourse.julialang.org/t/how-to-create-fixed-length-tuple-without-heap-allocation/10250/1 "2018-04-09T17:39:19Z")

</div>

The following program allocates memory (according to the `@time` macro) proportionally to `n`, which suggests that each invocation of `tuple` creates something on the heap. Note that the program is written so that the length of the tuple is known at compile time, and hence, in principle, it should be possible to allocate the tuple on the stack. Is there a different technique to create a fixed-length tuple without heap allocation? I think I might be able to solve this problem with a generated function, but maybe something simpler is available?

```julia
module test_memtup
function sumtup(j, ::Val{N}) where N
    v = tuple(((j + i) for i=1:N)...)
    sm = 0
    for k = 1 : N
        sm += v[k]
    end
    sm
end

function test(n)
    N = 6
    s = 0
    for k = 1 : n
        s += sumtup(k, Val{N}())
    end
    s
end
end

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 9, 2018, 5:59pm UTC](https://discourse.julialang.org/t/how-to-create-fixed-length-tuple-without-heap-allocation/10250/2 "2018-04-09T17:59:58Z")

</div>

You should use the `ntuple` function to generate `v`:

```julia
function sumtup(j, ::Val{N}) where N
    v = ntuple(i->j+i,Val{N}())
    sm = 0
    for k = 1 : N
        sm += v[k]
    end
    sm
end

@benchmark sumtup(3,Val{5}())
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 2.338 ns (0.00% GC)
  median time: 2.630 ns (0.00% GC)
  mean time: 2.705 ns (0.00% GC)
  maximum time: 67.517 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 9, 2018, 6:05pm UTC](https://discourse.julialang.org/t/how-to-create-fixed-length-tuple-without-heap-allocation/10250/3 "2018-04-09T18:05:45Z")

</div>

Although this would be the simplest:

```julia
sumtup(j, N) = sum(ntuple(i->j+i,N))
@benchmark sumtup(3,Val{5}())
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 2.630 ns (0.00% GC)
  median time: 2.923 ns (0.00% GC)
  mean time: 2.928 ns (0.00% GC)
  maximum time: 42.673 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```
