02. Design Plan

Recap

Last post covered why I decided to build a persistent graph engine directly on top of mmap with zero external libraries and zero internal malloc calls (because I hate myself). Before touching gcc, though, I had to design the exact binary layout on paper, because once I start casting raw file bytes to C structs, changing a struct's size breaks everything downstream and I'd have to go back recalculating.

I picked 500 MiB pretty arbitrarily as a clean ceiling for a bounded-space file. The 5/128 split (PARTITION_CONST = 5.0 / 128.0), on the other hand, came from playing around with the numbers until the ratio of nodes, overflow buckets, and string storage actually felt balanced. It's not some magic academic formula, just sitting down and thinking through how much space each region realistically needs so none of them starve too early.

Slicing Up the 500 MiB

To keep pointer math dead simple on v1, I went with a single fixed-size 500 MiB (524,288,000 bytes) file divided into standard 4096-byte OS pages, giving exactly 128,000 pages total.
From there, the file is partitioned into four contiguous zones using PARTITION_CONST = 5.0 / 128.0:

+--------------------+-------------------------+---------------------------+----------------------------------+
| Page 0 (4 KiB)     | Node Region             | Edge Bucket Region        | Tiered Data Heap (SlotS / M / L) |
| GraphHeader "CMGP" | 5,000 pages (~320k)     | 10,000 pages (~1.28M)     | 112,999 pages (divided in 3)     |
+--------------------+-------------------------+---------------------------+----------------------------------+
  • Page 0 (GraphHeader - 4096 bytes): Starts with the 4-byte magic signature 'C', 'M', 'G', 'P', followed by the start page of each region, max capacities, live item counters, the five free-list head indices, an array of 256 root node IDs, and 2996 bytes of zeroed padding so the header occupies one exact OS page.
  • Node Region (5,000 pages): Starting at Page 1. Holds 320,000 fixed-size GraphNode records.
  • Overflow Bucket Region (10,000 pages): Holds 1,280,000 EdgeBucket records for nodes that outgrow their inline edge array.
  • Payload Slab Region (112,999 pages): Everything left over, split evenly into three fixed-size string pools (SlotS, SlotM, SlotL).

Struct Packing

Usually C just pads your structs for you, and it's fine, except when like me and you want precise sizes, struct padding silently bloats records and wastes pages. Every core on-disk struct in include/graph_core.h is explicitly sized to powers of two with __attribute__((packed)):

typedef struct {
  uint32_t target_id; // 4 bytes; Either node id or edge bucket id
  uint16_t rel_type;  // 2 bytes; 65534 = bucket overflow, 65535 = inactive
} __attribute__((packed)) GraphEdge; // 6 bytes

typedef struct {
  uint32_t node_id;     // 4 bytes
  uint32_t data_id;     // 4 bytes
  GraphEdge edges[8];   // 48 bytes
  uint16_t flags[4];    // 8 bytes
} __attribute__((packed)) GraphNode; // Exactly 64 bytes!

typedef struct {
  uint32_t bucket_id;   // 4 bytes
  uint32_t state;       // 4 bytes
  GraphEdge edges[4];   // 24 bytes
} __attribute__((packed)) EdgeBucket; // Exactly 32 bytes!

Notice the numbers:

  • A GraphNode is 64 bytes—which matches a standard x86-64 CPU cache line (64 nodes per 4096-byte page, zero straddling across page boundaries!).
  • An EdgeBucket is 32 bytes—two buckets per cache line, 128 buckets per page.

Handling Hyper-Connected Nodes Without Dynamic Arrays

Storing 8 inline GraphEdges right inside GraphNode means any node with less than 8 outgoing edges requires zero pointer chasing to read its neighbors since they are already sitting in the same 64-byte cache line.
What happens when you add a 9th edge?

  1. We allocate one EdgeBucket (4 edge slots).
  2. We move whatever was sitting in node.edges[7] into bucket.edges[0], and put the new 9th edge into bucket.edges[1].
  3. We overwrite node.edges[7] with { .target_id = new_bucket_id, .rel_type = 65534 } (UINT16_MAX - 1).

If that bucket fills up too, bucket.edges[3] pulls the exact same trick and chains to another EdgeBucket. Simple, compact, and no realloc anywhere. A linked list basically, gets inefficient for nodes with tons of connections. BUT, I do not care enough for that issue right now.

String Slots

Allowing arbitrary byte-by-byte variable strings inside an mmap file got super complicated when I tried to think of it.
So instead, I used tiering as an idea: three fixed-size record types that cleanly divide 4096-byte pages:

  • SlotS (256 bytes total -> 248 bytes of string data): 16 slots per page.
  • SlotM (512 bytes total -> 504 bytes of string data): 8 slots per page.
  • SlotL (1024 bytes total -> 1016 bytes of string data): 4 slots per page.

To avoid needing an extra slot_type field on GraphNode, all three pools share one continuous global slot_id space:

  • 0 .. max_slot_s - 1 -> SlotS
  • max_slot_s .. (max_slot_s + max_slot_m - 1) $\rightarrow$ SlotM
  • (max_slot_s + max_slot_m) .. total - 1 $\rightarrow$ SlotL

Just by comparing node.data_id against the bounds in GraphHeader, the engine instantly knows which pool to index into and which deallocator to call. Kind of annoying while coding since I need to check at each point, but then again. It IS more space efficient so I guess it is fine.

Free-Lists

The last piece of the design was allocation speed. Scanning 320,000 nodes or 1.28M buckets linearly to find a free slot would destroy write performance, and maintaining separate bitmap pages adds bookkeeping I'm not willing to do.
Decided that every free record stores the index of the next free record inside itself a "free-list" linked list situation:

  • When a GraphNode is unallocated, its data_id field holds the node_id of the next free node (UINT32_MAX marks the end of the chain).
  • When an EdgeBucket or SlotS/M/L is unallocated, its state field holds the next free index (UINT32_MAX - 1 means occupied).

Allocating is two integer assignments (pop head from GraphHeader), and freeing is two integer assignments (push freed ID onto head). Pure O(1). Me like that.

Wrapping Up

On paper, everything fit together cleanly: 4096-byte header, 64-byte nodes, 32-byte chained buckets, 256/512/1024-byte string stores, and optimal free-lists everywhere. Next post goes into actually turning this into working C code, and the pointer/edge-compaction headaches along the way since this comes at a price after all.


Written on: 2026-08-22