|
| 1 | +use std::any::Any; |
| 2 | + |
| 3 | +use crate::DataSize; |
| 4 | + |
| 5 | +/// Memory usage statistics for a single data type. |
| 6 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 7 | +pub struct MemoryStats { |
| 8 | + /// The total number of instances of this data type. |
| 9 | + pub count: usize, |
| 10 | + |
| 11 | + /// The total number of "stack" bytes used by instances of this data type. |
| 12 | + /// |
| 13 | + /// See [`stack_size_of`] for details on the meaning of this quantity. |
| 14 | + /// |
| 15 | + /// [`stack_size_of`]: Self::stack_size_of |
| 16 | + pub total_stack_bytes: usize, |
| 17 | + |
| 18 | + /// The estimated total number of bytes used by instances of this data type. |
| 19 | + /// |
| 20 | + /// See [`heap_size_of`] for details on the meaning of this quantity. |
| 21 | + /// |
| 22 | + /// [`heap_size_of`]: Self::heap_size_of |
| 23 | + pub total_heap_bytes: usize, |
| 24 | +} |
| 25 | + |
| 26 | +impl MemoryStats { |
| 27 | + /// Returns the sum of `total_stack_bytes` and `total_heap_bytes` for |
| 28 | + /// `self`. |
| 29 | + #[inline] |
| 30 | + pub fn total_bytes(&self) -> usize { |
| 31 | + self.total_stack_bytes + self.total_heap_bytes |
| 32 | + } |
| 33 | + |
| 34 | + /// Returns the computed memory statistics for a single value. |
| 35 | + #[inline] |
| 36 | + pub fn from_value<T>(value: &T) -> Self |
| 37 | + where |
| 38 | + T: Any + DataSize, |
| 39 | + { |
| 40 | + Self { |
| 41 | + count: 1, |
| 42 | + total_stack_bytes: Self::stack_size_of(value), |
| 43 | + total_heap_bytes: Self::heap_size_of(value), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Returns the computed memory statistics for a collection of values. |
| 48 | + #[inline] |
| 49 | + pub fn from_values<'a, T, I>(values: I) -> Self |
| 50 | + where |
| 51 | + T: Any + DataSize, |
| 52 | + I: IntoIterator<Item = &'a T>, |
| 53 | + { |
| 54 | + let mut count = 0; |
| 55 | + let mut total_stack_bytes = 0; |
| 56 | + let mut total_heap_bytes = 0; |
| 57 | + |
| 58 | + for value in values.into_iter() { |
| 59 | + count += 1; |
| 60 | + total_stack_bytes += Self::stack_size_of(value); |
| 61 | + total_heap_bytes += Self::heap_size_of(value); |
| 62 | + } |
| 63 | + |
| 64 | + Self { |
| 65 | + count, |
| 66 | + total_stack_bytes, |
| 67 | + total_heap_bytes, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// Returns the "stack" size of the given value. |
| 72 | + /// |
| 73 | + /// This quantity represents how many bytes the type *would* take up on the |
| 74 | + /// stack if were allocated there. |
| 75 | + /// |
| 76 | + /// This quantity is **exact** and is computed using [`std::mem::size_of`]. |
| 77 | + #[inline] |
| 78 | + pub fn stack_size_of<T>(value: &T) -> usize |
| 79 | + where |
| 80 | + T: Any, |
| 81 | + { |
| 82 | + std::mem::size_of_val(value) |
| 83 | + } |
| 84 | + |
| 85 | + /// Returns the estimated heap size of the given value. |
| 86 | + /// |
| 87 | + /// This quantity represents how many bytes the type occupies apart from the |
| 88 | + /// immediate bytes of its fields. |
| 89 | + #[inline] |
| 90 | + pub fn heap_size_of<T>(value: &T) -> usize |
| 91 | + where |
| 92 | + T: DataSize, |
| 93 | + { |
| 94 | + value.estimate_heap_size() |
| 95 | + } |
| 96 | + |
| 97 | + /// Returns the estimated total size of the given value. |
| 98 | + /// |
| 99 | + /// This quantity is the sum of [`stack_size_of`] and [`heap_size_of`]. |
| 100 | + /// |
| 101 | + /// [`stack_size_of`]: Self::stack_size_of |
| 102 | + /// [`heap_size_of`]: Self::heap_size_of |
| 103 | + pub fn total_size_of<T>(value: &T) -> usize |
| 104 | + where |
| 105 | + T: Any + DataSize, |
| 106 | + { |
| 107 | + Self::stack_size_of(value) + Self::heap_size_of(value) |
| 108 | + } |
| 109 | +} |
0 commit comments