Skip to main content

cranpose_core/slot/
introspection.rs

1use super::{
2    GroupRecord, SlotDebugAnchor, SlotDebugEntry, SlotDebugEntryKind, SlotDebugGroup,
3    SlotDebugScope, SlotDebugSnapshot, SlotTable, SlotTableLocalDebugStats,
4};
5use crate::{Key, RecomposeScope, ScopeId};
6
7impl SlotTable {
8    fn group_heap_bytes(&self) -> usize {
9        self.groups.capacity() * size_of::<GroupRecord>()
10    }
11
12    /// Heap bytes reserved by the table for groups, payloads, nodes and anchors.
13    pub fn heap_bytes(&self) -> usize {
14        self.group_heap_bytes()
15            + self.payloads.heap_bytes()
16            + self.node_heap_bytes()
17            + self.anchors.heap_bytes()
18            + self.payload_anchors.heap_bytes()
19    }
20
21    /// Returns size and count figures for this table alone, without retained or pending-drop data.
22    pub fn debug_stats(&self) -> SlotTableLocalDebugStats {
23        SlotTableLocalDebugStats {
24            group_count: self.groups.len(),
25            group_capacity: self.groups.capacity(),
26            group_record_size: size_of::<GroupRecord>(),
27            group_heap_bytes: self.group_heap_bytes(),
28            payload_count: self.total_payload_count(),
29            payload_capacity: self.payloads.capacity(),
30            active_payload_anchor_count: self.payload_anchors.active_len(),
31            payload_anchor_slot_count: self.payload_anchors.slot_len(),
32            detached_payload_anchor_count: self.payload_anchors.detached_len(),
33            invalidated_payload_anchor_count: self.payload_anchors.invalidated_len(),
34            free_payload_anchor_count: self.payload_anchors.free_len(),
35            payload_anchor_capacity: self.payload_anchors.capacity(),
36            payload_anchor_heap_bytes: self.payload_anchors.heap_bytes(),
37            node_count: self.total_node_count(),
38            node_capacity: self.node_debug_capacity(),
39            active_anchor_count: self.anchors.active_len(),
40            anchor_slot_count: self.anchors.slot_len(),
41            detached_anchor_count: self.anchors.detached_len(),
42            invalidated_anchor_count: self.anchors.invalidated_len(),
43            free_anchor_count: self.anchors.free_len(),
44            anchor_capacity: self.anchors.capacity(),
45            anchor_heap_bytes: self.anchors.heap_bytes(),
46            #[cfg(any(test, feature = "leak-debug"))]
47            mutation: super::SlotTableMutationDebugStats {
48                payload_shift_bytes: self.payloads.shift_bytes(),
49                ..self.diagnostics
50            },
51        }
52    }
53
54    /// Returns one `(group index, call site key, scope id, subtree length)` tuple per group.
55    pub fn debug_dump_groups(&self) -> Vec<(usize, Key, Option<ScopeId>, usize)> {
56        self.groups
57            .iter()
58            .enumerate()
59            .map(|(index, group)| {
60                (
61                    index,
62                    group.key.static_key,
63                    group.scope.as_ref().map(RecomposeScope::id),
64                    group.subtree_len as usize,
65                )
66            })
67            .collect()
68    }
69
70    /// Returns a copy of the groups, anchors and scopes of this table.
71    /// The retained subtree counts are zero here; the composer fills them in.
72    pub fn debug_snapshot(&self) -> SlotDebugSnapshot {
73        let active_groups = self
74            .groups
75            .iter()
76            .enumerate()
77            .map(|(index, group)| SlotDebugGroup {
78                index,
79                anchor: group.anchor,
80                parent_anchor: group.parent_anchor,
81                static_key: group.key.static_key,
82                explicit_key: group.key.explicit_key,
83                ordinal: group.key.ordinal,
84                scope_id: group.scope.as_ref().map(RecomposeScope::id),
85                depth: group.depth,
86                subtree_len: group.subtree_len,
87                payload_len: self.group_payload_len_at(index),
88                node_len: self.group_node_len_at(index),
89                subtree_node_count: group.subtree_node_count,
90            })
91            .collect::<Vec<_>>();
92        let mut anchors = self
93            .anchors
94            .active_entries()
95            .map(|(anchor, stored)| SlotDebugAnchor {
96                anchor,
97                group_index: self.resolve_group_index(stored, anchor).unwrap_or(stored),
98            })
99            .collect::<Vec<_>>();
100        anchors.sort_by_key(|entry| entry.group_index);
101
102        let mut scopes = self
103            .groups
104            .iter()
105            .enumerate()
106            .filter_map(|(group_index, group)| {
107                group.scope.as_ref().map(|scope| SlotDebugScope {
108                    scope_id: scope.id(),
109                    anchor: group.anchor,
110                    group_index,
111                })
112            })
113            .collect::<Vec<_>>();
114        scopes.sort_by_key(|entry| entry.scope_id);
115
116        SlotDebugSnapshot {
117            active_payload_count: self.total_payload_count(),
118            active_node_count: self.total_node_count(),
119            active_scope_count: scopes.len(),
120            active_groups,
121            anchors,
122            scopes,
123            retained_subtree_count: 0,
124            retained_group_count: 0,
125            retained_payload_count: 0,
126            retained_node_count: 0,
127            retained_scope_count: 0,
128        }
129    }
130
131    /// Returns one row per group, payload and node in the table, in that order.
132    pub fn debug_dump_slot_entries(&self) -> Vec<SlotDebugEntry> {
133        let mut rows = Vec::new();
134        for (index, group) in self.groups.iter().enumerate() {
135            rows.push(SlotDebugEntry {
136                kind: SlotDebugEntryKind::Group,
137                path: format!("group[{index}]"),
138                line: format!(
139                    "Group(key={:?}, scope={:?}, subtree_len={}, payload_len={}, node_len={})",
140                    group.key,
141                    group.scope.as_ref().map(RecomposeScope::id),
142                    group.subtree_len,
143                    self.group_payload_len_at(index),
144                    self.group_node_len_at(index)
145                ),
146            });
147        }
148        for (group_index, group) in self.groups.iter().enumerate() {
149            for (payload_index, payload) in self.group_payload_records_at(group_index).enumerate() {
150                rows.push(SlotDebugEntry {
151                    kind: SlotDebugEntryKind::Payload,
152                    path: format!("group[{group_index}].payload[{payload_index}]"),
153                    line: format!(
154                        "Payload(owner={:?}, kind={}, type={})",
155                        group.anchor,
156                        payload.kind.label(),
157                        (payload.payload_type.type_name)()
158                    ),
159                });
160            }
161        }
162        for (group_index, _) in self.groups.iter().enumerate() {
163            for (node_index, node) in self.group_node_records_at(group_index).iter().enumerate() {
164                rows.push(SlotDebugEntry {
165                    kind: SlotDebugEntryKind::Node,
166                    path: format!("group[{group_index}].node[{node_index}]"),
167                    line: format!(
168                        "Node(owner={:?}, parent={:?}, id={}, gen={}, lifecycle={:?})",
169                        node.owner, node.parent_id, node.id, node.generation, node.lifecycle
170                    ),
171                });
172            }
173        }
174        rows
175    }
176}