pub struct Pool { /* private fields */ }Expand description
The best candidates seen across rounds, worst victim last.
Sorted by score ascending, so Pool::take pops the end and the weakest
candidate is always at the front where a better one displaces it. Sixteen
entries is small enough that a sorted array beats anything with a shape, and
the shifting is a rotate over at most fifteen Vec headers.
The array is allocated on the first offer rather than on construction, because a database that never evicts anything is the common one and it does not deserve sixteen anythings.
Implementations§
Source§impl Pool
impl Pool
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Forget every candidate and keep the buffers.
The caller runs this when the answers stop meaning anything, which is a policy change and a flush. Both leave a pool full of scores on a scale nothing uses any more or keys that are not there, and while the recheck on the way out would survive either, a stale pool is sixteen wasted lookups in front of the next eviction.
Sourcepub fn memory_bytes(&self) -> usize
pub fn memory_bytes(&self) -> usize
What the buffers cost.
Sourcepub fn offer(&mut self, key: &[u8], score: u64)
pub fn offer(&mut self, key: &[u8], score: u64)
Put a candidate in the running.
A key already held is re-scored rather than held twice, because the same key turning up in two rounds is ordinary and two entries for it would be one wasted slot and one guaranteed miss on the way out.
A key worse than everything held is dropped when the pool is full, which is the common case once it has warmed up and is the reason this is cheap.
Sourcepub fn take(&mut self) -> Option<&[u8]>
pub fn take(&mut self) -> Option<&[u8]>
The worst key held, removed from the pool.
It is removed whether or not the caller can use it, because a candidate the caller looked at and rejected is a candidate that will be rejected again next time, and the point of a pool is to stop paying for the same answer twice.