# When will a vector of structs be contiguous in memory?

**URL:** https://discourse.julialang.org/t/when-will-a-vector-of-structs-be-contiguous-in-memory/70181
**Category:** Performance
**Created:** [October 21, 2021, 10:32pm UTC](https://discourse.julialang.org/t/when-will-a-vector-of-structs-be-contiguous-in-memory/70181 "2021-10-21T22:32:47Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 21, 2021, 11:49pm UTC](https://discourse.julialang.org/t/when-will-a-vector-of-structs-be-contiguous-in-memory/70181/2 "2021-10-21T23:49:37Z")

</div>

Maybe you can try `Pair{V, Union{Nothing,Vector{V}}}` or a similar named tuple instead of a tuple

```julia
julia> Base.allocatedinline(Tuple{Int,Union{Vector{Int},Nothing}})
false

julia> Base.allocatedinline(Pair{Int,Union{Vector{Int},Nothing}})
true

julia> Base.allocatedinline(@NamedTuple{first::Int,second::Union{Vector{Int},Nothing}})
true

julia> sizeof(Pair{Int,Union{Vector{Int},Nothing}})
16

```

Using a null pointer would mean using an undef field. However, you don’t get inlined allocation with this approach

```julia
julia> struct P{K,V}
           a::K
           b::V
           P{K,V}(a, b) where {K,V} = new{K,V}(a,b)
           P{K,V}(a) where {K,V} = new{K,V}(a)
       end

julia> P{Int,Vector{Int}}(1)
P{Int64, Vector{Int64}}(1, #undef)

julia> Base.allocatedinline(P{Int,Vector{Int}})
false

```

---

_[View the full topic](https://discourse.julialang.org/t/when-will-a-vector-of-structs-be-contiguous-in-memory/70181)._
