Skip to main content

cranpose_core/slot/
debug.rs

1use crate::{AnchorId, Key, ScopeId};
2
3/// Size and count figures for one slot table, from `SlotTable::debug_stats`.
4/// Counts are in items and sizes are in bytes.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub struct SlotTableLocalDebugStats {
7    /// Groups stored in the table.
8    pub group_count: usize,
9    /// Groups the group vector holds before it reallocates.
10    pub group_capacity: usize,
11    /// Size of one group record in bytes.
12    pub group_record_size: usize,
13    /// Heap bytes reserved for group records.
14    pub group_heap_bytes: usize,
15    /// Payload slots stored across all groups.
16    pub payload_count: usize,
17    /// Payload slots the payload store holds before it reallocates.
18    pub payload_capacity: usize,
19    /// Payload anchors that point at a live payload.
20    pub active_payload_anchor_count: usize,
21    /// Entries in the payload anchor registry in any state.
22    pub payload_anchor_slot_count: usize,
23    /// Payload anchors whose payload is detached from the table.
24    pub detached_payload_anchor_count: usize,
25    /// Payload anchor entries that no longer resolve to a payload.
26    pub invalidated_payload_anchor_count: usize,
27    /// Payload anchor ids free for reuse.
28    pub free_payload_anchor_count: usize,
29    /// Payload anchor entries the registry holds before it reallocates.
30    pub payload_anchor_capacity: usize,
31    /// Heap bytes reserved by the payload anchor registry.
32    pub payload_anchor_heap_bytes: usize,
33    /// Nodes stored across all groups.
34    pub node_count: usize,
35    /// Nodes the node storage holds before it reallocates.
36    pub node_capacity: usize,
37    /// Group anchors that point at a live group.
38    pub active_anchor_count: usize,
39    /// Entries in the group anchor registry in any state.
40    pub anchor_slot_count: usize,
41    /// Group anchors whose group is detached from the table.
42    pub detached_anchor_count: usize,
43    /// Group anchor entries that no longer resolve to a group.
44    pub invalidated_anchor_count: usize,
45    /// Group anchor ids free for reuse.
46    pub free_anchor_count: usize,
47    /// Anchor entries the registry holds before it reallocates.
48    pub anchor_capacity: usize,
49    /// Heap bytes reserved by the group anchor registry.
50    pub anchor_heap_bytes: usize,
51    /// Counters for subtree moves and index refreshes.
52    #[cfg(any(test, feature = "leak-debug"))]
53    pub mutation: SlotTableMutationDebugStats,
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
57pub(crate) struct SlotLifecycleDebugStats {
58    pub pending_drop_count: usize,
59    pub pending_drop_capacity: usize,
60}
61
62/// Counters for subtrees the composer keeps for hidden content, from the retention state.
63/// Counts are in items and sizes are in bytes.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
65pub struct SlotRetentionDebugStats {
66    /// Subtrees currently retained.
67    pub retained_subtree_count: usize,
68    /// Groups inside retained subtrees.
69    pub retained_group_count: usize,
70    /// Payload slots inside retained subtrees.
71    pub retained_payload_count: usize,
72    /// Nodes inside retained subtrees.
73    pub retained_node_count: usize,
74    /// Recompose scopes inside retained subtrees.
75    pub retained_scope_count: usize,
76    /// Group anchors held by retained subtrees.
77    pub retained_anchor_count: usize,
78    /// Heap bytes held by retained subtrees.
79    pub retained_heap_bytes: usize,
80    /// Subtrees dropped from retention since the composition started.
81    pub retained_evictions_total: usize,
82}
83
84/// Combined size and count figures for a slot table: the table's own figures, its pending
85/// drops and its retained subtrees.
86/// Counts are in items and sizes are in bytes.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
88pub struct SlotTableDebugStats {
89    /// Groups stored in the table.
90    pub group_count: usize,
91    /// Groups the group vector holds before it reallocates.
92    pub group_capacity: usize,
93    /// Size of one group record in bytes.
94    pub group_record_size: usize,
95    /// Heap bytes reserved for group records.
96    pub group_heap_bytes: usize,
97    /// Payload slots stored across all groups.
98    pub payload_count: usize,
99    /// Payload slots the payload store holds before it reallocates.
100    pub payload_capacity: usize,
101    /// Payload anchors that point at a live payload.
102    pub active_payload_anchor_count: usize,
103    /// Entries in the payload anchor registry in any state.
104    pub payload_anchor_slot_count: usize,
105    /// Payload anchors whose payload is detached from the table.
106    pub detached_payload_anchor_count: usize,
107    /// Payload anchor entries that no longer resolve to a payload.
108    pub invalidated_payload_anchor_count: usize,
109    /// Payload anchor ids free for reuse.
110    pub free_payload_anchor_count: usize,
111    /// Payload anchor entries the registry holds before it reallocates.
112    pub payload_anchor_capacity: usize,
113    /// Heap bytes reserved by the payload anchor registry.
114    pub payload_anchor_heap_bytes: usize,
115    /// Nodes stored across all groups.
116    pub node_count: usize,
117    /// Nodes the node storage holds before it reallocates.
118    pub node_capacity: usize,
119    /// Removed values waiting for their deferred drop.
120    pub pending_drop_count: usize,
121    /// Values the pending drop queue holds before it reallocates.
122    pub pending_drop_capacity: usize,
123    /// Group anchors that point at a live group.
124    pub active_anchor_count: usize,
125    /// Entries in the group anchor registry in any state.
126    pub anchor_slot_count: usize,
127    /// Group anchors whose group is detached from the table.
128    pub detached_anchor_count: usize,
129    /// Group anchor entries that no longer resolve to a group.
130    pub invalidated_anchor_count: usize,
131    /// Group anchor ids free for reuse.
132    pub free_anchor_count: usize,
133    /// Anchor entries the registry holds before it reallocates.
134    pub anchor_capacity: usize,
135    /// Heap bytes reserved by the group anchor registry.
136    pub anchor_heap_bytes: usize,
137    /// Subtrees currently retained.
138    pub retained_subtree_count: usize,
139    /// Groups inside retained subtrees.
140    pub retained_group_count: usize,
141    /// Payload slots inside retained subtrees.
142    pub retained_payload_count: usize,
143    /// Nodes inside retained subtrees.
144    pub retained_node_count: usize,
145    /// Recompose scopes inside retained subtrees.
146    pub retained_scope_count: usize,
147    /// Group anchors held by retained subtrees.
148    pub retained_anchor_count: usize,
149    /// Heap bytes held by retained subtrees.
150    pub retained_heap_bytes: usize,
151    /// Subtrees dropped from retention since the composition started.
152    pub retained_evictions_total: usize,
153    /// Counters for subtree moves and index refreshes.
154    #[cfg(any(test, feature = "leak-debug"))]
155    pub mutation: SlotTableMutationDebugStats,
156}
157
158impl SlotTableDebugStats {
159    pub(crate) fn from_parts(
160        local: SlotTableLocalDebugStats,
161        lifecycle: SlotLifecycleDebugStats,
162        retention: SlotRetentionDebugStats,
163    ) -> Self {
164        Self {
165            group_count: local.group_count,
166            group_capacity: local.group_capacity,
167            group_record_size: local.group_record_size,
168            group_heap_bytes: local.group_heap_bytes,
169            payload_count: local.payload_count,
170            payload_capacity: local.payload_capacity,
171            active_payload_anchor_count: local.active_payload_anchor_count,
172            payload_anchor_slot_count: local.payload_anchor_slot_count,
173            detached_payload_anchor_count: local.detached_payload_anchor_count,
174            invalidated_payload_anchor_count: local.invalidated_payload_anchor_count,
175            free_payload_anchor_count: local.free_payload_anchor_count,
176            payload_anchor_capacity: local.payload_anchor_capacity,
177            payload_anchor_heap_bytes: local.payload_anchor_heap_bytes,
178            node_count: local.node_count,
179            node_capacity: local.node_capacity,
180            pending_drop_count: lifecycle.pending_drop_count,
181            pending_drop_capacity: lifecycle.pending_drop_capacity,
182            active_anchor_count: local.active_anchor_count,
183            anchor_slot_count: local.anchor_slot_count,
184            detached_anchor_count: local.detached_anchor_count,
185            invalidated_anchor_count: local.invalidated_anchor_count,
186            free_anchor_count: local.free_anchor_count,
187            anchor_capacity: local.anchor_capacity,
188            anchor_heap_bytes: local.anchor_heap_bytes,
189            retained_subtree_count: retention.retained_subtree_count,
190            retained_group_count: retention.retained_group_count,
191            retained_payload_count: retention.retained_payload_count,
192            retained_node_count: retention.retained_node_count,
193            retained_scope_count: retention.retained_scope_count,
194            retained_anchor_count: retention.retained_anchor_count,
195            retained_heap_bytes: retention.retained_heap_bytes,
196            retained_evictions_total: retention.retained_evictions_total,
197            #[cfg(any(test, feature = "leak-debug"))]
198            mutation: local.mutation,
199        }
200    }
201}
202
203/// Counters for work the slot table does when groups move, from the table's debug stats.
204/// `*_count` fields count operations or items summed over all operations; `*_max_span` fields
205/// hold the largest single operation.
206#[cfg(any(test, feature = "leak-debug"))]
207#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
208pub struct SlotTableMutationDebugStats {
209    /// Subtree moves done.
210    pub subtree_move_count: usize,
211    /// Groups moved, summed over all subtree moves.
212    pub moved_group_count: usize,
213    /// Most groups moved by one subtree move.
214    pub moved_group_max_span: usize,
215    /// Payload slots moved, summed over all subtree moves.
216    pub moved_payload_count: usize,
217    /// Most payload slots moved by one subtree move.
218    pub moved_payload_max_span: usize,
219    /// Nodes moved, summed over all subtree moves.
220    pub moved_node_count: usize,
221    /// Most nodes moved by one subtree move.
222    pub moved_node_max_span: usize,
223    /// Times the payload locations of a range of groups were rewritten.
224    pub payload_location_range_refresh_count: usize,
225    /// Groups covered, summed over all range refreshes of payload locations.
226    pub payload_location_range_refresh_group_count: usize,
227    /// Most groups covered by one range refresh of payload locations.
228    pub payload_location_range_refresh_group_max_span: usize,
229    /// Payload slots rewritten, summed over all range refreshes of payload locations.
230    pub payload_location_range_refresh_payload_count: usize,
231    /// Most payload slots rewritten by one range refresh of payload locations.
232    pub payload_location_range_refresh_payload_max_span: usize,
233    /// Times the payload locations of a single group were rewritten.
234    pub payload_location_refresh_count: usize,
235    /// Payload slots rewritten, summed over all single-group refreshes of payload locations.
236    pub payload_location_refresh_payload_count: usize,
237    /// Most payload slots rewritten by one single-group refresh of payload locations.
238    pub payload_location_refresh_max_span: usize,
239    /// Times the stored group indexes behind the group anchors were rewritten.
240    pub group_index_refresh_count: usize,
241    /// Groups covered, summed over all group index refreshes.
242    pub group_index_refresh_group_count: usize,
243    /// Most groups covered by one group index refresh.
244    pub group_index_refresh_max_span: usize,
245    /// Times the cached payload and node segment ranges were updated.
246    pub segment_range_update_count: usize,
247    /// Groups covered, summed over all segment range updates.
248    pub segment_range_update_group_count: usize,
249    /// Most groups covered by one segment range update.
250    pub segment_range_update_max_span: usize,
251    /// Bytes the payload storage moved to insert, remove, detach, restore or
252    /// reorder payloads.
253    pub payload_shift_bytes: usize,
254}
255
256// The slot table's mutation counters, or without leak-debug a stand-in whose
257// records compile out.
258#[cfg(any(test, feature = "leak-debug"))]
259pub(crate) type SlotTableDiagnostics = SlotTableMutationDebugStats;
260
261#[cfg(not(any(test, feature = "leak-debug")))]
262#[derive(Default)]
263pub(crate) struct SlotTableDiagnostics;
264
265#[cfg(not(any(test, feature = "leak-debug")))]
266impl SlotTableDiagnostics {
267    pub(crate) fn record_subtree_move(&mut self, _groups: usize, _payloads: usize, _nodes: usize) {}
268
269    pub(crate) fn record_payload_location_range_refresh(
270        &mut self,
271        _groups: usize,
272        _payloads: usize,
273    ) {
274    }
275
276    pub(crate) fn record_group_index_refresh(&mut self, _groups: usize) {}
277
278    pub(crate) fn record_payload_location_refresh(&mut self, _payloads: usize) {}
279
280    pub(crate) fn record_segment_range_update(&mut self, _groups: usize) {}
281}
282
283#[cfg(any(test, feature = "leak-debug"))]
284impl SlotTableMutationDebugStats {
285    pub(crate) fn record_subtree_move(
286        &mut self,
287        group_span: usize,
288        payload_span: usize,
289        node_span: usize,
290    ) {
291        self.subtree_move_count = self.subtree_move_count.saturating_add(1);
292        self.moved_group_count = self.moved_group_count.saturating_add(group_span);
293        self.moved_group_max_span = self.moved_group_max_span.max(group_span);
294        self.moved_payload_count = self.moved_payload_count.saturating_add(payload_span);
295        self.moved_payload_max_span = self.moved_payload_max_span.max(payload_span);
296        self.moved_node_count = self.moved_node_count.saturating_add(node_span);
297        self.moved_node_max_span = self.moved_node_max_span.max(node_span);
298    }
299
300    pub(crate) fn record_payload_location_range_refresh(
301        &mut self,
302        group_span: usize,
303        payload_span: usize,
304    ) {
305        self.payload_location_range_refresh_count =
306            self.payload_location_range_refresh_count.saturating_add(1);
307        self.payload_location_range_refresh_group_count = self
308            .payload_location_range_refresh_group_count
309            .saturating_add(group_span);
310        self.payload_location_range_refresh_group_max_span = self
311            .payload_location_range_refresh_group_max_span
312            .max(group_span);
313        self.payload_location_range_refresh_payload_count = self
314            .payload_location_range_refresh_payload_count
315            .saturating_add(payload_span);
316        self.payload_location_range_refresh_payload_max_span = self
317            .payload_location_range_refresh_payload_max_span
318            .max(payload_span);
319    }
320
321    pub(crate) fn record_group_index_refresh(&mut self, group_span: usize) {
322        self.group_index_refresh_count = self.group_index_refresh_count.saturating_add(1);
323        self.group_index_refresh_group_count = self
324            .group_index_refresh_group_count
325            .saturating_add(group_span);
326        self.group_index_refresh_max_span = self.group_index_refresh_max_span.max(group_span);
327    }
328
329    pub(crate) fn record_payload_location_refresh(&mut self, payload_span: usize) {
330        self.payload_location_refresh_count = self.payload_location_refresh_count.saturating_add(1);
331        self.payload_location_refresh_payload_count = self
332            .payload_location_refresh_payload_count
333            .saturating_add(payload_span);
334        self.payload_location_refresh_max_span =
335            self.payload_location_refresh_max_span.max(payload_span);
336    }
337
338    pub(crate) fn record_segment_range_update(&mut self, group_span: usize) {
339        self.segment_range_update_count = self.segment_range_update_count.saturating_add(1);
340        self.segment_range_update_group_count = self
341            .segment_range_update_group_count
342            .saturating_add(group_span);
343        self.segment_range_update_max_span = self.segment_range_update_max_span.max(group_span);
344    }
345}
346
347/// The kind of row in the slot table dump returned by `SlotTable::debug_dump_slot_entries`.
348#[derive(Debug, Clone, Copy, PartialEq, Eq)]
349pub enum SlotDebugEntryKind {
350    /// A group row.
351    Group,
352    /// A payload row, such as a remembered value.
353    Payload,
354    /// A node row.
355    Node,
356}
357
358/// One row in the slot table dump returned by `SlotTable::debug_dump_slot_entries`.
359#[derive(Debug, Clone, PartialEq, Eq)]
360pub struct SlotDebugEntry {
361    /// What kind of item the row describes.
362    pub kind: SlotDebugEntryKind,
363    /// Location of the item in the table, such as `group[2].payload[0]`.
364    pub path: String,
365    /// Text description of the item for logs.
366    pub line: String,
367}
368
369/// A copy of the groups, anchors and scopes of a slot table, for tests and diagnostics.
370#[derive(Debug, Clone, PartialEq, Eq, Default)]
371pub struct SlotDebugSnapshot {
372    /// Every group in the table, in table order.
373    pub active_groups: Vec<SlotDebugGroup>,
374    /// Every live group anchor with the index of the group it points at, sorted by that index.
375    pub anchors: Vec<SlotDebugAnchor>,
376    /// Every recompose scope with its group, sorted by scope id.
377    pub scopes: Vec<SlotDebugScope>,
378    /// Payload slots in the table.
379    pub active_payload_count: usize,
380    /// Nodes in the table.
381    pub active_node_count: usize,
382    /// Groups that own a recompose scope.
383    pub active_scope_count: usize,
384    /// Subtrees retained for hidden content.
385    pub retained_subtree_count: usize,
386    /// Groups inside retained subtrees.
387    pub retained_group_count: usize,
388    /// Payload slots inside retained subtrees.
389    pub retained_payload_count: usize,
390    /// Nodes inside retained subtrees.
391    pub retained_node_count: usize,
392    /// Recompose scopes inside retained subtrees.
393    pub retained_scope_count: usize,
394}
395
396/// One group of a slot table as seen in a [`SlotDebugSnapshot`].
397#[derive(Debug, Clone, Copy, PartialEq, Eq)]
398pub struct SlotDebugGroup {
399    /// Position of the group in the table's group order.
400    pub index: usize,
401    /// Anchor that identifies this group.
402    pub anchor: AnchorId,
403    /// Anchor of the parent group.
404    pub parent_anchor: AnchorId,
405    /// Key of the call site that produced the group.
406    pub static_key: Key,
407    /// Key the caller gave the group, if any.
408    pub explicit_key: Option<Key>,
409    /// Position of the group among earlier sibling groups with the same call site key. A
410    /// keyed group has ordinal 0.
411    pub ordinal: u32,
412    /// Id of the recompose scope the group owns, if any.
413    pub scope_id: Option<ScopeId>,
414    /// Nesting depth of the group; top level groups have the lowest depth.
415    pub depth: u32,
416    /// Number of groups in the group's subtree, including the group itself.
417    pub subtree_len: u32,
418    /// Payload slots the group owns directly.
419    pub payload_len: usize,
420    /// Nodes the group owns directly.
421    pub node_len: usize,
422    /// Nodes in the group's subtree.
423    pub subtree_node_count: u32,
424}
425
426/// A live group anchor and the group it points at, in a [`SlotDebugSnapshot`].
427#[derive(Debug, Clone, Copy, PartialEq, Eq)]
428pub struct SlotDebugAnchor {
429    /// The anchor.
430    pub anchor: AnchorId,
431    /// Index of the group the anchor points at.
432    pub group_index: usize,
433}
434
435/// A recompose scope and the group that owns it, in a [`SlotDebugSnapshot`].
436#[derive(Debug, Clone, Copy, PartialEq, Eq)]
437pub struct SlotDebugScope {
438    /// Id of the recompose scope.
439    pub scope_id: ScopeId,
440    /// Anchor of the group that owns the scope.
441    pub anchor: AnchorId,
442    /// Index of the group that owns the scope.
443    pub group_index: usize,
444}