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 tou32::MAX
, divisible byusize::BITS
and it must not be0
. -
BITARRAY_LEN
- Number ofusize
in the bitmap per bucket. Must be equal toLEN / 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>
impl<T, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>
sourcepub const fn max_buckets() -> u32
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>
impl<T: Send + Sync, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>
pub fn with_capacity(cap: u32) -> Self
sourcepub fn try_insert(
&self,
value: T
) -> Result<ArenaArc<T, BITARRAY_LEN, LEN>, (T, u32)>
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.
sourcepub fn try_reserve(&self, new_len: u32) -> bool
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§impl<T: Send + Sync, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>
impl<T: Send + Sync, const BITARRAY_LEN: usize, const LEN: usize> Arena<T, BITARRAY_LEN, LEN>
sourcepub fn remove(&self, slot: u32) -> Option<ArenaArc<T, BITARRAY_LEN, LEN>>
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.