cranpose_core/node_marks.rs
1//! A small value for each applier node, kept in the applier's dense node
2//! order: one walk over the tree reads and sets a node's mark with the two
3//! array reads that find its storage, where a hash set hashed the id and
4//! probed its groups.
5
6use crate::{MemoryApplier, NodeId, collections::map::HashMap};
7
8/// Marks on the nodes of one [`MemoryApplier`], for one walk over its tree.
9///
10/// A mark is a byte; an unmarked node reads `0`. Marks stay until
11/// [`NodeMarks::reset`], and only a walk that leaves the applier's tree as it
12/// is may keep them: a node stored again in another place loses its mark.
13///
14/// ```
15/// use cranpose_core::{MemoryApplier, NodeId, NodeMarks};
16///
17/// let applier = MemoryApplier::new();
18/// let mut marks = NodeMarks::default();
19/// marks.reset(&applier);
20/// assert_eq!(marks.get(&applier, NodeId(7)), 0);
21/// marks.set(&applier, NodeId(7), 2);
22/// assert_eq!(marks.get(&applier, NodeId(7)), 2);
23/// ```
24#[derive(Clone, Default)]
25pub struct NodeMarks {
26 dense: Vec<u8>,
27 marked: MarkedSlots,
28 elsewhere: HashMap<NodeId, u8>,
29}
30
31// A reset clears the slots it listed while they are at most one in this many
32// of all, and at most `MAX_LISTED`; past that, it clears every slot, and the
33// list keeps no more room than that for a walk that marked most nodes.
34const LISTED_SHARE: usize = 32;
35const MAX_LISTED: usize = 2048;
36// Below this many slots a walk lists none and a reset clears every slot: a
37// walk of a busy frame marks a large share of such an applier's nodes, and
38// its notes cost more than the clear.
39const MIN_LISTED: usize = 16_384;
40
41// The dense slots given a mark since the last reset, while `room` is not 0: a
42// reset clears those alone, so its work follows the marks a walk set, not the
43// applier's node count. A `room` of 0, set by a reset of a small applier or by
44// a walk that marks more than `room` slots, makes the reset clear every slot.
45#[derive(Clone, Default)]
46struct MarkedSlots {
47 slots: Vec<usize>,
48 room: usize,
49}
50
51impl MarkedSlots {
52 #[inline]
53 fn note(&mut self, slot: usize) {
54 if self.slots.len() < self.room {
55 self.slots.push(slot);
56 } else {
57 self.room = 0;
58 }
59 }
60
61 fn clear(&mut self, dense: &mut [u8]) {
62 if self.room == 0 {
63 dense.fill(0);
64 } else {
65 for &slot in &self.slots {
66 dense[slot] = 0;
67 }
68 }
69 self.slots.clear();
70 }
71}
72
73impl NodeMarks {
74 /// Unmarks every node, with room for every node `applier` holds now.
75 pub fn reset(&mut self, applier: &MemoryApplier) {
76 self.marked.clear(&mut self.dense);
77 let nodes = applier.state.nodes.len();
78 if self.dense.len() < nodes {
79 self.dense.resize(nodes, 0);
80 }
81 self.marked.room = if self.dense.len() < MIN_LISTED {
82 0
83 } else {
84 (self.dense.len() / LISTED_SHARE).min(MAX_LISTED)
85 };
86 // A clear writes every control byte of the map, even of an empty one.
87 if !self.elsewhere.is_empty() {
88 self.elsewhere.clear();
89 }
90 }
91
92 /// The mark of `id`: `0` while it has none.
93 #[inline]
94 pub fn get(&self, applier: &MemoryApplier, id: NodeId) -> u8 {
95 self.mark_at(MarkPlace::of(applier, id))
96 }
97
98 /// Adds the bits of `mark` to `id`'s mark, finding its storage once,
99 /// and returns the mark it had.
100 ///
101 /// ```
102 /// use cranpose_core::{MemoryApplier, NodeId, NodeMarks};
103 ///
104 /// let applier = MemoryApplier::new();
105 /// let mut marks = NodeMarks::default();
106 /// marks.reset(&applier);
107 /// assert_eq!(marks.add(&applier, NodeId(7), 1), 0);
108 /// assert_eq!(marks.add(&applier, NodeId(7), 2), 1);
109 /// assert_eq!(marks.get(&applier, NodeId(7)), 3);
110 /// ```
111 #[inline]
112 pub fn add(&mut self, applier: &MemoryApplier, id: NodeId, mark: u8) -> u8 {
113 let entry = self.entry_at(MarkPlace::of(applier, id));
114 let had = *entry;
115 *entry |= mark;
116 had
117 }
118
119 /// Adds the bits of `mark` to `id`'s mark and those of `ancestor_mark`
120 /// to the mark of each node above it, from its parent up to the first
121 /// that had them. Each node's storage is found once.
122 pub fn add_with_ancestors(
123 &mut self,
124 applier: &MemoryApplier,
125 id: NodeId,
126 mark: u8,
127 ancestor_mark: u8,
128 ) {
129 let mut place = MarkPlace::of(applier, id);
130 *self.entry_at(place) |= mark;
131 while let Some(parent) = applier.parent_at(place) {
132 place = MarkPlace::of(applier, parent);
133 let entry = self.entry_at(place);
134 let had = *entry;
135 *entry |= ancestor_mark;
136 if had & ancestor_mark == ancestor_mark {
137 break;
138 }
139 }
140 }
141
142 /// Marks `id` with `mark`.
143 #[inline]
144 pub fn set(&mut self, applier: &MemoryApplier, id: NodeId, mark: u8) {
145 *self.entry_at(MarkPlace::of(applier, id)) = mark;
146 }
147
148 #[inline]
149 pub(crate) fn mark_at(&self, place: MarkPlace) -> u8 {
150 match place {
151 MarkPlace::Dense(slot) => self.dense.get(slot).copied().unwrap_or(0),
152 MarkPlace::Elsewhere(id) => self.elsewhere.get(&id).copied().unwrap_or(0),
153 }
154 }
155
156 #[inline]
157 pub(crate) fn entry_at(&mut self, place: MarkPlace) -> &mut u8 {
158 match place {
159 MarkPlace::Dense(slot) if slot < self.dense.len() => {
160 let entry = &mut self.dense[slot];
161 if self.marked.room != 0 && *entry == 0 {
162 self.marked.note(slot);
163 }
164 entry
165 }
166 _ => self.grown_entry_at(place),
167 }
168 }
169
170 // The entry of a node stored past the room `reset` made, or kept apart.
171 #[cold]
172 #[inline(never)]
173 fn grown_entry_at(&mut self, place: MarkPlace) -> &mut u8 {
174 match place {
175 MarkPlace::Dense(slot) => {
176 self.dense.resize(slot + 1, 0);
177 self.marked.note(slot);
178 &mut self.dense[slot]
179 }
180 MarkPlace::Elsewhere(id) => self.elsewhere.entry(id).or_insert(0),
181 }
182 }
183}
184
185// Where a node's mark is kept, and its node in the applier: the node's
186// dense storage slot, or its id for a node kept apart.
187#[derive(Clone, Copy)]
188pub(crate) enum MarkPlace {
189 Dense(usize),
190 Elsewhere(NodeId),
191}
192
193impl MarkPlace {
194 #[inline]
195 pub(crate) fn of(applier: &MemoryApplier, id: NodeId) -> Self {
196 applier
197 .resolve_node_index(id)
198 .map_or(Self::Elsewhere(id), Self::Dense)
199 }
200}