From b2be0a513d7aa53da6fd767eb59b27a7c7d42232 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 27 May 2022 18:47:34 -0400 Subject: [PATCH] Make compute_patch_set take snapshots by value --- src/change_processor.rs | 4 +-- src/serve_session.rs | 2 +- src/snapshot/patch_compute.rs | 47 +++++++++++++++++------------------ src/snapshot/tests/compute.rs | 10 ++++---- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/change_processor.rs b/src/change_processor.rs index 7082a687..dce1cd8d 100644 --- a/src/change_processor.rs +++ b/src/change_processor.rs @@ -291,7 +291,7 @@ fn compute_and_apply_changes(tree: &mut RojoTree, vfs: &Vfs, id: Ref) -> Option< } }; - let patch_set = compute_patch_set(snapshot.as_ref(), &tree, id); + let patch_set = compute_patch_set(snapshot, &tree, id); apply_patch_set(tree, patch_set) } Ok(None) => { @@ -334,7 +334,7 @@ fn compute_and_apply_changes(tree: &mut RojoTree, vfs: &Vfs, id: Ref) -> Option< } }; - let patch_set = compute_patch_set(snapshot.as_ref(), &tree, id); + let patch_set = compute_patch_set(snapshot, &tree, id); apply_patch_set(tree, patch_set) } }; diff --git a/src/serve_session.rs b/src/serve_session.rs index a4eced44..96a68ac9 100644 --- a/src/serve_session.rs +++ b/src/serve_session.rs @@ -130,7 +130,7 @@ impl ServeSession { let snapshot = snapshot_from_vfs(&instance_context, &vfs, &start_path)?; log::trace!("Computing initial patch set"); - let patch_set = compute_patch_set(snapshot.as_ref(), &tree, root_id); + let patch_set = compute_patch_set(snapshot, &tree, root_id); log::trace!("Applying initial patch set"); apply_patch_set(&mut tree, patch_set); diff --git a/src/snapshot/patch_compute.rs b/src/snapshot/patch_compute.rs index c778f1fa..23e2f302 100644 --- a/src/snapshot/patch_compute.rs +++ b/src/snapshot/patch_compute.rs @@ -1,7 +1,10 @@ //! Defines the algorithm for computing a roughly-minimal patch set given an //! existing instance tree and an instance snapshot. -use std::collections::{HashMap, HashSet}; +use std::{ + collections::{HashMap, HashSet}, + mem::take, +}; use rbx_dom_weak::types::{Ref, Variant}; @@ -11,11 +14,7 @@ use super::{ }; #[profiling::function] -pub fn compute_patch_set( - snapshot: Option<&InstanceSnapshot>, - tree: &RojoTree, - id: Ref, -) -> PatchSet { +pub fn compute_patch_set(snapshot: Option, tree: &RojoTree, id: Ref) -> PatchSet { let mut patch_set = PatchSet::new(); if let Some(snapshot) = snapshot { @@ -75,7 +74,7 @@ fn rewrite_refs_in_snapshot(context: &ComputePatchContext, snapshot: &mut Instan fn compute_patch_set_internal( context: &mut ComputePatchContext, - snapshot: &InstanceSnapshot, + mut snapshot: InstanceSnapshot, tree: &RojoTree, id: Ref, patch_set: &mut PatchSet, @@ -88,12 +87,12 @@ fn compute_patch_set_internal( .get_instance(id) .expect("Instance did not exist in tree"); - compute_property_patches(snapshot, &instance, patch_set); - compute_children_patches(context, snapshot, tree, id, patch_set); + compute_property_patches(&mut snapshot, &instance, patch_set); + compute_children_patches(context, &mut snapshot, tree, id, patch_set); } fn compute_property_patches( - snapshot: &InstanceSnapshot, + snapshot: &mut InstanceSnapshot, instance: &InstanceWithMeta, patch_set: &mut PatchSet, ) { @@ -103,32 +102,32 @@ fn compute_property_patches( let changed_name = if snapshot.name == instance.name() { None } else { - Some(snapshot.name.clone().into_owned()) + Some(take(&mut snapshot.name).into_owned()) }; let changed_class_name = if snapshot.class_name == instance.class_name() { None } else { - Some(snapshot.class_name.clone().into_owned()) + Some(take(&mut snapshot.class_name).into_owned()) }; let changed_metadata = if &snapshot.metadata == instance.metadata() { None } else { - Some(snapshot.metadata.clone()) + Some(take(&mut snapshot.metadata)) }; - for (name, snapshot_value) in &snapshot.properties { - visited_properties.insert(name.as_str()); + for (name, snapshot_value) in take(&mut snapshot.properties) { + visited_properties.insert(name.clone()); - match instance.properties().get(name) { + match instance.properties().get(&name) { Some(instance_value) => { - if snapshot_value != instance_value { - changed_properties.insert(name.clone(), Some(snapshot_value.clone())); + if &snapshot_value != instance_value { + changed_properties.insert(name, Some(snapshot_value)); } } None => { - changed_properties.insert(name.clone(), Some(snapshot_value.clone())); + changed_properties.insert(name, Some(snapshot_value)); } } } @@ -160,7 +159,7 @@ fn compute_property_patches( fn compute_children_patches( context: &mut ComputePatchContext, - snapshot: &InstanceSnapshot, + snapshot: &mut InstanceSnapshot, tree: &RojoTree, id: Ref, patch_set: &mut PatchSet, @@ -173,7 +172,7 @@ fn compute_children_patches( let mut paired_instances = vec![false; instance_children.len()]; - for snapshot_child in snapshot.children.iter() { + for snapshot_child in take(&mut snapshot.children) { let matching_instance = instance_children .iter() @@ -210,7 +209,7 @@ fn compute_children_patches( None => { patch_set.added_instances.push(PatchAdd { parent_id: id, - instance: snapshot_child.clone(), + instance: snapshot_child, }); } } @@ -258,7 +257,7 @@ mod test { children: Vec::new(), }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, root_id); + let patch_set = compute_patch_set(Some(snapshot), &tree, root_id); let expected_patch_set = PatchSet { updated_instances: vec![PatchUpdate { @@ -308,7 +307,7 @@ mod test { class_name: Cow::Borrowed("foo"), }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, root_id); + let patch_set = compute_patch_set(Some(snapshot), &tree, root_id); let expected_patch_set = PatchSet { added_instances: vec![PatchAdd { diff --git a/src/snapshot/tests/compute.rs b/src/snapshot/tests/compute.rs index 7b15d378..309d73b6 100644 --- a/src/snapshot/tests/compute.rs +++ b/src/snapshot/tests/compute.rs @@ -23,7 +23,7 @@ fn set_name_and_class_name() { children: Vec::new(), }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, tree.get_root_id()); + let patch_set = compute_patch_set(Some(snapshot), &tree, tree.get_root_id()); let patch_value = redactions.redacted_yaml(patch_set); assert_yaml_snapshot!(patch_value); @@ -47,7 +47,7 @@ fn set_property() { children: Vec::new(), }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, tree.get_root_id()); + let patch_set = compute_patch_set(Some(snapshot), &tree, tree.get_root_id()); let patch_value = redactions.redacted_yaml(patch_set); assert_yaml_snapshot!(patch_value); @@ -78,7 +78,7 @@ fn remove_property() { children: Vec::new(), }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, tree.get_root_id()); + let patch_set = compute_patch_set(Some(snapshot), &tree, tree.get_root_id()); let patch_value = redactions.redacted_yaml(patch_set); assert_yaml_snapshot!(patch_value); @@ -107,7 +107,7 @@ fn add_child() { }], }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, tree.get_root_id()); + let patch_set = compute_patch_set(Some(snapshot), &tree, tree.get_root_id()); let patch_value = redactions.redacted_yaml(patch_set); assert_yaml_snapshot!(patch_value); @@ -139,7 +139,7 @@ fn remove_child() { children: Vec::new(), }; - let patch_set = compute_patch_set(Some(&snapshot), &tree, tree.get_root_id()); + let patch_set = compute_patch_set(Some(snapshot), &tree, tree.get_root_id()); let patch_value = redactions.redacted_yaml(patch_set); assert_yaml_snapshot!(patch_value);