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.

  1. Composite types, such as SpaceDescriptor(usize), doesn’t implement the IsZero trait, even if it has the #[repr(transparent)] annotation.
  2. The IsZero trait is private to the std 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§