DagrA guide to binary formats, and to Dagrdraft

9 Chapter nine

Lists: gaps, bits and mixed elements

Most of the bytes in most data are in lists. A record has a dozen fields, and one of them is a list of ten thousand things. So how a format writes a list decides more about its size and speed than anything else it does. This chapter is about what Dagr does with them, and about two kinds of list most formats do not have.

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.

a list of:
how long, how many, lengthswhich positions are emptywhich elements are written the long wayvaluestext

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.

The same elsewhere

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.

light up:
how long, how many, lengthswhich cells are emptyheaders: what each cell isnumbers and flagstext

The same elsewhere

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.

What these panels are.

Try it

  1. 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?
  2. Make a list of sixteen yes/no values with gaps allowed, and leave ten of them empty. How many bytes hold the values?
  3. With whole numbers and gaps allowed, compare 0, 0, 0, 0 with -, -, -, -. Then say what each one means.
  4. 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?
  5. Still in the column panel, find the cell whose value takes the most bytes for the least information. (Try 0.1.)
  6. 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.