Is Vector{Union{T, Missing}} double the size of Vector{T}?

Viewed 60

Is it right or there is a problem with Base.summarysize or ?

julia> x = rand(Int8, 100);

julia> Base.summarysize(x)
140

julia> x = allowmissing(x);

julia> Base.summarysize(x)
240
1 Answers

It adds one single byte per element in this case:

Julia can now also store "isbits Union" values inline in an Array, as opposed to requiring an indirection box. The optimization is accomplished by storing an extra "type tag array" of bytes, one byte per array element, alongside the bytes of the actual array data. https://docs.julialang.org/en/v1/devdocs/isbitsunionarrays/

For example consider the case with an Int16:

julia> x = rand(Int16, 100);

julia> Base.summarysize(x)
240

julia> x = allowmissing(x);

julia> Base.summarysize(x)
340

where 340 = (2+1)*100 + 40

  • 2 bytes: the size of Int16
  • 1 byte: extra-byte (the type tag)
  • 40 bytes: meta-data (with the shape, offset...) independent of the array's size

Note that using less than a byte for the type tag would result that the elements are no longer aligned at bytes boundaries.

Related