Module mmtk::util::rust_util::zeroed_alloc
source · Expand description
This module is for allocating large arrays or vectors with initial zero values.
Note: The standard library uses the IsZero
trait to specialize the intialization of Vec<T>
if the initial element values are zero. Primitive type, such as i8
, usize
, f32
, as well
as types with known representations such as Option<NonZeroUsize>
implement the IsZero
trait. However, it has several limitations.
- Composite types, such as
SpaceDescriptor(usize)
, doesn’t implement theIsZero
trait, even if it has the#[repr(transparent)]
annotation. - The
IsZero
trait is private to thestd
module, and we cannot use it.
Therefore, vec![0usize; 33554432]
takes only 4 microseconds, while
vec![SpaceDescriptor(0); 33554432]
will take 22 milliseconds to execute on some machine.
If such an allocation happens during start-up, the delay will be noticeable to light-weight
scripting languages, such as Ruby.
(Note: We no longer allocate such large vecs at start-up. We keep this module in case we need to allocate large vectors in the future.)
We implement our own fast allocation of large zeroed vectors in this module. If one day Rust provides a standard way to optimize for zeroed allocation of vectors of composite types, we can switch to the standard mechanism.
Functions§
- new_zeroed_vec 🔒 ⚠Allocate a
Vec<T>
of all-zero values.