...
A single byte containing 1 for the value of true
. The and 0 for the value of false
does not need any representation as it stored as a default zero-size field.
If the number of fields is N and t is an array that stores a binary tuple we can find the answers for the following:Does the tuple contain a nullmap?
hasNullmap = t[0] & 0b100;
How many bytes are occupied by the nullmap?
nullmapBytes = hasNullmap ? (N + 7) / 8 : 0;
How many bytes are occupied by one offset table entry?
offsetEntryBytes = 1 << (t[0] & 0b11);
...
What is offset of the value area?
valueBaseOffset = 1 + nullmapBytes + offsetTableBytes;
What is the whole tuple size?
...
In order to build a tuple using minimum possible space it is required to learn two things:
...
what is the total length of all non-null values
...
. After that we can figure out the minimum possible size of the offset table entries.
Thus, generally speaking, building a binary tuple is a two-pass procedure. Sometimes it might be possible to turn this into a single pass (almost) by over-provisioning the allocated storage for the worst case and then fixing it up at the end.
...