Why none of the earlier layouts will do
Dagr's graphs are compact because lengths and pointers, and in packed nodes numbers too, take only the room they need. That is exactly what makes them impossible to change in place. If a counter goes from 100 to 100,000 its LEB128 form grows by two bytes, and everything after it would have to move, and every pointer across it would be wrong.
Changing a value where it lies takes the opposite design: every field at a fixed place, with a fixed size, known before the program runs. Dagr's third container, the SharedBuffer, is that.
Status = SharedBuffer("Status", root_type="Machine", node_types=[
Node("Machine", frozen=True, fields=[
"temperature" >> t.f32 >> required,
"rounds" >> t.u64 >> required,
"level" >> t.u8 >> required,
"running" >> t.bool >> required,
"name" >> t.utf8 >> cap(12),
]),
])
The node is marked frozen because a shared buffer needs what frozen promises: a layout that never changes. Its bytes follow the rules of this chapter, though, and not those of the frozen layout in chapter 5.
That schema is 32 bytes, always. Change the values below and watch which bytes change.
- A change touches only its own bytes. Go from 100 rounds to 100000. Eight bytes are rewritten and the other 24 are not looked at. In a packed graph the value would have grown and everything behind it moved.
- Dagr chose the order. The schema lists
temperaturefirst, and the region starts withrounds. The widest fields go first, so that each sits at a multiple of its own size, as chapter 14 described, with as little padding as possible. - Small things share a byte.
runningis one bit. So is the mark that says whethernameis set. - Text has a capacity.
cap(12)reserves twelve bytes and a length. Type a longer name and it is cut. A short name leaves the rest of the room unused, and it is there all the same.
What is given up
A SharedBuffer keeps the schema language and little else of what the earlier chapters praised.
- It is not a graph. A node inside another is stored inside it, whole. The same node in two places is stored twice. A type that contains itself would have no fixed size, so it is refused.
- Nothing has a variable length. Every text and every list declares its capacity, and pays for it whether it is used or not.
- The bytes do not say what they are. There is no framing, no header, no mark of the schema. Byte 0 is the first field. Two programs generated from different versions of the schema will read each other's memory without complaint and get nonsense.
- The schema hardly changes. Adding a field moves or enlarges something, and both sides are looking at the same bytes at the same time, so there is no "old reader, new writer" to be gentle with. Dagr records the layout when you first build and refuses a change that would move a byte. If you expect to add fields, reserve room for them from the start, and put a version number in the region yourself.
What this buys is that reading a field is one memory access at an address known in advance, and so is writing one.
Two programs at once
Now let a second program read the region while the first is changing it. Each single field is safe enough. The trouble starts with values that belong together.
Below, a writer moves 10 from a to b, again and again. The two always add up to 100, before a transfer and after it. A reader takes a look at both. You decide who takes the next step.
The writer
The reader
What the reader has reported
With no protection, press "interleave them badly for me". The reader reports an a from before a transfer and a b from after it: 110 in total, a state the writer never created. This is called a torn read. No single step did anything wrong.
A counter instead of a lock
The obvious cure is a lock: whoever comes second waits for the first. Then a slow or stopped reader can hold up the writer, which is the last thing you want from a program whose job is to keep up with a machine.
Switch the protection to seqlock and try the same bad interleaving.
- The writer adds one to a counter before it starts and one more when it is done. So the counter is odd exactly while a write is in progress.
- The reader notes the counter, copies what it wants, and looks at the counter again. If the counter was odd, or has changed, the copy may be torn. The reader throws it away and tries again.
The writer never waits for anyone. The reader never reports a state that did not exist. What the reader pays is that it may have to try more than once, and under a writer that never pauses it could be kept trying for a while.
This scheme is safe here for a reason that goes back to the top of the chapter. A torn copy is just wrong numbers, to be thrown away. In a format with pointers a torn copy could contain half a pointer, and following it before the second look at the counter could crash. A SharedBuffer has no pointers.
What the picture leaves out
The reader copies while the writer may be writing, on purpose. Many languages do not allow that with ordinary reads and writes. In C, C++ and Rust, two threads that touch the same memory with no coordination, one of them writing, make the program wrong, whatever is done with the result afterwards, and a compiler may assume it never happens. Throwing the torn copy away does not repair that. The copy itself has to be made of reads and writes that the language allows to race.
So in the generated code every read and write of the shared memory during a copy is an atomic one of the weakest kind, a word at a time. It costs the same instructions as a plain copy, and the language defines what happens. The writer, for its part, never changes the shared memory field by field. It prepares its changes in a private copy and publishes that between the two steps of the counter, which is the copy the panel shows.
That holds in every language Dagr generates a SharedBuffer for. It did not always: until late 2026 five of them copied with plain reads and writes, which is how seqlocks are commonly written and which worked in every test. A reader of this guide asked the question, and the generators were changed. Where a tool exists to check it, it was checked: the race detectors of Rust, Go and Odin are silent on the new code, and Rust's reported the old.
Other ways to share
How a region is shared is declared once for the whole region, and it decides what the generated code lets you do. It is not a property of a field, because, as the panel shows, the problem is never one field.
| declared as | what a reader gets | suited to |
|---|---|---|
none | plain reads and writes | one owner, or your own locking |
seqlock | whole, consistent copies; may retry | state that is read often and written by one |
double_buffer | the newest whole state, never a retry | readers that may not be delayed |
ring | records in order, each exactly once | a stream of events between two parties |
atomic, seqlock_mw, mpsc_ring | the same ideas with several writers | many producers, one region |
With several programs there is one more thing to plan for: a writer can die in the middle of a write. The counter then stays odd, and a patient reader would wait forever. The variants for several writers record which process holds the region and notice when that process is gone.
Where this is used
- Two processes on one machine, mapping the same memory. One measures, the other displays or collects, and neither sends the other anything.
- Two languages in one process. A Rust library and a Swift application can pass a pointer to a region across their boundary. Both have generated code for the same offsets, so nothing is converted.
- A file that is the state. Map a file into memory, change fields, and the operating system writes it to disk. After a restart the state is simply there.
- A browser's workers, sharing one buffer between threads.
- Small devices with no memory allocator, where a layout known at build time is a requirement.
Programmers have always done this by hand, with a struct of their language placed over shared memory. It works until the second language or the second compiler arrives and lays the same struct out differently, and it leaves the protocol between reader and writer to be invented each time. What Dagr adds is one schema from which every language gets the same offsets, and the sharing protocol generated with them. Two small complete examples are in Dagr's repository: Rust and Swift over one region, and a region in C.
What these panels are.
- The first uses Dagr's real layout. The position of every field was computed by Dagr and is only used by the page. The bytes the page writes are read back by Dagr's reference reader in the checks, and the values agree.
- The second is a simulation. A web page cannot show you two programs truly running at once, so you play the scheduler. The steps are those of the real scheme. The generated code also takes care of things the picture leaves out, such as the exact guarantees it asks of the processor for each read and write, and what each language allows two threads to do with the same memory.
Try it
- In the first panel, tick and untick
running. Which byte changes, and what else lives in it? - Type a name of twenty letters. What is stored? Which earlier chapter's format would have stored all twenty, and at what cost here?
- In the second panel, with no protection, find an interleaving in which the reader reports 90.
- With the seqlock, let the reader take its first step while the counter is odd. What does it do?
- With the seqlock, can you make the reader report a total other than 100? What would you have to change about the writer to make that possible?
What comes next
Most of this guide assumed that the writer and the reader have the same schema. A SharedBuffer depends on that completely. Everywhere else there is room for a schema to change while old data and old programs are still around. Chapter 16 is about how much room, and about the tool that watches it.
