04. Lessons Learned...
Recap
Over the last four posts, I went from the API manual and motivation to carving up a 500 MiB mmap file, designing 64-byte nodes and free-lists, and implementing swap-with-last edge compaction inside libc-mmap-graph.so.
Now that the momentary motivation is gone, it’s worth stepping back and looking honestly at what worked great and what is janky.
I do plan on coming back to this project someday, because there is plenty of room to make it genuinely solid. For now though, it can stay right where it is as v1, at least until I actually run into a personal project that needs a cleaner, hardened v2. (No point over-engineering a version 2 nobody, including me, needs this week.)
What I'm Good On
- Thinking in pages and cache lines: Packing
GraphNodeto64bytes andEdgeBucketto32bytes made all the offset math clean and predictable. I stopped thinking of "objects" and start seeing exact byte ranges on a memory map. Screw Object Oriented Design. - Free-lists: Hiding the
next_freeindex inside dead records (data_idandstate) meantO(1)allocation and deallocation across five different pools with zero extra metadata tables or bitmaps. - Inline edges + overflow buckets: Keeping
8edges inline handles most sparse graph nodes in a single read, while chaining32-byte buckets meant I didn't have to impose an arbitrary max degree per node. -fvisibility=hiddenshared library: Separating messy pointer-arithmetic internals (graph_core.c,graph_ops.c) from a clean public C boundary (api/api.h) made the library actually pleasant to link against. Gotta love encapsulation.
What I Got Wrong (Or Cut Corners On)
If I ever write c-mmap-graph v2, here are issues with the v1:
map_scheme()touches every page on first creation: Even though_create_filecleverly creates a sparse500 MiBfile vialseek,map_scheme()immediately loops over all320,000nodes,1.28Mbuckets, and everySlotS/M/Lto pre-link the free-lists andmemsetbuffers which faults and dirties the entire500 MiBon first creation anyway!- Orphaned incoming edges on
killNode: Because edges are stored purely as outgoing adjacency lists, killing nodeBdoesn't tell nodeAthat itsA -> Bedge now points to a freed (or worse, re-allocated!)node_id. Fixing that cleanly without full-graph scans calls for either per-node generation counters ({ node_id, gen }handles) or an incoming-edge index. - Fixed
500 MiBceiling & crash safety: If one slot pool (SlotS) fills up whileSlotLis empty, you hitENSSLTeven with hundreds of megabytes free, andmsync(MS_ASYNC)without a write-ahead log or shadow header means a hard power loss mid-split could leave a half-linked bucket. - Packed struct alignment:
__attribute__((packed))works fine on forgiving x86-64 CPUs, thoughGraphNode(4 + 4 + 48 + 8 = 64bytes) andEdgeBucket(4 + 4 + 24 = 32bytes) are actually naturally aligned ifGraphEdge(4 + 2 = 6bytes) is handled carefully, avoiding unaligned loads on stricter architectures.
Wrapping Up
For a 3–4 day sprint written purely by hand to see how far raw mmap and C structs could take me, c-mmap-graph taught me more about storage internals, binary layouts, and ABI boundaries than reading five textbooks ever would.
Check out the repo at github.com/Iliyas-Amine/c-mmap-graph if you want to poke around the source. Onto the next rabbit hole.
Written on: 2026-08-23