From 20c9c89b27d86e7338aa54afc05428cd49b0db46 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sun, 24 Jun 2018 20:26:58 -0700 Subject: [PATCH] RbxTree robustness * delete_instance is no longer O(n) * renamed get_instance to get_instance_and_descendants, which is more accurate --- server/src/rbx.rs | 21 +++++++++++++-------- server/src/web.rs | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/server/src/rbx.rs b/server/src/rbx.rs index d2aa7b50..f4c2a9a4 100644 --- a/server/src/rbx.rs +++ b/server/src/rbx.rs @@ -68,13 +68,18 @@ impl RbxTree { let mut ids_to_visit = vec![id]; let mut ids_deleted = Vec::new(); - for instance in self.instances.values_mut() { - match instance.children.iter().position(|&v| v == id) { - Some(index) => { - instance.children.remove(index); - }, - None => {}, - } + // We only need to explicitly remove a child from the first instance we + // delete, since all others will descend from this instance. + let parent_id = match self.instances.get(&id) { + Some(instance) => instance.parent, + None => None, + }; + + if let Some(parent_id) = parent_id { + let mut parent = self.instances.get_mut(&parent_id).unwrap(); + let index = parent.children.iter().position(|&v| v == id).unwrap(); + + parent.children.remove(index); } loop { @@ -95,7 +100,7 @@ impl RbxTree { ids_deleted } - pub fn get_instance<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap) + pub fn get_instance_and_descendants<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap) where T: From<&'a RbxInstance> { let mut ids_to_visit = vec![id]; diff --git a/server/src/web.rs b/server/src/web.rs index 7c81641a..2a5fefd4 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -187,7 +187,7 @@ impl Server { let mut instances = HashMap::new(); for requested_id in &requested_ids { - rbx_session.tree.get_instance(*requested_id, &mut instances); + rbx_session.tree.get_instance_and_descendants(*requested_id, &mut instances); } Response::json(&ReadResponse {