English
ours-bufpool
libs/bufpool — packet and stream buffers with headroom, taken from a pool and returned to it.
Why
Protocols wrap data in headers. A buffer with free space before the data (headroom) lets every layer write its header in place (prepend) without copying the payload into a bigger buffer. The pool removes the allocator from the data path. A buffer returns to the pool on Drop, so a connection holds a buffer only while it moves data: an idle one holds nothing.
The API in outline
Buf— a buffer withheadroom/tailroom:extend_from_slice,prepend,spare_mut+commit(writing "into the tail" without a copy),advance,truncate,reset. The out-of-room error isNoRoom.BufPool::new(capacity, headroom, max_cached)— a pool of buffers of one size; every thread keeps no more thanmax_cachedfree ones and frees the rest;get,cachedandin_use(for the current thread, for diagnostics),trim.- Packets:
PACKET_HEADROOM(128 bytes),packet_buf(len)— from the pool of small (2048) or large (65535) datagrams. - Streams:
stream_pool()(16 KiB — one TLS record),large_stream_pool()(64 KiB — for protocols whose frames are larger than a TLS record) andhuge_stream_pool()(256 KiB, cached up to 8 per thread — copying through in-process TLS: sixteen records per system call). RecordRoom— how a stream that seals records in place (its own TLS) wants to see them in the buffer handed to it: the room before the plaintext, its limit, the room after, records per buffer.take_vec/recycle_vec— a per-thread cache ofVec<u8>(up to 8 of them and up to 512 KiB in total: a batch of TLS records and a few single ones) for framing buffers and TLS records: a connection takes them for the duration of a transfer and gives them back while waiting.take_initialized— the same, but a vector from the cache keeps the length it was returned with (TLS does not zero the batch again).trim_thread()— frees what the thread's caches (take_vecand all pools) held and nobody took since the previous call (the minimum of free ones over the interval is surplus). The kernel calls it once a second on every shard: after load the memory is returned, under steady load what is needed stays in the cache.trim_all()— from any thread tells all threads to drop their caches entirely, for a caller outside the shards that decides memory is short: threads see it through an atomic counter on the nextget, buffer return ortrim_thread. Large buffers (≥128 KiB) musl gives back withmunmapright away.
Who uses it
Almost the whole data path: ours-api, ours-relay, ours-tls13, ours-h2, ours-net-sys, ours-kernel, ours-proto, ours-uot, ours-quic, the binary, most protocol and transport modules, and dial, dispatch, ntp, dns, router, group.
Specifics
- All caches are
thread_local!with no lock on the hot path: shards run one per thread, and a buffer usually returns to the shard that took it. A buffer remembers the thread that took it (anArcof its incoming queueInbox); released on another thread, it is put into that queue (aMutex, no more than 2 MiB, the surplus is freed), and the taking thread moves the queue into its cache when its cache for this pool is empty, and ontrim_thread. This way an HTTP/2 session driven by one shard, with the relay of its streams on another, does not allocate and zero new pages for every read buffer (128 KiB —mmap/munmapin musl).in_usecounts such a buffer as returned when the taking thread has picked it up. A shared pool under a mutex ate up to half of the CPU on locking with 4+ shards. The contents of cached vectors are not zeroed;recycle_vecreturns the vector back if it did not take it, so that the caller can wipe secrets itself. - No dependencies.
Tests
Unit tests in src/lib.rs.