Skip to main content

FlatCombiner

Struct FlatCombiner 

Source
pub struct FlatCombiner<S> { /* private fields */ }
Expand description

Flat combining dispatcher for batched operation execution.

Wraps shared mutable state with a two-level locking strategy:

  1. A publication queue (queue) where threads post operations
  2. The shared state (state) where operations are executed

The combiner thread locks the state once, drains the queue, and executes all operations in sequence — keeping the hot data in cache and minimizing lock handoffs.

Implementations§

Source§

impl<S> FlatCombiner<S>

Source

pub fn new(state: S) -> Self

Create a new flat combiner wrapping the given shared state.

Source

pub fn execute<R>(&self, op: impl FnOnce(&mut S) -> R) -> R

Execute a single operation directly on the shared state.

Bypasses the publication queue. Use this when you need a return value or when contention is not expected.

Source

pub fn with_state<R>(&self, f: impl FnOnce(&S) -> R) -> R

Read from the shared state without mutation.

Source

pub fn submit(&self, op: impl FnOnce(&mut S) + Send + 'static)

Submit an operation to the publication queue for batched execution.

The operation will be executed during the next combine call. Operations are executed in submission order within each batch.

Source

pub fn submit_batch( &self, ops: impl IntoIterator<Item = Box<dyn FnOnce(&mut S) + Send>>, )

Submit multiple operations at once (avoids repeated lock acquisitions).

Source

pub fn combine(&self) -> usize

Drain all pending operations and execute them as a single batch.

The combiner holds the state lock for the entire batch, keeping the data hot in L1 cache. Returns the number of operations executed.

Returns 0 if no operations are pending.

Source

pub fn combine_with<R>( &self, around: impl FnOnce(&mut S, &dyn Fn(&mut S)) -> R, ) -> (usize, R)

Combine with a pre/post hook for additional work during the batch.

The around function receives a mutable reference to the state and a closure that executes all pending operations. This allows wrapping the batch with setup/teardown logic (e.g., marking a dirty flag, snapshotting state).

Reentrant calls back into execute, with_state, combine, or combine_with from inside around are rejected with a panic instead of deadlocking on the state mutex.

Source

pub fn pending_count(&self) -> usize

Number of operations currently in the publication queue.

Source

pub fn generation(&self) -> u64

Current generation counter. Incremented after each combine pass.

Source

pub fn stats(&self) -> CombinerStats

Get a snapshot of current performance statistics.

Source

pub fn reset_stats(&self)

Reset statistics counters.

Trait Implementations§

Source§

impl<S: Debug> Debug for FlatCombiner<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> !Freeze for FlatCombiner<S>

§

impl<S> RefUnwindSafe for FlatCombiner<S>

§

impl<S> Send for FlatCombiner<S>
where S: Send,

§

impl<S> Sync for FlatCombiner<S>
where S: Send,

§

impl<S> Unpin for FlatCombiner<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for FlatCombiner<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for FlatCombiner<S>

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, 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