DagrA guide to binary formats, and to Dagrdraft

14 Chapter fourteen

Alignment: where exactly the bytes sit

Until now it has not mattered at which address a value lies, only that a reader can find it. For one kind of code it matters a great deal: code that works on thousands of numbers at once. This chapter is about what that code asks for, and how a Dagr schema can promise it for the one list that needs it.

What alignment is

Memory is a long row of bytes, each with an address: 0, 1, 2 and so on. A value is aligned when its address is a multiple of some number, usually its own size. A four-byte number is aligned at address 8 or 12, and not at 9.

Processors care for a simple reason. They fetch memory in chunks, and a value that lies across the edge of two chunks takes more work. For a single number on today's common processors the extra work is small. It grows with the size of what is fetched:

Move the four numbers below along the row and see who is content.

    four 32-bit floats, 16 bytesa bar: every 16th address

    Data that is not where such code wants it can always be copied to a place that is. For a few numbers nobody would notice. For the cases where alignment matters, a million readings or a model's weights, that copy is the very work one hoped to avoid by reading data in place.

    Dagr aligns nothing unless asked

    Chapter 7 showed FlatBuffers and Cap'n Proto putting every value at a multiple of its size, and Dagr's regular layout putting values wherever they fall. That choice keeps Dagr's bytes small, and for single numbers it costs little.

    For the list that will be fed to vector code or a graphics card, a schema can say so, on that field alone:

    Sensor = Node("Sensor", fields=[
        "label"    >> t.utf8,
        "readings" >> t.f32.array >> aligned(16),
    ])

    The number is the boundary you need: 8, 16, 32, 64, 128 or 256. Below is that node. Choose a boundary and watch where the readings land. Each row of the picture is 16 addresses, so a boundary is easy to see.

    aligned to:
    bytes in front of the buffer:
    readingspadding, framingtextpointerstablea bar: a boundarydashed: the first readingstriped: not ours

    Nothing about the list itself changed. It is still a count followed by plain four-byte numbers, and a reader that knows nothing about alignment reads it as before. All that was added is padding, in two places:

    Dagr writes a buffer from its end, so every position is fixed by its distance from the end. Make that distance a multiple of 16 and the length a multiple of 16, and the position from the front is a multiple of 16 too.

    What the padding costs

    Try 64 with three readings. The padding outweighs the data. A boundary of N can cost up to nearly N bytes at each of the two places, whatever the size of the list. For a list of a hundred thousand numbers that is nothing. For a list of three it is absurd, which is why this is a decision for each field and not a property of the format.

    The half that the format cannot promise

    A format decides where bytes are within a buffer. It has no say in where the buffer is placed in memory. So the promise comes in two halves, and only together do they make an aligned address:

    Your half is usually easy. A file mapped into memory by the operating system starts on a page boundary, and a page is thousands of bytes, a multiple of every boundary Dagr offers. Memory allocators can be asked for an aligned block. The case that goes wrong is a buffer copied to wherever was convenient.

    A buffer inside something else

    Often a buffer is not alone. It arrives inside someone else's envelope, behind a few bytes that are not Dagr's: a message length, a protocol's own fields. The receiver aligns the whole message, and the Dagr buffer begins a few bytes in. Every list in it is then off by exactly those bytes.

    Set "bytes in front of the buffer" to 5 or 12 in the panel. The writer can be told how many bytes will precede the buffer, and it shifts its padding so that the readings are aligned where they will actually be. The bytes on the wire differ only in padding. A reader is not told and does not need to be.

    What you get for it

    The generated code can hand you the list as it lies in the buffer: an address and a count, with no element decoded and nothing copied. That is what vector code and graphics interfaces take.

    Two cautions belong here. On machines where the graphics card has its own memory, "no copy" means no copy on the processor's side: the data still travels to the card. And the memory must stay mapped for as long as anything is reading it.

    The limits

    When a list is not aligned, nothing is lost but the last step: a plain list of numbers is still one block, and the readers copy it out in a single move instead of number by number.

    Next to the others

    what is alignedto what
    Protocol Buffersnothing
    FlatBuffersevery valueits own size; more on request
    Cap'n Protoevery valueits own size, in 8-byte words
    Apache Arrowevery column8 bytes, 64 recommended
    Dagrthe fields that ask8 to 256, chosen per field

    Arrow deserves its own sentence. If your data is tables of numbers for analysis, Arrow is the format that whole field has agreed on, and it is built around exactly this. Dagr's aligned fields are for data that is mostly a graph of records and has, somewhere in it, a few large blocks of numbers that should not be copied.

    The exact rules, with worked examples for every case, are in the specification.

    What these panels are.

    Try it

    1. In the first panel, find every start address at which a single float is aligned and the block of four is not.
    2. In the second panel, with no alignment, change the label's length by one letter at a time. What happens to the address of the first reading?
    3. Now choose 16 and do the same. What moves, and what stays?
    4. Choose 32 and go from five readings to nine. When does the buffer grow, and by how much?
    5. Set 5 bytes in front. The first reading's position within the buffer is no longer a multiple of 16. Why is that right?

    What comes next

    Every chapter so far was about bytes that are written once and read afterwards. Chapter 15 is about memory that two programs look at at the same time, while one of them is still changing it. Dagr's name for that is a SharedBuffer.