Trait datafusion::execution::memory_pool::proxy::VecAllocExt
source · pub trait VecAllocExt {
type T;
// Required methods
fn push_accounted(&mut self, x: Self::T, accounting: &mut usize);
fn allocated_size(&self) -> usize;
}
Expand description
Extension trait for Vec
to account for allocations.
Required Associated Types§
Required Methods§
sourcefn push_accounted(&mut self, x: Self::T, accounting: &mut usize)
fn push_accounted(&mut self, x: Self::T, accounting: &mut usize)
Push new element to vector and increase
accounting
by any newly allocated bytes.
Note that allocation counts capacity, not size
§Example:
// use allocated to incrementally track how much memory is allocated in the vec
let mut allocated = 0;
let mut vec = Vec::new();
// Push data into the vec and the accounting will be updated to reflect
// memory allocation
vec.push_accounted(1, &mut allocated);
assert_eq!(allocated, 16); // space for 4 u32s
vec.push_accounted(1, &mut allocated);
assert_eq!(allocated, 16); // no new allocation needed
// push more data into the vec
for _ in 0..10 { vec.push_accounted(1, &mut allocated); }
assert_eq!(allocated, 64); // underlying vec has space for 10 u32s
assert_eq!(vec.allocated_size(), 64);
§Example with other allocations:
// You can use the same allocated size to track memory allocated by
// another source. For example
let mut allocated = 27;
let mut vec = Vec::new();
vec.push_accounted(1, &mut allocated); // allocates 16 bytes for vec
assert_eq!(allocated, 43); // 16 bytes for vec, 27 bytes for other
sourcefn allocated_size(&self) -> usize
fn allocated_size(&self) -> usize
Return the amount of memory allocated by this Vec to store elements
(size_of<T> * capacity
).
Note this calculation is not recursive, and does not include any heap
allocations contained within the Vec’s elements. Does not include the
size of self
§Example:
let mut vec = Vec::new();
// Push data into the vec and the accounting will be updated to reflect
// memory allocation
vec.push(1);
assert_eq!(vec.allocated_size(), 16); // space for 4 u32s
vec.push(1);
assert_eq!(vec.allocated_size(), 16); // no new allocation needed
// push more data into the vec
for _ in 0..10 { vec.push(1); }
assert_eq!(vec.allocated_size(), 64); // space for 64 now