pub struct ArenaPool<B: FixedRange, F> { /* private fields */ }Expand description
A pool of recyclable BumpArenas — hand them out with
checkout, return them with
give_back (an O(1) reset plus
retain up to a cap), so in steady state the same backings are reused with no
munmap / mmap / re-fault. The motivating use is a per-commit / per-branch
workload where throwing arenas away per unit of work would dominate the cost.
B is the backing type; F mints a fresh backing on demand (e.g.
|| MmapBacked::new(32 * 1024)). Arenas are wrapped in BumpArena
internally. When idle arenas should not hold physical RAM, an OsBacked
backing additionally enables release_idle.
Implementations§
Source§impl<B, F> ArenaPool<B, F>
impl<B, F> ArenaPool<B, F>
Sourcepub fn new(cap: usize, factory: F) -> Self
pub fn new(cap: usize, factory: F) -> Self
Create an empty pool that retains up to cap idle arenas and mints new
backings with factory.
Sourcepub fn checkout(&mut self) -> Result<BumpArena<B>, AllocError>
pub fn checkout(&mut self) -> Result<BumpArena<B>, AllocError>
Check out an arena: reuse a reset idle one if available, otherwise mint a fresh backing via the factory and wrap it. The returned arena always starts empty (cursor 0).
Sourcepub fn give_back(&mut self, arena: BumpArena<B>)
pub fn give_back(&mut self, arena: BumpArena<B>)
Return an arena to the pool. It is reset (O(1)) and
retained for reuse if the pool is below its cap; otherwise it is dropped
(releasing its backing). In steady state — checkouts and give-backs
balanced under the cap — this performs no allocation syscalls.
Sourcepub fn prewarm(&mut self, n: usize) -> Result<usize, AllocError>
pub fn prewarm(&mut self, n: usize) -> Result<usize, AllocError>
Pre-mint up to n idle arenas (clamped to the remaining cap), so the
first checkouts don’t pay backing construction. Returns the number
actually added; stops early and returns Err if the factory fails.
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Number of reset arenas currently retained and ready for checkout.
Source§impl<B, F> ArenaPool<B, F>
impl<B, F> ArenaPool<B, F>
Sourcepub fn release_idle(&self)
pub fn release_idle(&self)
Release the physical pages backing every idle arena
(madvise(DONTNEED) / MEM_RESET) while keeping their virtual
reservations mapped for reuse. Bounds resident memory for arenas sitting
idle without dropping or re-mmaping them; the next checkout re-faults
the pages it touches.