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:
- Vector instructions, often called SIMD, handle four, eight or sixteen numbers in one step. They load 16, 32 or 64 bytes at once. Their fastest forms require that block to start at a multiple of its size, and fail outright when it does not.
- Graphics cards are handed memory by address and offset, and the interfaces that do this state which offsets they accept: multiples of 16, of 256, depending on the use.
Move the four numbers below along the row and see who is content.
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.
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:
- After the readings, so that the list ends at the right distance from the end of the buffer.
- At the very front, after the framing, so that the buffer's whole length is a multiple of the boundary.
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:
- Dagr's half: the first reading is a multiple of N bytes from the start of the buffer.
- Your half: the buffer starts at an address that is a multiple of N.
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.
- On a processor, the list can be read by vector instructions directly, in their aligned forms.
- On a graphics card, the usual way is to hand over the whole mapped file once and name each list by its offset in it. An offset that is a multiple of the right number is exactly what those interfaces ask for.
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
- Only regular and frozen nodes. In a packed node the request is ignored, because packed data is sized by its values and has no fixed places.
- Only lists of plain numbers, text, and bytes. A list of nodes or of optional values has no single block to align.
- Not in a DataSink's records, which are always packed. And the allowance for bytes in front of the buffer does not yet extend to a graph that has a header of the kind in chapter 11.
- The numbers are little-endian. Every common processor and graphics card today is too.
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 aligned | to what | |
|---|---|---|
| Protocol Buffers | nothing | |
| FlatBuffers | every value | its own size; more on request |
| Cap'n Proto | every value | its own size, in 8-byte words |
| Apache Arrow | every column | 8 bytes, 64 recommended |
| Dagr | the fields that ask | 8 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.
- The first is a picture of the rule, not a measurement. Which instructions tolerate which addresses differs between processors.
- The second computes real Dagr bytes in your browser, checked against Dagr's reference writer for every boundary and many lengths and offsets: identical. The check also confirms the claim itself, that the first reading lands on its boundary.
- Addresses start at zero in the picture, as if the memory began exactly on a boundary. That is "your half" from above.
Try it
- In the first panel, find every start address at which a single float is aligned and the block of four is not.
- 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?
- Now choose 16 and do the same. What moves, and what stays?
- Choose 32 and go from five readings to nine. When does the buffer grow, and by how much?
- 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.
