Skip to main content

knotrel_core/
hdt_profile.rs

1//! Read-only structural storage accounting for HDT.
2//!
3//! Vector capacities measure requested buffer storage, not allocator usable size.
4//! Payload byte values count live keys and values of the ordered (B-tree) and
5//! hashed containers, and exclude B-tree spare slots, node metadata and hash
6//! table overhead. Neither value is total heap usage or RSS; allocator overhead
7//! and the snapshot are excluded.
8
9/// Structural storage snapshot of an [`crate::HdtGraph`], from
10/// [`crate::HdtGraph::storage_stats`], collected outside timed graph
11/// operations.
12///
13/// A stable interface of the experimental HDT engine
14/// ([`crate::EngineConfig::Hdt`]). The struct is exhaustive: its eight public
15/// fields are frozen for 1.x, and a new field requires a major release.
16///
17/// # Guaranteed
18///
19/// For a snapshot `s` of a graph `g`, computed at the call:
20///
21/// - `s.vertex_count == g.node_count()` and `s.edge_count == g.edge_count()`.
22/// - Every `*_bytes` field is in bytes; `vertex_count`, `edge_count`,
23///   `available_levels` and the per-level counts are plain counts.
24/// - `s.vector_capacity_bytes` is the sum of `s.level_capacity_bytes`,
25///   `s.edge_arc_capacity_bytes` and, for every level, its
26///   `token_capacity_bytes`, `vertex_index_capacity_bytes`,
27///   `free_list_capacity_bytes`, `adjacency_capacity_bytes` and
28///   `mapping_capacity_bytes`.
29/// - `s.levels` is empty exactly when `s.vertex_count == 0`, and
30///   `s.levels[i].level == i` for every index `i` (ascending, from level 0).
31/// - `s.available_levels == 0` exactly when `s.vertex_count == 0`, and
32///   `s.levels.len() <= s.available_levels`. Since vertices are never
33///   removed, `available_levels` never decreases for one graph.
34/// - Level 0 holds a spanning forest of the whole graph:
35///   `s.levels[0].tree_edges` equals `vertex_count` minus the number of
36///   connected components, and `tree_edges` does not increase from one level
37///   to the next (each level's forest contains the next one's).
38/// - Each edge has exactly one level, where it is counted at both endpoints:
39///   the sum over all levels of `tree_incidences + non_tree_incidences` is
40///   `2 * s.edge_count`, and the sum of `tree_incidences` is
41///   `2 * s.levels[0].tree_edges`.
42///
43/// # Implementation-dependent
44///
45/// Every capacity and payload byte value, the number of materialized levels,
46/// the per-level `vertices` record slots, which edges are tree edges at which
47/// level, and the exact `available_levels` beyond the properties above depend
48/// on the implementation, the operation history and the standard library's
49/// growth policy, and may change in any release. Capacities retain their
50/// peaks after removals. None of the values is total heap usage or RSS:
51/// allocator overhead, hash table and B-tree overhead and the snapshot itself
52/// are excluded.
53///
54/// # Examples
55///
56/// ```
57/// use knotrel_core::HdtGraph;
58///
59/// let mut graph = HdtGraph::new();
60/// graph.link(1, 2)?;
61/// graph.link(2, 3)?;
62/// graph.link(3, 1)?;
63/// graph.add_node(4);
64/// let storage = graph.storage_stats();
65/// assert_eq!(storage.vertex_count, 4);
66/// assert_eq!(storage.edge_count, 3);
67/// // Four vertices in two components: a spanning forest of two edges.
68/// assert_eq!(storage.levels[0].tree_edges, 2);
69/// let incidences: usize = storage
70///     .levels
71///     .iter()
72///     .map(|level| level.tree_incidences + level.non_tree_incidences)
73///     .sum();
74/// assert_eq!(incidences, 2 * storage.edge_count);
75/// # Ok::<(), knotrel_core::GraphError>(())
76/// ```
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct HdtStorageStats {
79    /// Registered graph vertices.
80    pub vertex_count: usize,
81    /// Live graph edges.
82    pub edge_count: usize,
83    /// Number of levels allowed by the current vertex universe.
84    pub available_levels: usize,
85    /// Owned vector capacities in bytes, excluding this snapshot.
86    pub vector_capacity_bytes: usize,
87    /// Live key/value payload bytes of the graph's ordered (B-tree) and hashed
88    /// containers: the vertex-ID and edge hash maps, and for every level its
89    /// B-tree adjacency sets and its `mapping_payload_bytes`. Despite the
90    /// name, hashed containers are included; B-tree and hash table overhead
91    /// are excluded.
92    pub ordered_payload_bytes: usize,
93    /// Capacity bytes of per-edge tour handle vectors.
94    pub edge_arc_capacity_bytes: usize,
95    /// Capacity bytes of the level vector, including inline container headers.
96    pub level_capacity_bytes: usize,
97    /// Materialized levels, in ascending order.
98    pub levels: Vec<HdtLevelStorage>,
99}
100
101/// Storage and live incidences of one materialized HDT level, an element of
102/// [`HdtStorageStats::levels`].
103///
104/// A stable interface of the experimental HDT engine; the struct is
105/// exhaustive, so its eleven public fields are frozen for 1.x. The properties
106/// that relate these fields to each other and to the graph are guaranteed as
107/// listed on [`HdtStorageStats`]; every byte value and `vertices` are
108/// implementation-dependent. `Default` gives an all-zero value.
109#[derive(Debug, Default, Clone, PartialEq, Eq)]
110pub struct HdtLevelStorage {
111    /// Level index.
112    pub level: usize,
113    /// Vertex record slots: live records plus released slots awaiting reuse.
114    pub vertices: usize,
115    /// Live spanning edges in this forest, including higher-level edges.
116    pub tree_edges: usize,
117    /// Exact-level tree incidences, counting both endpoints.
118    pub tree_incidences: usize,
119    /// Exact-level non-tree incidences, counting both endpoints.
120    pub non_tree_incidences: usize,
121    /// Euler-tour token arena capacity bytes.
122    pub token_capacity_bytes: usize,
123    /// Vertex-to-token vector capacity bytes.
124    pub vertex_index_capacity_bytes: usize,
125    /// Recycled-token handle vector capacity bytes.
126    pub free_list_capacity_bytes: usize,
127    /// Capacity bytes of both vectors of adjacency set headers.
128    pub adjacency_capacity_bytes: usize,
129    /// Capacity bytes of the local-to-global mapping and released-slot vectors.
130    pub mapping_capacity_bytes: usize,
131    /// Live key/value payload bytes of the global-to-local mapping, a hash
132    /// map; excludes hash table overhead.
133    pub mapping_payload_bytes: usize,
134}