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 GraphNode to 64 bytes and EdgeBucket to 32 bytes 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_free index inside dead records (data_id and state) meant O(1) allocation and deallocation across five different pools with zero extra metadata tables or bitmaps.
  • Inline edges + overflow buckets: Keeping 8 edges inline handles most sparse graph nodes in a single read, while chaining 32-byte buckets meant I didn't have to impose an arbitrary max degree per node.
  • -fvisibility=hidden shared 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:

  1. map_scheme() touches every page on first creation: Even though _create_file cleverly creates a sparse 500 MiB file via lseek, map_scheme() immediately loops over all 320,000 nodes, 1.28M buckets, and every SlotS/M/L to pre-link the free-lists and memset buffers which faults and dirties the entire 500 MiB on first creation anyway!
  2. Orphaned incoming edges on killNode: Because edges are stored purely as outgoing adjacency lists, killing node B doesn't tell node A that its A -> B edge 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.
  3. Fixed 500 MiB ceiling & crash safety: If one slot pool (SlotS) fills up while SlotL is empty, you hit ENSSLT even with hundreds of megabytes free, and msync(MS_ASYNC) without a write-ahead log or shadow header means a hard power loss mid-split could leave a half-linked bucket.
  4. Packed struct alignment: __attribute__((packed)) works fine on forgiving x86-64 CPUs, though GraphNode (4 + 4 + 48 + 8 = 64 bytes) and EdgeBucket (4 + 4 + 24 = 32 bytes) are actually naturally aligned if GraphEdge (4 + 2 = 6 bytes) 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