Skip to main content

ThreadLocalPool

Struct ThreadLocalPool 

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

Thread-local fast packet pool for zero-contention packet building.

This pool uses thread-local storage to cache packet builders, falling back to a shared ArrayQueue when the local cache is empty. This design:

  • Eliminates atomic operations on the hot path (when local cache is warm)
  • Maintains fairness through periodic returns to shared pool
  • Auto-refills from shared pool in batches to amortize atomic costs

§Performance

When the local cache is warm, acquire() and release() have zero atomic operations, making them ~10-15% faster than the shared pool under contention.

§Nonce Safety

All builders in the pool (including those in thread-local caches) share a single TX counter. This prevents nonce reuse when different threads encrypt with the same key.

Implementations§

Source§

impl ThreadLocalPool

Source

pub const DEFAULT_LOCAL_CAPACITY: usize = 8

Default number of builders to cache per thread

Source

pub fn new(size: usize, key: &[u8; 32], session_id: u64) -> Self

Create a new thread-local pool

Source

pub fn with_origin( size: usize, key: &[u8; 32], session_id: u64, origin_hash: u64, ) -> Self

Create a new thread-local pool with origin identity

Source

pub fn with_local_capacity( size: usize, key: &[u8; 32], session_id: u64, origin_hash: u64, local_capacity: usize, ) -> Self

Create a new thread-local pool with custom local capacity

Source

pub fn acquire(&self) -> PacketBuilder

Acquire a builder from the pool.

First tries the thread-local cache (zero atomics), then falls back to the shared pool, refilling the local cache in batches.

Source

pub fn release(&self, builder: PacketBuilder)

Release a builder back to the pool.

Keeps builders in the thread-local cache up to local_capacity * 2, then returns excess to the shared pool.

Source

pub fn capacity(&self) -> usize

Get the pool capacity

Source

pub fn shared_available(&self) -> usize

Get the number of builders in the shared pool

Source

pub fn session_id(&self) -> u64

Get the session ID

Source

pub fn local_capacity(&self) -> usize

Get the local capacity per thread

Source§

impl ThreadLocalPool

Source

pub fn get(&self) -> ThreadLocalPooledBuilder<'_>

Get a builder with RAII guard

Trait Implementations§

Source§

impl Debug for ThreadLocalPool

Source§

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

Formats the value using the given formatter. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more