Skip to content

Puzzle Pieces

Small C++ libraries, and the systems they snap into.

Part 42 of 42ofFamiliars
  1. 1Session Zero
  2. 2Rolling for a Name
  3. 3The Haunted Handler
  4. 4The Sparse Wheel
  5. 5The Fork in the Path
  6. 6The Planet as a Prefix
  7. 7The Long Division
  8. 8The Line You Usually Cancel
  9. 9Sixteen Bytes Too Far
  10. 10Packing a UUID
  11. 11Bytes You Can Type
  12. 12The Shunting Yard
  13. 13Interchangeable Parts
  14. 14One Loop Per Core
  15. 15The Seam
  16. 16Append Only
  17. 17Who Owns the Tree
  18. 18Down the Flume
  19. 19The Syscalls That Lie
  20. 20Asking the Machine
  21. 21The Earth Is Not a Ball
  22. 22Smith and Schmidt
  23. 23Many Ways to Be Similar
  24. 24Built to Disappear
  25. 25Sometimes a Mutex Is Right
  26. 26Work, Not Threads
  27. 27Before the Program Runs
  28. 28No Collisions, Ever
  29. 29The Names Enums Throw Away
  30. 30The Bag of Holding
  31. 31A Map of Blocks
  32. 32Certainly Absent
  33. 33A Tree Made of Types
  34. 34Everything but the Views
  35. 35Who Owns the Wheel
  36. 36A Guest on the Loop
  37. 37Acknowledged Means Durable
  38. 38Sorted as Bytes
  39. 39Store, Then Search
  40. 40When Power Leaves
  41. 41Keys Stay Home
  42. 42Puzzle Pieces

Session Zero opened the Familiars as a party of small, sharp C++ libraries, the first of them carved out of Xapiand, and every post since has opened one of them on its own terms. This one steps back to look at the whole table.

Extraction on its own proves very little: a library that compiles in its own repository has only shown that it can be moved. The test I cared about was whether different systems could be built from the same box, and whether the box got better for it. Three have been, and the pieces now travel in both directions.

Inside Xapiand nothing was tangled on purpose, yet everything could reach everything else. Most attempts to make a large C++ codebase reusable end in the same place: a common library. Someone moves the string type, the allocator hooks, a base class and a logging singleton into it, and from then on every project that wants one hash function swallows the whole universe to get it.

So the Familiars follow a short set of rules, and the catalog’s AGENTS.md spells them out for people and coding agents alike. Each library is one idea in its own repository, with a README, a test suite, usually a small demo, and one CMake target named after it. There is no umbrella header, no common runtime and no registration step. Most are header-only, and more than half pull in nothing first-party at all. A library brings its own dependencies through FetchContent, and builds its tests only when it is the top-level project, so you never compile a test suite you are not running.

Using one takes a few lines of CMake:

# One library; its own dependencies come along.
include(FetchContent)
FetchContent_Declare(flat-map
GIT_REPOSITORY https://github.com/Kronuz/flat-map.git
GIT_TAG main)
FetchContent_MakeAvailable(flat-map)
target_link_libraries(my_service PRIVATE flat_map::flat_map)

Every library runs the same shared CI: Linux on x86_64 with GCC and Clang and on arm64, macOS on arm64 and x86_64, FreeBSD, and separate AddressSanitizer, UndefinedBehaviorSanitizer and ThreadSanitizer builds. A piece that only builds on my laptop is not a piece anyone else can use.

Here is what the same box has built so far. Each system on top links only the pieces it needs, and the shared ones in the middle are where the reuse actually happens.

Three systems built from the same libraries: prism, Detent and Xapiand all use http and logger; Detent and Xapiand both use consensus, which runs on journal and completion; Xapiand also uses prospect, which runs on strata; Detent and strata both use task.

prism is the smallest: an HTTP application server with content negotiation, compression, colored logs and tracebacks. It is mostly glue. http brings the server, built on reactor (shared-nothing Asio reactors, one per core), http-parser and radix-router, and logger and traceback bring the diagnostics. The catalog calls it the worked example: read it first if you are building a service.

Detent has nothing to do with search. It is runtime configuration (feature flags, kill switches and live settings, read without allocating), and it was the real test, because the pieces it started from had been written for other systems. Its authority serves HTTP with the same http stack, and every handler is a task coroutine. Its durable state replicates through Raft in consensus, on top of journal and completion. Rollout buckets come from sha256 and endian, names are validated with char-classify, and the daemons log through logger. None of that is Detent’s own code, so Detent’s own code is about configuration. Every dependency is pinned by commit in one cmake/dependencies.json, so a change to a shared piece reaches Detent only when Detent asks for it.

Xapiand closes the loop: the engine that gave the catalog its first pieces is now built from it. Its search runs on prospect, which adds terms, postings, BM25 scoring and coroutine queries on top of strata, an LSM-style segment store: immutable segments, background merges under a memory budget, and commits verified against power loss, a full disk and media faults. strata never names a search type, and under it sit termdict for the sorted term table, fusefilter to answer “is this key here?” in three probes, completion for I/O and task for coroutines. Its replication groups run on the same consensus Raft as Detent, and it serves HTTP through the same http and logger as prism. Together, prospect and strata are built to replace Xapian inside it. Store, Then Search tells that half of the story in full.

The part I did not expect is that Detent gave pieces back.

Its model keeps four tables that are built in bulk and merged in order, which is exactly what a sorted vector does well and what std::map does badly. So on October 23 I wrote a flat_map inside Detent, with a bulk builder and linear merges, and moved the four tables onto it. The next day it became flat-map, a library of its own, and Detent includes it from there. Its authority had a similar story: an immutable, versioned map whose versions share structure, living inside Detent since October 4. It became persistent-map on October 29, and Detent switched to the library on November 25.

Xapiand gave the catalog htm and logger and now uses prospect and consensus from it; Detent gave it flat-map and persistent-map and uses http and consensus from it.

That direction matters more than it looks. Once a piece lives in the catalog, a fix found while building one system lands in the library, and every other system picks it up on its next pin. The catalog’s first rule is never duplicate; extend: if a library almost does the job, it gets extended at the source, never copied and patched. The second half of that bargain is that these libraries are used by applications that are not in front of me, so additions are welcome, and a breaking change to an API or an on-disk format has to be called out explicitly.

None of this needed a grand design up front. Many of the Familiars spent years inside Xapiand before their edges stopped moving, and flat-map needed a single day; in both cases the library came out after the boundary was obvious, not before.

What does need discipline is keeping leaves as leaves. A hash function never gets to pick up logging because it would be convenient, and a container never learns about I/O. That is the whole trick: if adding something would make a library harder to drop into an unrelated project, it belongs in the caller, or in a new library. Keep that one rule, and the pieces stay small enough to fit puzzles you have not thought of yet.

If you want to explore, start at Meet the Familiars to find the piece you need, the catalog for how they layer, and Session Zero for the series that opens them one at a time.