Skip to main content

re_byte_size/
lib.rs

1//! Calculate the heap-allocated size of values at runtime.
2
3mod arrow_sizes;
4mod bookkeeping_btreemap;
5mod mem_usage_tree;
6mod parking_lot_sizes;
7mod primitive_sizes;
8mod smallvec_sizes;
9mod std_sizes;
10mod tuple_sizes;
11
12pub mod testing;
13
14pub use self::bookkeeping_btreemap::BookkeepingBTreeMap;
15pub use self::mem_usage_tree::{
16    MemUsageNode, MemUsageTree, MemUsageTreeCapture, NamedMemUsageTree,
17};
18
19// ---
20
21/// Approximations of stack and heap size for both internal and external types.
22///
23/// Motly used for statistics and triggering events such as garbage collection.
24// TODO(#8630): Derive macro for this trait.
25pub trait SizeBytes {
26    /// Returns the total size of `self` in bytes, accounting for both stack and heap space.
27    #[inline]
28    fn total_size_bytes(&self) -> u64 {
29        self.stack_size_bytes() + self.heap_size_bytes()
30    }
31
32    /// Returns the total size of `self` on the stack, in bytes.
33    ///
34    /// Defaults to `std::mem::size_of_val(self)`.
35    #[inline]
36    fn stack_size_bytes(&self) -> u64 {
37        std::mem::size_of_val(self) as _
38    }
39
40    /// Returns how many bytes `self` uses on the heap.
41    ///
42    /// In some cases `self` may be just a slice of a larger buffer.
43    /// This will in that case only return the memory used by that smaller slice.
44    ///
45    /// If we however are the sole owner of the memory (e.g. a `Vec`), then we return
46    /// the heap size of all children plus the capacity of the buffer.
47    fn heap_size_bytes(&self) -> u64;
48
49    /// Is `Self` just plain old data?
50    ///
51    /// If `true`, this will make most blanket implementations of `SizeBytes` much faster (e.g. `Vec<T>`).
52    #[inline]
53    fn is_pod() -> bool {
54        false
55    }
56}