A list knows what it holds
The plain way to write a list is a count and then the elements, each written as it would be on its own. Most formats stop there. But a list has something a single value lacks: all its elements are of one type, and the writer sees them all before it writes any. Decisions can be made once for the whole list.
The layouts built for fast access use that for speed. A list of numbers is one block of fixed-width values, so the thousandth is found by multiplying, as chapter 7 and chapter 14 show. This chapter is about the packed layout, which uses it for size. Choose what the list holds.
- Every list starts the same way: how many bytes it takes, so a reader can step over it, and how many elements it has.
- Whole numbers are each written the shorter way, LEB128 or plain bytes. One bit per element would have to say which. Usually they all go the same way, and then two bits next to the count say so for the whole list, and the bits per element are left out. Try
3, 70, 1000, then make one of them3000000. - Yes and no take one bit each.
- Enums take one, two or four bits each, as in chapter 8.
- Text is each text with its length in front. There is nothing to share between the elements here.
- Records in a list are written whole, one after another, each with its own length.
Lists with gaps
A sensor reports every minute, and sometimes it has nothing to report. A column of a table has cells that were never filled in. The honest way to store that is a list in which a position can be empty, and it is different from a list that is shorter, and different from a list with a zero in it.
Tick "the schema allows gaps" above and put a dash where a value is missing: 3, -, 1000, -, -, 70.
- One bit per position says whether it is empty. That is all an empty position costs.
- The values that exist are written with no holes between them. An empty position takes no room among the values, whatever the type. A list of a thousand positions with ten values in it is 125 bytes of bits and ten values.
- The schema says which kind of list it is. A field declared
t.u32.arrayhas no gaps and no bits for them. One declaredt.u32.array_with_optionalshas both. A list that never has gaps does not pay for the possibility.
The same elsewhere
- JSON has it:
[3, null, 1000], at four bytes for eachnull. - Protocol Buffers does not. A repeated field holds values and nothing else. The usual way out is to wrap each number in a message of its own, which costs at least two bytes per element, the present ones included.
- FlatBuffers and Cap'n Proto do not for numbers. A list of numbers is a block, and a block has no holes. People pick a value that means "none", such as −1, and hope it never occurs as data.
- The formats built for tables of data have had this for a long time. Apache Arrow keeps a bit per position apart from the values. Parquet also leaves the missing values out, which is what Dagr does.
Lists of mixed elements
The last chapter ended with unions: a value that is one of several kinds. Put those in a list and you have something schemas usually cannot say: a list whose elements are not all of one type, and are typed all the same. A column of a spreadsheet is the everyday example.
UnionType("Cell", types=[("whole", t.i64), ("real", t.f64), ("text", t.utf8), ("flag", t.bool)])
"column" >> t.ref("Cell").array_with_optionals
Type a column. A dash is an empty cell, yes and no are flags, and anything that is not a number is text.
- The headers stand together, in front. A single union is a header and then its value. A list of them is all the headers, then all the values. Light up one cell and you find it in two places.
- That is what makes the list more than a pile. "Which cells are text?" or "is there anything in here I do not understand?" is answered from the headers alone, a byte per cell, without touching a value.
- Each value is as small as its type allows. The 7 is one byte, the 2.5 is a 16-bit float, the yes is a byte. The header carries, besides the variant, how the value was written. So no value needs a length of its own unless it is text or something that nests.
- Gaps work as before: a bit per position, and an empty cell has neither a header nor a value. With gaps the list also says how long the headers are in all, so that a reader can find where the values begin.
- An unknown variant can be stepped over. A reader built from an older schema reads the cells it knows and reports the others as unknown, because every header says how long its value is.
The same elsewhere
- JSON and CBOR allow mixed lists freely, because every value says what it is anyway. Nothing tells you in advance which kinds to expect.
- Protocol Buffers has no list of
oneof. You write a message that holds theoneofand repeat the message. Each element then carries a label and a length around its value. - FlatBuffers has lists of unions, as two lists side by side: the types and the pointers. It is the same split as Dagr's headers and values. The members are tables reached by pointer, so a plain number has to be wrapped in one.
- Cap'n Proto has no bare union to put in a list. You make a struct with a union in it, and every element is as large as the largest member.
What all this costs
These lists are small because nothing in them has a fixed size, and that has a price which is always the same one.
- To reach the hundredth element you pass the first ninety-nine. The values have different lengths, and with gaps you also count bits to learn which value belongs to which position. Reading a list from front to back is fast. Jumping into the middle is not.
- You can buy the jump back, list by list. A list of numbers marked
rawis written at full width, as chapter 2 showed for floats, and then the hundredth is found by multiplying. Or the record uses a layout built for access, and all its lists are blocks. - There are many encodings. Each combination in this chapter, of element type and with or without gaps, is laid out in its own way, about twenty in the packed layout alone, in every language Dagr generates code for. You do not see that as a user. It is the largest single piece of work in the format, and the place where two implementations are most likely to disagree, which is why every one of them is tested byte for byte against the others.
- Lists of unions are the most general and the slowest. A list of numbers is read by a loop over numbers. A list of cells is a decision per element. If all your cells are numbers, say so in the schema.
What these panels are.
- The bytes are Dagr's: a field of a packed node, without the tag in front. The encoder is checked against Dagr's reference writer for every kind of list on this page, with and without gaps.
- The JSON size is the list as compact JSON, counted by the page.
- Whole numbers here are
u32, from 0 to 4,294,967,295. Other widths work the same way with other limits.
Try it
- Make a list of eight small whole numbers. Then change one to
3000000. Which bytes appear, and what would have happened if all eight were that large? - Make a list of sixteen yes/no values with gaps allowed, and leave ten of them empty. How many bytes hold the values?
- With whole numbers and gaps allowed, compare
0, 0, 0, 0with-, -, -, -. Then say what each one means. - In the column panel, make every cell a text. What do the headers look like, and what would a list of plain texts have saved?
- Still in the column panel, find the cell whose value takes the most bytes for the least information. (Try
0.1.) - Turn gaps off in the column panel with no empty cell in it. Which byte disappears besides the bits, and why was it needed?
What comes next
One union and lists of that union are enough to describe any data at all: a value is a number, a text, a list of values, or a set of named values. That is what JSON is, and what CBOR is in binary. Chapter 10 writes that schema in Dagr and puts the result next to CBOR, to see what a format built on schemas has to offer when there is no schema to speak of.
