Struct concurrent_arena::Arena

source ·
pub struct Arena<T, const BITARRAY_LEN: usize, const LEN: usize> { /* private fields */ }
Expand description
  • LEN - Number of elements stored per bucket. Must be less than or equal to u32::MAX, divisible by usize::BITS and it must not be 0.

  • BITARRAY_LEN - Number of usize in the bitmap per bucket. Must be equal to LEN / usize::BITS.

    For best performance, try to set this to number of CPUs that are going to access Arena concurrently.

Arena stores the elements in buckets to ensure that the address for elements are stable while improving efficiency.

Every bucket is of size LEN.

The larger LEN is, the more compact the Arena will be, however it might also waste space if it is unused.

And, allocating a large chunk of memory takes more time.

Arena internally stores the array of buckets as a triomphe::ThinArc and use ArcSwapAny to grow the array atomically, without blocking any reader.

Examples

If you provides Arena with invalid LEM or BITARRAY_LEN, then your code will panic at runtime:

use concurrent_arena::*;
let arena = Arena::<u32, 1, 100>::new();

To make it a compile time failure, you need to call max_buckets:

use concurrent_arena::*;
const MAX_BUCKETS: u32 = Arena::<u32, 1, 100>::max_buckets();

Implementations§

source§

impl<T, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>

source

pub const fn max_buckets() -> u32

Maximum buckets Arena can have.

source§

impl<T: Send + Sync, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>

source

pub fn new() -> Self

Would preallocate 2 buckets.

source

pub fn with_capacity(cap: u32) -> Self

source

pub fn try_insert( &self, value: T ) -> Result<ArenaArc<T, BITARRAY_LEN, LEN>, (T, u32)>

Return Ok(arc) on success, or Err((value, len)) where value is the input param value and len is the length of the Arena at the time of insertion.

This function is lock-free.

source

pub fn try_reserve(&self, new_len: u32) -> bool

Try to reserve min(new_len, Self::max_buckets()) buckets.

This function is technically lock-free.

source

pub fn reserve(&self, new_len: u32)

Reserve min(new_len, Self::max_buckets()) buckets.

source

pub fn insert(&self, value: T) -> ArenaArc<T, BITARRAY_LEN, LEN>

Insert one value.

If there isn’t enough buckets, then try to reserve one bucket and restart the operation.

source§

impl<T: Send + Sync, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>

source

pub fn remove(&self, slot: u32) -> Option<ArenaArc<T, BITARRAY_LEN, LEN>>

May enter busy loop if the slot is not fully initialized.

This function is lock free.

source

pub fn get(&self, slot: u32) -> Option<ArenaArc<T, BITARRAY_LEN, LEN>>

May enter busy loop if the slot is not fully initialized.

This function is lock free.

source

pub fn len(&self) -> u32

Return number of buckets allocated.

This function is lock free.

source

pub fn is_empty(&self) -> bool

This function is lock free.

Trait Implementations§

source§

impl<T: Debug, const BITARRAY_LEN: usize, const LEN: usize> Debug for Arena<T, BITARRAY_LEN, LEN>

source§

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

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

impl<T: Sync + Send, const BITARRAY_LEN: usize, const LEN: usize> Default for Arena<T, BITARRAY_LEN, LEN>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, const BITARRAY_LEN: usize, const LEN: usize> !RefUnwindSafe for Arena<T, BITARRAY_LEN, LEN>

§

impl<T, const BITARRAY_LEN: usize, const LEN: usize> Send for Arena<T, BITARRAY_LEN, LEN>where T: Send + Sync,

§

impl<T, const BITARRAY_LEN: usize, const LEN: usize> Sync for Arena<T, BITARRAY_LEN, LEN>where T: Send + Sync,

§

impl<T, const BITARRAY_LEN: usize, const LEN: usize> Unpin for Arena<T, BITARRAY_LEN, LEN>where T: Unpin,

§

impl<T, const BITARRAY_LEN: usize, const LEN: usize> !UnwindSafe for Arena<T, BITARRAY_LEN, LEN>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.