Struct cranelift_egraph::BumpVec
source · pub struct BumpVec<T> { /* private fields */ }
Expand description
A vector of T
stored within a BumpArena
.
This is something like a normal Vec
, except that all accesses
and updates require a separate borrow of the BumpArena
. This, in
turn, makes the Vec itself very compact: only three u32
s (12
bytes). The BumpSlice
variant is only two u32
s (8 bytes) and
is sufficient to reconstruct a slice, but not grow the vector.
The BumpVec
does not implement Clone
or Copy
; it
represents unique ownership of a range of indices in the arena. If
dropped, those indices will be unavailable until the arena is
freed. This is “fine” (it is normally how arena allocation
works). To explicitly free and make available for some
allocations, a very rudimentary reuse mechanism exists via
BumpVec::free(arena)
. (The allocation path opportunistically
checks the first range on the freelist, and can carve off a piece
of it if larger than needed, but it does not attempt to traverse
the entire freelist; this is a compromise between bump-allocation
speed and memory efficiency, which also influences speed through
cached-memory reuse.)
The type T
should not have a Drop
implementation. This
typically means that it does not own any boxed memory,
sub-collections, or other resources. This is important for the
efficiency of the data structure (otherwise, to call Drop
impls,
the arena needs to track which indices are live or dead; the
BumpVec itself cannot do the drop because it does not retain a
reference to the arena). Note that placing a T
with a Drop
impl in the arena is still safe, because leaking (that is, never
calling Drop::drop()
) is safe. It is merely less efficient, and
so should be avoided if possible.
Implementations§
source§impl<T> BumpVec<T>
impl<T> BumpVec<T>
sourcepub fn as_slice<'a>(&'a self, arena: &'a BumpArena<T>) -> &'a [T] ⓘ
pub fn as_slice<'a>(&'a self, arena: &'a BumpArena<T>) -> &'a [T] ⓘ
Returns a slice view of this BumpVec
, given a borrow of the
arena.
sourcepub fn as_mut_slice<'a>(&'a mut self, arena: &'a mut BumpArena<T>) -> &'a mut [T] ⓘ
pub fn as_mut_slice<'a>(&'a mut self, arena: &'a mut BumpArena<T>) -> &'a mut [T] ⓘ
Returns a mutable slice view of this BumpVec
, given a
mutable borrow of the arena.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of this vector. Does not require access to the arena.
sourcepub fn cap(&self) -> usize
pub fn cap(&self) -> usize
Returns the capacity of this vector. Does not require access to the arena.
sourcepub fn reserve(&mut self, extra_len: usize, arena: &mut BumpArena<T>)
pub fn reserve(&mut self, extra_len: usize, arena: &mut BumpArena<T>)
Reserve extra_len
capacity at the end of the vector,
reallocating if necessary.
sourcepub fn push(&mut self, t: T, arena: &mut BumpArena<T>)
pub fn push(&mut self, t: T, arena: &mut BumpArena<T>)
Push an item, growing the capacity if needed.
sourcepub fn clone(&self, arena: &mut BumpArena<T>) -> BumpVec<T>where
T: Clone,
pub fn clone(&self, arena: &mut BumpArena<T>) -> BumpVec<T>where T: Clone,
Clone, if T
is cloneable.