# Space usage of nested tuples

**URL:** <https://discourse.julialang.org/t/space-usage-of-nested-tuples/102872>\
**Category:** General Usage\
**Created:** [August 16, 2023, 5:34pm UTC](https://discourse.julialang.org/t/space-usage-of-nested-tuples/102872 "2023-08-16T17:34:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilanggear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilanggear/32/862_2.png) [@ilanggear](https://discourse.julialang.org/u/ilanggear)\
**Post date:** [August 16, 2023, 5:34pm UTC](https://discourse.julialang.org/t/space-usage-of-nested-tuples/102872/1 "2023-08-16T17:34:23Z")

</div>

Are vector elements (eg tuples with three fields each) in a structure such as `myList = Vector{Tuple{UInt32, UInt16, UInt16}}` stored as pointers to tuples, or directly as three UInts? In other words, would myList and myMatrix (initialzed as zeros(N, 3) ) take the same amount of space on a per element basis?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 16, 2023, 5:43pm UTC](https://discourse.julialang.org/t/space-usage-of-nested-tuples/102872/2 "2023-08-16T17:43:34Z")

</div>

> [@ilanggear](#):
>
> `Vector{Tuple{UInt32, UInt16, UInt16}}` stored as pointers to tuples, or directly as three UInts?

Directly (“inline”) as three integers, much like an array of `struct`. In general, immutable “bitstype” data is stored inline in an array (or elsewhere), without pointers.

Note that a mixed-type tuple will be subject to [struct padding](https://www.javatpoint.com/structure-padding-in-c), so there may internally be additional padding bytes between entries depending on the sizes. In the particular case of `Tuple{UInt32, UInt16, UInt16}` there is no padding and it requires 8 bytes per entry:

```julia
julia> a = Tuple{UInt32, UInt16, UInt16}[(3,4,5)]
1-element Vector{Tuple{UInt32, UInt16, UInt16}}:
 (0x00000003, 0x0004, 0x0005)

julia> sizeof(a)
8

```

but for example `Tuple{UInt8, UInt64}` occupies 16 bytes since the second entry is padded to an 8-byte boundary:

```julia
julia> a = Tuple{UInt8, UInt64}[(3,4)]
1-element Vector{Tuple{UInt8, UInt64}}:
 (0x03, 0x0000000000000004)

julia> sizeof(a)
16

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 16, 2023, 5:49pm UTC](https://discourse.julialang.org/t/space-usage-of-nested-tuples/102872/3 "2023-08-16T17:49:41Z")

</div>

Yes. `ismutabletype(Tuple{UInt32, UInt16, UInt16}) == false` and most immutable objects are stored inline in arrays.

As for memory use, I will remark that the two aren’t quite comparable. Your tuples are heterogeneous while a `Matrix` must be homogeneous. But for homogeneous tuples, the two would be comparable except for possible struct padding (as discussed in the above post).

If you’re considering a layout like `zeros(UInt32,N,3)`, you might also consider the `StaticArrays.jl` package for forming a `Vector{SVector{3,UInt32}}`. This is memory-wise identical to a `Vector{NTuple{3,UInt32}}` (or a `zeros(UInt32,3,N)` assuming no padding – note the dimensions were swapped from your question as Julia is column-major) but provides array-like behavior from the `SVector`s rather than tuple-like behavior.

---

<div class="post-metadata">

**Author:** ![ilanggear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilanggear/32/862_2.png) [@ilanggear](https://discourse.julialang.org/u/ilanggear)\
**Post date:** [August 16, 2023, 6:38pm UTC](https://discourse.julialang.org/t/space-usage-of-nested-tuples/102872/4 "2023-08-16T18:38:19Z")

</div>

Thanks!
