Skip to main content

MergeMemoryPool

Struct MergeMemoryPool 

Source
pub struct MergeMemoryPool { /* private fields */ }
Expand description

Shares reserved merge workspace across child reservations and temporary loans.

This pool charges a single reservation to its parent MemoryPool. After acquiring workspace through a child MemoryReservation, call Self::retain to keep that capacity available even when children release their reservations. Children and WorkspaceLoans share this capacity; only usage above the existing parent reservation requires additional parent memory. Call Self::release_unused when no further merge needs the idle workspace. Live children and loans keep the pool and their reserved memory alive.

All parent accounting and allocation policy uses the MemoryConsumer supplied to Self::new. Child consumers are not registered with the parent, so their names and MemoryConsumer::can_spill flags do not affect the parent’s accounting or fair shares. Consumers that need a separate parent allocation policy should register directly with the parent. They can borrow already-acquired workspace with Self::borrow and request any additional capacity through their own reservation.

§Example

let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
let workspace = Arc::new(MergeMemoryPool::new(
    Arc::clone(&parent),
    MemoryConsumer::new("merge workspace"),
));
let pool: Arc<dyn MemoryPool> = Arc::clone(&workspace) as _;
let reservation = MemoryConsumer::new("merge children").register(&pool);
reservation.try_grow(60)?;
workspace.retain(60);
reservation.free();

// Another operator can use only the space outside the retained workspace.
let contender = MemoryConsumer::new("contender").register(&parent);
contender.try_grow(40)?;
assert_eq!(parent.reserved(), 100);

// Children and loans reuse that workspace without another parent grant.
let cursor = reservation.new_empty();
cursor.try_grow(20)?;
let loan = workspace.borrow(60);
assert_eq!(loan.size(), 40);
workspace.release_unused();
assert_eq!(parent.reserved(), 100); // Both the cursor and loan remain charged.
drop(loan);
assert_eq!(parent.reserved(), 60); // Only the cursor and contender remain.

Implementations§

Source§

impl MergeMemoryPool

Source

pub fn new(parent: Arc<dyn MemoryPool>, consumer: MemoryConsumer) -> Self

Creates a pool whose memory is charged to consumer in parent.

Registers the consumer without reserving any memory. The consumer’s allocation policy applies to the combined usage of all children and loans.

Source

pub fn retain(&self, size: usize)

Sets the amount of acquired workspace to retain when children release it.

The workspace must first be acquired through a child reservation. This method sets the retained floor without changing the parent reservation. Use Self::release_unused to stop retaining idle workspace immediately.

§Panics

Panics if size exceeds this pool’s current reservation in the parent, as reported by its MemoryPool::reserved method.

Source

pub fn borrow(self: &Arc<Self>, size: usize) -> WorkspaceLoan

Lends up to size unused bytes without acquiring more parent memory.

Returns a smaller loan if less workspace is available, including an empty loan if all capacity is in use. The loan is counted alongside child reservations so they cannot spend the same credit. Dropping it returns any outstanding bytes.

Source

pub fn release_unused(&self)

Stops retaining idle workspace and returns unused capacity to the parent.

Live child reservations and loans remain charged. Their future releases also return memory to the parent, unless Self::retain is called again.

Trait Implementations§

Source§

impl Debug for MergeMemoryPool

Source§

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

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

impl Display for MergeMemoryPool

Source§

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

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

impl MemoryPool for MergeMemoryPool

Source§

fn name(&self) -> &str

Return pool name
Source§

fn grow(&self, _reservation: &MemoryReservation, additional: usize)

Infallibly grow the provided reservation by additional bytes Read more
Source§

fn try_grow( &self, _reservation: &MemoryReservation, additional: usize, ) -> Result<()>

Attempt to grow the provided reservation by additional bytes Read more
Source§

fn shrink(&self, _reservation: &MemoryReservation, subtractive: usize)

Infallibly shrink the provided reservation by shrink bytes
Source§

fn reserved(&self) -> usize

Return the total amount of memory reserved
Source§

fn memory_limit(&self) -> MemoryLimit

Return the memory limit of the pool Read more
Source§

fn register(&self, _consumer: &MemoryConsumer)

Registers a new MemoryConsumer Read more
Source§

fn unregister(&self, _consumer: &MemoryConsumer)

Records the destruction of a MemoryReservation with MemoryConsumer Read more

Auto Trait Implementations§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V