Skip to main content

dive_core/store/
memory.rs

1//! Requested persistent-storage bytes, distinct from process RSS.
2
3use crate::strings::StringsMemory;
4
5/// A snapshot of known persistent-store storage and explicit accounting gaps.
6///
7/// Allocated slab slots and buffer capacities are measured, including unused
8/// slots. Nonempty retired hash tables are counted once while any cached reader
9/// retains them. This is a component snapshot, not an atomic process snapshot,
10/// a peak-memory measurement or an allocation-time limit.
11///
12/// Known retained bytes exclude allocator rounding/metadata, Arc control blocks,
13/// opaque string-index bucket overhead/spare capacity, and externally owned printer names,
14/// random streams and registers. Worker arenas, caches and graph buffers belong
15/// to their respective owners, not this snapshot. Whole-process RSS accounting
16/// remains necessary and is itself sampled, not a hard allocation-time cap.
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub struct StoreMemory {
19    /// Store object and allocated column/shard/generation/counter arrays.
20    pub fixed_bytes: usize,
21    /// Actual allocated `OnceLock` slab slots, including unused slots.
22    pub slab_bytes: usize,
23    /// Handle slices boxed by large or representation-preserving nodes.
24    pub boxed_payload_bytes: usize,
25    /// Payload bytes in the current deduplication tables.
26    pub current_table_bytes: usize,
27    /// Payload bytes in nonempty old tables still owned by cached readers.
28    pub retired_table_bytes: usize,
29    /// Number of nonempty old tables still owned by cached readers.
30    pub retired_tables: usize,
31    /// Allocated retirement-record vector capacity, including unused slots.
32    pub retirement_tracking_bytes: usize,
33    /// Cumulative temporary `Vec<u64>` bytes requested by index rehashes.
34    /// These are allocation traffic, not currently retained bytes.
35    pub rehash_scratch_bytes: usize,
36    /// Largest individual rehash temporary vector capacity in bytes. This is
37    /// not a concurrent or whole-store peak; old/new tables overlap it.
38    pub largest_rehash_scratch_bytes: usize,
39    /// String-table payload, slot storage and opaque index capacity.
40    pub strings: StringsMemory,
41    /// The store retains a shared external printer/name context.
42    pub has_printer: bool,
43    /// The store retains a shared external random-stream provider.
44    pub has_draws: bool,
45    /// The store retains a shared external register provider.
46    pub has_registers: bool,
47}
48
49impl StoreMemory {
50    /// A lower bound on retained bytes of the measured components, or `None`
51    /// on arithmetic overflow. Rehash traffic is excluded; gaps are documented
52    /// on [`StoreMemory`]. This must not be used as a complete memory budget.
53    #[must_use]
54    pub fn known_retained_bytes(self) -> Option<usize> {
55        [
56            self.fixed_bytes,
57            self.slab_bytes,
58            self.boxed_payload_bytes,
59            self.current_table_bytes,
60            self.retired_table_bytes,
61            self.retirement_tracking_bytes,
62            self.strings.known_retained_bytes()?,
63        ]
64        .into_iter()
65        .try_fold(0usize, usize::checked_add)
66    }
67}