DagrA guide to binary formats, and to Dagrdraft

4 Chapter four

Graphs, not trees

Put a record inside a record and you have a tree, and every format can write a tree. But data is rarely that tidy. Two orders belong to one customer. Your friend has you as a friend. This chapter is about what breaks when data stops being a tree, in the bytes and in the program that builds them.

Where trees stop

A record that holds other records is written by writing them inside it, and they hold theirs the same way. JSON does this with braces, Protocol Buffers with a length in front of each inner message. It works as long as everything has exactly one place to be.

Two things in ordinary data do not:

Here are four people. An arrow means "has as a friend". Start with the tree, then add the arrows that make it something else, and see what each way of writing it down makes of it.


  
show:

Pointers in the bytes

The other fix is to let the format itself say "that one, over there". Each node is written once, and where it is referred to, the bytes hold a pointer: the distance from here to there. Readers follow it without knowing or caring that it is a number.

These are the same three graphs, written by Dagr. Its name for this container, one value with everything that hangs from it, is a DataGraph. In the regular layout every node sits behind a pointer. In the packed layout nodes are written inside their parent, which is to say as a tree. Point at a pointer to see where it leads.

So the bytes are a solved problem: write each node once, point at it, leave a slot when you must point at something unfinished. The harder half of the story is not in the bytes at all.

The trouble in memory

Before a graph is written it has to exist in your program, and the obvious way to hold one is the way every language teaches: an object for each person, with a list of references to other people.

let alex = Person(name: "Alex")
let bob = Person(name: "Bob")
alex.friends = [bob]
bob.friends = [alex]

Four lines, and two things have already gone wrong.

Asking a simple question

Is this graph equal to that one? The obvious way to compare two people is to compare their names and then their friends, who are people, so compare their names and then their friends. In the first panel, choose "a cycle" and then "equal? the obvious way" to watch it go round. Printing a person does the same. So does computing a hash.

The fix is for the comparison to keep a list of the pairs it is already comparing. If it meets one of them again, it does not go round a second time. Choose "equal? keeping a list". Every function that walks the graph needs this treatment: equality, hashing, printing, and writing it out. Dagr generates all of them for the types in your schema, so you do not write them, but it is worth knowing that they are there and why.

Letting go

The second problem is quieter. Many languages free an object when the last reference to it goes away: Swift, Rust's Rc, C++'s shared_ptr. Alex holds a reference to Bob, and Bob to Alex. When your program is done with both, each is still referred to by the other, so neither is freed. That is a memory leak, and it needs no bug in your code, only a cycle in your data.

Languages with a garbage collector that traces what is still reachable, such as Java, Go, JavaScript and Python, do not have this problem. Others give you weak references, and you must decide which arrow of every cycle is the weak one. Rust goes further and makes a graph of objects that point at each other awkward to write in the first place.

The arena

Dagr's answer is to stop using references between objects. All the people live in one table, owned by one object, the arena. A person in your hands is not the record. It is a small handle: the arena, and a row number in it. A friend is a row number too.

let arena = SocialGraphArena()
var alex = arena.newPerson(name: "Alex")
var bob = arena.newPerson(name: "Bob")
alex.friends = [bob]
bob.friends = [alex]          // a cycle, and nothing to leak

print(alex.friends[0].friends[0].name)   // "Alex"

The code reads the same as before, because the handle has the properties the object had. Underneath, everything is different:

It is not free, and you should know what it costs:

When you do not need one

The last cost matters, because much data is built once, written, and forgotten, and much data is only read.

What the panels are. The first is a drawing: the text in it is produced by a few lines of JavaScript that walk the arrows, not by any format. The second shows Dagr's bytes, written by its reference writer and explained by dagr explain.

Try it

  1. In the first panel, start from "a tree" and add only the arrow from Bob to Alex. How many people are in the smallest cycle you can make?
  2. With "a shared friend", count the comparisons the obvious way of testing equality makes, and then with a list. Why does the list help even though nothing loops?
  3. In the second panel, with "a shared friend", find the two pointers to Dan. Are they the same number? Should they be?
  4. All four people in the regular form use one table of field positions, the table of chapter 3. Find it, and find the four bytes that refer to it.
  5. A format with ids and a format with pointers both write each person once. Name one thing a reader of the pointer form can do that a reader of the id form cannot do without help from your program.

What comes next

Whether a node sits behind a pointer or inside its parent turned out to decide a great deal: sharing, cycles, size. In Dagr that is one switch on a type, and there is a second switch beside it. Chapter 5 puts the two together.