pub struct OptionCore<T> { /* private fields */ }
Expand description
A Core
implementation that is essentially a Vec<Option<T>>
.
This implementation is quite different from the BitVecCore
. This one only
manages one allocation, meaning it does not suffer from the same
disadvantages as BitVecCore
. This can sometimes lead to better memory
access times due to caching effects. However, this implementation has the
major disadvantage of wasting memory in most cases.
Usually, size_of::<Option<T>>() > size_of::<T>()
. The difference can be
as high as 4 byte due to alignment. But as only one bit is used to store
the None/Some
information, all that memory is wasted. A worst case
scenario is something like Option<u32>
: this is 8 byte large (on most
platforms), meaning that a stable vector with OptionCore
would use twice
as much memory as a Vec<u32>
. This is not only wasteful, but has a
negative effect on speed as the information is not very densely packed.
In general, only use this implementation in one of these cases:
- Your
T
isNonNull
, meaning thatOption<T>
has the same size asT
. This is then even more memory efficient than the default core implementation. Iterating over indices of a stable vector is still slower in this case as the relevant information is further apart. - Your
T
is very large, meaning that the amount of wasted memory is small in comparison.
In both cases, switching the implementation from default to this only makes sense after you measured that you actually gain performance from it. The interface of both implementations is exactly the same.
Trait Implementations§
Source§impl<T: Clone> Clone for OptionCore<T>
impl<T: Clone> Clone for OptionCore<T>
Source§impl<T> Core<T> for OptionCore<T>
impl<T> Core<T> for OptionCore<T>
Source§unsafe fn has_element_at(&self, idx: usize) -> bool
unsafe fn has_element_at(&self, idx: usize) -> bool
Assumes that idx < capacity
Source§fn new() -> Self
fn new() -> Self
Source§fn len(&self) -> usize
fn len(&self) -> usize
len
). See the trait docs for
more information.Source§fn cap(&self) -> usize
fn cap(&self) -> usize
cap
). See the trait docs for
more information.Source§unsafe fn realloc(&mut self, new_cap: usize)
unsafe fn realloc(&mut self, new_cap: usize)
cap
of at least new_cap
. This
method should try its best to allocate exactly new_cap
. Read moreSource§unsafe fn remove_at(&mut self, idx: usize) -> T
unsafe fn remove_at(&mut self, idx: usize) -> T
idx
and returns it. Read moreSource§unsafe fn get_unchecked(&self, idx: usize) -> &T
unsafe fn get_unchecked(&self, idx: usize) -> &T
idx
. Read moreSource§unsafe fn get_unchecked_mut(&mut self, idx: usize) -> &mut T
unsafe fn get_unchecked_mut(&mut self, idx: usize) -> &mut T
idx
. Read moreSource§fn clear(&mut self)
fn clear(&mut self)
len
to 0. Read moreSource§unsafe fn swap(&mut self, a: usize, b: usize)
unsafe fn swap(&mut self, a: usize, b: usize)
a
and b
. That is: the element
and the “filled/empty” status are swapped. The slots at indices a
and b
can be empty or filled. Read moreSource§unsafe fn first_filled_slot_from(&self, idx: usize) -> Option<usize>
unsafe fn first_filled_slot_from(&self, idx: usize) -> Option<usize>
idx
, returning the
index of the first filled slot that is found. Read moreSource§unsafe fn first_filled_slot_below(&self, idx: usize) -> Option<usize>
unsafe fn first_filled_slot_below(&self, idx: usize) -> Option<usize>
idx - 1
, returning the
index of the first filled slot that is found. Read more