Skip to main content

cranpose_core/slot/
table.rs

1use super::{
2    AnchorRegistry, DeferredDrop, GroupRecord, MovableIndex, NodeRecord, PayloadAnchorRegistry,
3    SlotLifecycleCoordinator, SlotWriteSessionState, debug::SlotTableDiagnostics,
4    payload_store::PayloadStore,
5};
6use crate::collections::growth_slack::GrowthSlack;
7
8mod insert_run;
9mod metadata;
10mod mutation;
11mod values;
12
13pub(crate) use insert_run::ParkedTailStorage;
14#[cfg(test)]
15pub(crate) use values::ValueSlotError;
16
17// A table's number, which no other table of its thread takes: maps key
18// hosts by it and a value slot names its table by it, so a table made where
19// a dropped one lay is not taken for it, and the maps' order does not follow
20// the heap's layout.
21struct SlotStorageIdentity(usize);
22
23impl SlotStorageIdentity {
24    fn new() -> Self {
25        crate::thread_state::cells(|cells| {
26            let id = cells.next_slot_storage_id.get();
27            cells.next_slot_storage_id.set(id + 1);
28            Self(id)
29        })
30    }
31
32    fn id(&self) -> usize {
33        self.0
34    }
35}
36
37pub(crate) struct SlotWriteSession<'a> {
38    pub(super) table: &'a mut SlotTable,
39    pub(in crate::slot) lifecycle: &'a mut SlotLifecycleCoordinator,
40    pub(in crate::slot) state: &'a mut SlotWriteSessionState,
41}
42
43/// Storage for the groups, remembered values and nodes that one composition produced.
44pub struct SlotTable {
45    storage_id: SlotStorageIdentity,
46    pub(super) groups: Vec<GroupRecord>,
47    pub(super) payloads: PayloadStore,
48    pub(super) nodes: Vec<NodeRecord>,
49    pub(super) anchors: AnchorRegistry,
50    pub(super) payload_anchors: PayloadAnchorRegistry,
51    pub(super) movables: MovableIndex,
52    pub(super) diagnostics: SlotTableDiagnostics,
53    stale_group_indexes: metadata::StaleGroupIndexes,
54    insert_run: Option<insert_run::InsertRun>,
55    next_group_generation: u32,
56}
57
58impl SlotTable {
59    /// Creates an empty table.
60    pub fn new() -> Self {
61        Self {
62            storage_id: SlotStorageIdentity::new(),
63            groups: Vec::new(),
64            payloads: PayloadStore::default(),
65            nodes: Vec::new(),
66            anchors: AnchorRegistry::new(),
67            payload_anchors: PayloadAnchorRegistry::new(),
68            movables: MovableIndex::default(),
69            diagnostics: Default::default(),
70            stale_group_indexes: metadata::StaleGroupIndexes::default(),
71            insert_run: None,
72            next_group_generation: 1,
73        }
74    }
75
76    pub(crate) fn write_session<'a>(
77        &'a mut self,
78        lifecycle: &'a mut SlotLifecycleCoordinator,
79        state: &'a mut SlotWriteSessionState,
80    ) -> SlotWriteSession<'a> {
81        SlotWriteSession {
82            table: self,
83            lifecycle,
84            state,
85        }
86    }
87
88    pub(crate) fn storage_id(&self) -> usize {
89        self.storage_id.id()
90    }
91
92    #[cfg(test)]
93    pub(in crate::slot) fn set_next_group_generation_for_test(&mut self, generation: u32) {
94        self.next_group_generation = generation;
95    }
96
97    #[cfg(test)]
98    pub(in crate::slot) fn set_next_group_anchor_for_test(&mut self, next_anchor: usize) {
99        self.anchors.set_next_anchor_for_test(next_anchor);
100    }
101
102    #[cfg(test)]
103    pub(in crate::slot) fn set_next_payload_anchor_for_test(&mut self, next_anchor: usize) {
104        self.payload_anchors.set_next_id_for_test(next_anchor);
105    }
106
107    pub(crate) fn compact_storage(&mut self) {
108        self.groups.shrink_to_fit();
109        self.payloads.compact();
110        self.nodes.shrink_to_fit();
111        self.anchors.shrink_to_fit();
112        self.payload_anchors.shrink_to_fit();
113        self.movables.shrink_to_fit();
114    }
115
116    pub(crate) fn storage_capacity(&self) -> usize {
117        self.groups.capacity() + self.payloads.capacity() + self.nodes.capacity()
118    }
119
120    pub(crate) fn trim_growth_slack(&mut self) {
121        self.groups.trim_growth_slack();
122        self.payloads.trim_growth_slack();
123        self.nodes.trim_growth_slack();
124        self.anchors.trim_growth_slack();
125        self.payload_anchors.trim_growth_slack();
126    }
127
128    pub(crate) fn take_effect_drops(&mut self) -> Vec<DeferredDrop> {
129        let mut drops = Vec::new();
130        self.payloads.for_each_mut(|payload| {
131            if let Some(fresh) = payload.payload_type.fresh {
132                let old = std::mem::replace(&mut payload.value, fresh());
133                drops.push(DeferredDrop::payload(old));
134            }
135        });
136        drops
137    }
138
139    pub(crate) fn take_all_drops(&mut self) -> Vec<DeferredDrop> {
140        let mut drops = Vec::with_capacity(self.payloads.len());
141        self.payloads
142            .drain_rev(|payload| drops.push(payload.into_deferred_drop()));
143        self.groups.clear();
144        self.stale_group_indexes = metadata::StaleGroupIndexes::default();
145        self.nodes.clear();
146        self.anchors.clear();
147        self.payload_anchors.clear();
148        self.movables.clear();
149        drops
150    }
151}
152
153impl Default for SlotTable {
154    fn default() -> Self {
155        Self::new()
156    }
157}