Skip to main content

Pool

Struct Pool 

Source
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

Source

pub const fn new() -> Pool

An empty pool that has not allocated anything.

Source

pub const fn len(&self) -> usize

How many candidates are held.

Source

pub const fn is_empty(&self) -> bool

Whether there is nothing to take.

Source

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.

Source

pub fn memory_bytes(&self) -> usize

What the buffers cost.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Pool

Source§

fn clone(&self) -> Pool

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Pool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Pool

Source§

fn default() -> Pool

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Pool

§

impl RefUnwindSafe for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

§

impl UnwindSafe for Pool

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.