DagrA guide to binary formats, and to Dagrdraft

15 Chapter fifteen

SharedBuffer: memory two programs share

Everything so far was written once and read afterwards. This chapter is about bytes that are never finished in another way: a piece of memory that one program keeps changing while another keeps reading it. There is nothing to send and nothing to decode. The bytes are the data.

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.

numbersroom for textlength in usesingle bitsa counter, used belowpaddingoutlined: just written

What is given up

A SharedBuffer keeps the schema language and little else of what the earlier chapters praised.

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.

protection:

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 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 aswhat a reader getssuited to
        noneplain reads and writesone owner, or your own locking
        seqlockwhole, consistent copies; may retrystate that is read often and written by one
        double_bufferthe newest whole state, never a retryreaders that may not be delayed
        ringrecords in order, each exactly oncea stream of events between two parties
        atomic, seqlock_mw, mpsc_ringthe same ideas with several writersmany 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

        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.

        Try it

        1. In the first panel, tick and untick running. Which byte changes, and what else lives in it?
        2. Type a name of twenty letters. What is stored? Which earlier chapter's format would have stored all twenty, and at what cost here?
        3. In the second panel, with no protection, find an interleaving in which the reader reports 90.
        4. With the seqlock, let the reader take its first step while the counter is odd. What does it do?
        5. 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.