Skip to content

Commit 93d0a72

Browse files
authored
fix: Optimize removal of unreachable nodes (#486)
1 parent d2bcd6d commit 93d0a72

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

consumer/src/tree.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ impl State {
4141
is_host_focused: bool,
4242
mut changes: Option<&mut InternalChanges>,
4343
) {
44-
let mut orphans = HashSet::new();
44+
let mut unreachable = HashSet::new();
4545

4646
if let Some(tree) = update.tree {
4747
if tree.root != self.data.root {
48-
orphans.insert(self.data.root);
48+
unreachable.insert(self.data.root);
4949
}
5050
self.data = tree;
5151
}
@@ -74,7 +74,7 @@ impl State {
7474
for (node_id, node_data) in update.nodes {
7575
let node_data = NodeData::from(node_data);
7676

77-
orphans.remove(&node_id);
77+
unreachable.remove(&node_id);
7878

7979
let mut seen_child_ids = HashSet::with_capacity(node_data.children().len());
8080
for (child_index, child_id) in node_data.children().iter().enumerate() {
@@ -84,7 +84,7 @@ impl State {
8484
node_id.0, child_id.0
8585
);
8686
}
87-
orphans.remove(child_id);
87+
unreachable.remove(child_id);
8888
let parent_and_index = ParentAndIndex(node_id, child_index);
8989
if let Some(child_state) = self.nodes.get_mut_cow(child_id) {
9090
if child_state.parent_and_index != Some(parent_and_index) {
@@ -110,7 +110,7 @@ impl State {
110110
}
111111
for child_id in node_state.data.children().iter() {
112112
if !seen_child_ids.contains(child_id) {
113-
orphans.insert(*child_id);
113+
unreachable.insert(*child_id);
114114
}
115115
}
116116
if *node_state.data != node_data {
@@ -144,31 +144,23 @@ impl State {
144144
self.focus = update.focus;
145145
self.is_host_focused = is_host_focused;
146146

147-
if !orphans.is_empty() {
148-
let mut to_remove = Vec::new();
149-
150-
fn traverse_orphan(
151-
nodes: &ChunkMap<NodeId, NodeState>,
152-
to_remove: &mut Vec<NodeId>,
147+
if !unreachable.is_empty() {
148+
fn traverse_unreachable(
149+
nodes: &mut ChunkMap<NodeId, NodeState>,
150+
changes: &mut Option<&mut InternalChanges>,
153151
id: NodeId,
154152
) {
155-
to_remove.push(id);
156-
let node = nodes.get(&id).unwrap();
153+
if let Some(changes) = changes {
154+
changes.removed_node_ids.insert(id);
155+
}
156+
let node = nodes.remove_cow(&id).unwrap();
157157
for child_id in node.data.children().iter() {
158-
traverse_orphan(nodes, to_remove, *child_id);
158+
traverse_unreachable(nodes, changes, *child_id);
159159
}
160160
}
161161

162-
for id in orphans {
163-
traverse_orphan(&self.nodes, &mut to_remove, id);
164-
}
165-
166-
for id in to_remove {
167-
if self.nodes.remove_cow(&id).is_some() {
168-
if let Some(changes) = &mut changes {
169-
changes.removed_node_ids.insert(id);
170-
}
171-
}
162+
for id in unreachable {
163+
traverse_unreachable(&mut self.nodes, &mut changes, id);
172164
}
173165
}
174166

0 commit comments

Comments
 (0)