# Iterating on Tuple allocates memory if values don't have identical types

**URL:** <https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410>\
**Category:** Performance\
**Created:** [April 13, 2022, 2:12am UTC](https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410 "2022-04-13T02:12:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdebecdelievre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdebecdelievre/32/35401_2.png) [@jdebecdelievre](https://discourse.julialang.org/u/jdebecdelievre)\
**Post date:** [April 13, 2022, 2:12am UTC](https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410/1 "2022-04-13T02:12:52Z")

</div>

Hi,  
Could someone help me understand why iterating on a tuple seems much slower if the values have non-unique types?

Here is a very simple example:

```julia
a = 1.
b = [1.]
c = [1.]
A = (a, b)
B = (b, c)
@btime for d=$A; d; end
@btime for d=$B; d; end

```

returns:

```julia
16.892 ns (1 allocation: 32 bytes)
1.471 ns (0 allocations: 0 bytes)

```

Should I set up the iteration differently?

Thank you very much  
Jean

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 13, 2022, 2:42am UTC](https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410/2 "2022-04-13T02:42:28Z")

</div>

First of all, welcome to our community! 🎊

Second, because the code will be inherently [type unstable](https://docs.julialang.org/en/v1/manual/faq/#man-type-stability)? The most relevant performance tip is probably: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions)

If you gonna go this way (what is really kinda strange to me for real code), probably the best you can do is to apply the same function (that does the same as the body of the loop) to each element separately.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 13, 2022, 3:21am UTC](https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410/3 "2022-04-13T03:21:23Z")

</div>

> [@jdebecdelievre](#):
>
> Should I set up the iteration differently?

Tuple de-structuring and indexing still work

```julia
@btime let (x,y)=$A; (x,y); end
@btime begin ($A[1],$A[2]); end

```

yielding

```julia
  2.200 ns (0 allocations: 0 bytes)
  2.400 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [April 13, 2022, 5:51am UTC](https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410/4 "2022-04-13T05:51:14Z")

</div>

You can also try `foreach` (or `map` if you need the result):

```julia
julia> @btime foreach(identity, $A)
  1.453 ns (0 allocations: 0 bytes)

julia> @btime map(identity, $A)
  2.585 ns (0 allocations: 0 bytes)
(1.0, [1.0])

```

---

<div class="post-metadata">

**Author:** ![jdebecdelievre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdebecdelievre/32/35401_2.png) [@jdebecdelievre](https://discourse.julialang.org/u/jdebecdelievre)\
**Post date:** [April 13, 2022, 6:56am UTC](https://discourse.julialang.org/t/iterating-on-tuple-allocates-memory-if-values-dont-have-identical-types/79410/5 "2022-04-13T06:56:39Z")

</div>

Thank you for the warm welcome and the replies. Type instability is indeed the reason, now I get it.  
The alternative syntaxes are also very welcome, thank you!
