# Why does it make allocation when using tuples parameters in closure?

**URL:** <https://discourse.julialang.org/t/why-does-it-make-allocation-when-using-tuples-parameters-in-closure/98369>\
**Category:** Performance\
**Created:** [May 5, 2023, 7:41am UTC](https://discourse.julialang.org/t/why-does-it-make-allocation-when-using-tuples-parameters-in-closure/98369 "2023-05-05T07:41:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wly/32/49517_2.png) [@wly](https://discourse.julialang.org/u/wly)\
**Post date:** [May 5, 2023, 7:41am UTC](https://discourse.julialang.org/t/why-does-it-make-allocation-when-using-tuples-parameters-in-closure/98369/1 "2023-05-05T07:41:24Z")

</div>

The following code have some allocation:

```julia
using BenchmarkTools

function memoize_test(n_outputs)
    function foo_i(i::Int64, u...)
    end
    return [(u...) -> foo_i(i, u...) for i in 1:n_outputs]
end

fun = memoize_test(10)

u_ = rand(28)
@btime $fun[1]($(u_)...)

```

Result:

```julia
1.560 μs (59 allocations: 1.14 KiB)

```

Why does it make some allocation?  
If you could give me some tips, thank you very much.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 5, 2023, 7:56am UTC](https://discourse.julialang.org/t/why-does-it-make-allocation-when-using-tuples-parameters-in-closure/98369/2 "2023-05-05T07:56:57Z")

</div>

It allocates because the length of `u` isn’t known to the compiler. The following doesn’t, for example:

```julia
julia> @btime $fun[1]($(Tuple(u_))...)
  2.946 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![wly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wly/32/49517_2.png) [@wly](https://discourse.julialang.org/u/wly)\
**Post date:** [May 5, 2023, 8:27am UTC](https://discourse.julialang.org/t/why-does-it-make-allocation-when-using-tuples-parameters-in-closure/98369/3 "2023-05-05T08:27:09Z")

</div>

Great. Thank you very much.  
However, when I use the package TimerOutputs, why does the result show that there are some allocations?

```julia
using TimerOutputs
function test_time()
    u = rand(28)
    u_ = Tuple(u)
    fun = memoize_test(10)
    reset_timer!()
    for i = 1:10
        @timeit "fun" fun[1](u_...)
    end
    show(TimerOutputs.get_defaulttimer());
end
test_time()

```

Result:

```julia
 ────────────────────────────────────────────────────────────────────
                            Time Allocations
                   ─────────────────────── ────────────────────────
 Tot / % measured: 28.3μs / 73.1% 12.2KiB / 93.2%

 Section ncalls time %tot avg alloc %tot avg
 ────────────────────────────────────────────────────────────────────
 fun 10 20.7μs 100.0% 2.07μs 11.4KiB 100.0% 1.14KiB
 ────────────────────────────────────────────────────────────────────

```

Thank you again.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 5, 2023, 8:32am UTC](https://discourse.julialang.org/t/why-does-it-make-allocation-when-using-tuples-parameters-in-closure/98369/4 "2023-05-05T08:32:00Z")

</div>

Some allocations would arise from allocating the vector of functions. If the lengths are known to be constant, perhaps you may use `Tuple`s, or `StaticArray`s
