Read Path
The read path is the code that runs every time your application calls get,
get_as or lookup on a detent::Reader. Its contract is strict: once warm,
no lock, syscall, sleep, logging I/O or heap allocation, and no thread of its
own. The core is libdetent_reader (src/detent_reader/core.cc), behind the C
ABI in include/detent/reader.h that C++, Python and Rust all call.
Step by Step
Section titled “Step by Step”- Validate the name.
ns:keysplits at the first colon; an invalid name, or a namespace outside the handle’s list, returns the fallback before any handle state is touched. - Pin the view. The handle holds an immutable view of the attached generations. A lookup adds one to its stripe of the view’s counters, so threads spread over 64 cache lines instead of sharing one reference count.
- Check the sequence. One acquire load of the entry’s sequence in the
mapped
changefile. Unchanged, the view is current. The store has one entry, the selected generation, serving every namespace. - Walk and evaluate. The lookup walks the memory-mapped generation and
evaluates with the precedence rules. A
page is verified once, the first time any lookup touches it; later lookups
test one bit. Only a percentage with a hash value pays for SHA-256.
get_as<T>then range-checks theint64intoT. - Unpin and return the value, or the caller’s fallback with the status.
Switching Generations
Section titled “Switching Generations”When the agent publishes, the sequence moves. The first lookup that sees it
tries one compare-and-swap on the handle’s refreshing flag; the winner
attaches the new generation, publishes a new view and answers from it. Every
other thread keeps answering from the installed view without waiting. A failed
attach keeps the installed view. See Update Path.
The first lookup in a namespace attaches it. Concurrent lookups in that namespace wait for the winner, so no thread answers a published knob with its fallback because it lost the race.
Pinning a Consistent Read
Section titled “Pinning a Consistent Read”A lookup reads one namespace from one generation. The reader promises nothing
across namespaces, and two lookups may straddle a switch. revision(namespace)
reports the revision a namespace’s attached generation carries.
Measured Cost
Section titled “Measured Cost”| Benchmark | Result |
|---|---|
detent_hash_bench 5000000 | 198.2 ns per stable_bucket() call |
The lookup benchmark is reader_bench (benchmarks/reader_bench.cc); its
header comment gives the usage.