pool only.Expand description
Prototype buffer pool for dataflow state. See
doc/developer/design/20260610_buffer_managed_state.md.
The pool is the cache: size-class anonymous virtual-memory regions whose
slots hold resident chunks. Slots are scoped to residency — eviction
returns a chunk’s slot to the free list along with its physical pages —
so slot demand tracks the resident set (bounded by the budget), not the
potentially unbounded live backlog. Reads are copy-out
(ChunkHandle::read_into): a resident slot is copied and an evicted
extent decompressed straight into the caller’s buffer, all under the
chunk’s state lock, so no reference into pool memory escapes the pool and
a read leaves residency untouched. The backing is the extent store of the
design’s Layer 1: a slot in a pool-owned anonymous-memory extent arena
holding the chunk’s lz4-compressed bytes, later paged to swap or demoted
to file per the pool’s ExtentBackend.
Memory descends a ladder of tiers, each with its own ceiling and each cheaper to vacate than the one above:
- Slots (uncompressed, free reads) — bounded by the budget; crossing it compresses the oldest chunks into extents and releases their slots.
- Warm free slots (pages kept for fault-free reuse) — bounded by the warm cap.
- Compressed-resident extents (reads decompress, no device) — bounded
by the headroom the RSS target leaves above the first two; crossing it
pushes the oldest extents to the swap device with
MADV_PAGEOUT. - The swap device (overflow). Reads fault and decompress. A pool
built over
ExtentBackend::Filedemotes extents to files in a scratch directory instead, and reads them back withpread.
Residency is a state, not a type. It descends through eviction and
ascends through exactly one transition: an admitting read
(ChunkHandle::read_into_admit) lifts an evicted chunk back to
BackedResident when a slot is free within the budget or stealable from
a clean backed victim of the same class, never by evicting or
compressing anything. Plain reads (ChunkHandle::read_into) leave
residency untouched. Eviction I/O runs on spill threads when enabled —
WriteInFlight marks a chunk whose compression a spill thread owns — and
inline on the evicting caller otherwise. Chunks are immutable after
Pool::insert_with, which is what makes a BackedResident slot always
identical to its extent and its eviction free of I/O.
Freeing an UnbackedResident chunk is a pure memory operation — the
design’s “never write dead data” win, surfaced as writes_elided in
PoolStats. Budget pressure evicts cold chunks via second-chance
FIFOs banded by the caller-supplied generational depth (ChunkHints).
Modules§
- extent 🔒
- The extent type and the arena that holds extents in memory, the first home of every extent whatever the pool’s backend.
- file 🔒
- The file-backed extent store: a userspace extent allocator over one anonymous file per extent class in a scratch directory.
- region 🔒
- Size-class virtual-memory regions for the buffer pool.
Structs§
- Chunk
Handle - Handle to one immutable chunk in a
Pool. Dropping the handle frees the chunk: the slot (if resident) returns to the region free list with its physical pages released, and the extent (if any) is deallocated, discarding any swapped copy for free. - Chunk
Hints - Advisory placement hints for a chunk, supplied at insert and immutable thereafter (merges mint new chunks, so a chunk’s generation never changes). Hints steer policy — eviction order and write-behind candidacy — never correctness: a mislabeled chunk performs worse, while the budget and residency invariants hold regardless.
- Chunk
Meta 🔒 - Chunk
State 🔒 - Counters 🔒
- Extent
Entry 🔒 - An
extent_queueentry. - File
Release 🔒 - The schedule of the file store’s warm-slot release.
- Identity
Codec - The identity
ExtentCodec: the stored form is the body. Encode and decode are copies, and range reads copy the range directly, so a chunk stored under this codec pays no compression work in either direction while remaining fully budgeted and backed like any other extent. - Parked
Extents 🔒 extent_queueentries set aside per extent class. SeePoolInner::parked_extents.- Pool
- A buffer pool over compressed extents, backed by swap or by files per its
ExtentBackend. Cheap to clone. All clones share one budget and one backing store. - Pool
Inner 🔒 - The shared state behind every
Poolhandle. One per process in practice;Poolclones and chunk handles share it through anArc, so it lives until the last handle and spill thread release it. - Pool
Stats - Snapshot of pool counters.
- Spill 🔒
- Hand-off point between budget enforcement and spill threads. Eviction I/O
(compression and the synchronous-reclaim
pageout) runs on spill threads when enabled, keeping multi-millisecond work off the threads that trip the budget; with no spill threads, eviction runs inline on the caller.
Enums§
- Backend
Kind - The store a pool actually runs on.
- Demotion 🔒
- The outcome of one
PoolInner::demote, as it bears on the pass. - Extent
Backend - The extent store a pool is built over, fixed for the pool’s lifetime.
- Extent
Store 🔒 - The extent store behind a
PoolInner. - Pass 🔒
- Which kind of caller runs a
PoolInner::enforce_compressed_cappass. - Residency 🔒
- Residency state of a chunk.
- Spill
Kind 🔒 - What a spill thread does with a chunk once compressed.
Constants§
- CLASS_
CAPACITY_ 🔒BYTES - Virtual reservation per size class. Purely virtual: physical memory materializes only for slots in use, and slots are scoped to residency, so this must exceed the largest plausible resident set per class, the budget plus in-flight slack, not the backlog. It is deliberately enormous (address space costs nothing, and touched pages are bounded by peak residency) so that no realistic budget, on any machine size, reaches the heap-fallback path.
- DEMOTION_
PASS_ 🔒BYTES - The most extent bytes one file-mode pass demotes: a spill thread’s pass while the eviction queue is empty, and an inline pass while spill threads exist. With queued evictions a spill thread’s pass demotes one extent, so the thread alternates one eviction with one demotion.
- DEPTH_
BANDS 🔒 - Number of depth bands the eviction queues are split into; depths at or beyond the last band share it.
- FILE_
CAPACITY_ 🔒REFRESH_ INTERVAL - How often a spill thread re-derives the file store’s capacity from the volume’s free space.
- READ_
LATENCY_ BUCKETS - Number of read-latency buckets. Bucket
i < READ_LATENCY_BUCKETS - 1counts reads that took less than32 µs << iand, fori > 0, at least16 µs << i. The last bucket counts reads of 65.536 ms and more. - SPILL_
IN_ FLIGHT_ MAX - Beyond this many queued or in-flight spill entries, eviction degrades to inline on the caller: bounded memory overshoot under burst beats an unbounded queue of still-resident chunks.
- SPILL_
PARK_ 🔒TIMEOUT - How long an idle spill thread parks, and how often a spill thread retries demotion while the file store’s full hint is set.
- WORD_
BYTES 🔒 - Bytes per word of a chunk body. Chunk lengths are in
u64words.
Statics§
- IDENTITY_
CODEC - The
IdentityCodecinstance to pass toPool::insert_with.
Traits§
- Extent
Codec - A chunk-provided transform between a chunk’s body bytes and the stored
bytes its extent holds. The pool owns scheduling: spill threads, the
residency state machine, cancellation, and the ledger. It invokes the
codec on opaque bytes at the extent boundary,
encodewhen backing a chunk (on a spill thread, or inline under overload) anddecodewhen reading an evicted one, possibly under the chunk’s state lock. The pool itself has no opinion on the stored form: framing, compression, and validation all belong to the codec. - Typed
Body - A chunk body the pool holds in its owner’s in-memory form, serializing it
only when it has to leave that form: on eviction, on backing, and on a
copy-out read. Under budget a typed chunk therefore costs the pool its
bookkeeping and nothing else, and
ChunkHandle::take_typedhands the body back without a copy.
Functions§
- backstop_
threshold 🔒 - The tier size at which callers with spill threads present enforce the compressed cap inline, and where an inline file-mode pass stops.
- band 🔒
- The eviction-queue band for a chunk of
depth. - class_
bit 🔒 - The bit of
classin a pass’s refused-class mask, or 0 for a class the mask cannot represent, which the pass then probes on every visit. - insert_
slack 🔒 - Resident bytes insertions may reserve above
budgetwhile enforcement catches up. Slots can transiently hold this much past the budget, so the RSS target reserves it too. - max_
stored_ len - The largest stored form
ExtentCodec::encodemay produce for abody_len-byte body: an incompressible-input expansion matching lz4’s worst case plus a four-byte length prefix. The extent store’s size-class ladder is provisioned to this bound, so a codec that exceeds it can strand payloads with no class to hold them (they degrade to unpageable heap fallbacks). - serialize_
typed 🔒 - The serialized form of a typed body, in a fresh buffer.