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