From 3f55673aa70c358e7f0169fc2b64d65a697d3a48 Mon Sep 17 00:00:00 2001 From: Sean Olson Date: Tue, 8 Dec 2020 11:29:36 -0800 Subject: [PATCH] Implement `shrink_to_fit` for `MeshGraph`. This change introduces a `shrink_to_fit` function for `MeshGraph`s. To achieve this with little overhead, `Core` now exposes its storage fields in the `graph` module, bypassing `AsStorage` and `Dispatch`. The dispatch mechanism isn't needed for `MeshGraph`, which only ever operates on an `OwnedCore` and known storage types. --- plexus/src/entity/storage/hash.rs | 12 ++++++++ plexus/src/graph/core.rs | 8 +++--- plexus/src/graph/mod.rs | 47 ++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/plexus/src/entity/storage/hash.rs b/plexus/src/entity/storage/hash.rs index 78303e19..a996aac8 100644 --- a/plexus/src/entity/storage/hash.rs +++ b/plexus/src/entity/storage/hash.rs @@ -28,6 +28,18 @@ where phantom: PhantomData

, } +impl HashStorage +where + E: Entity, + InnerKey: Eq + Hash, + R: Default, + P: Mode, +{ + pub fn shrink_to_fit(&mut self) { + self.inner.shrink_to_fit(); + } +} + impl AsStorage for HashStorage where E: Entity, diff --git a/plexus/src/graph/core.rs b/plexus/src/graph/core.rs index 1df4508c..62cadd42 100644 --- a/plexus/src/graph/core.rs +++ b/plexus/src/graph/core.rs @@ -41,10 +41,10 @@ pub struct Core where G: GraphData, { - vertices: V, - arcs: A, - edges: E, - faces: F, + pub(in crate::graph) vertices: V, + pub(in crate::graph) arcs: A, + pub(in crate::graph) edges: E, + pub(in crate::graph) faces: F, phantom: PhantomData, } diff --git a/plexus/src/graph/mod.rs b/plexus/src/graph/mod.rs index 41a4cd03..35f0c7bd 100644 --- a/plexus/src/graph/mod.rs +++ b/plexus/src/graph/mod.rs @@ -564,7 +564,7 @@ where /// Gets the number of vertices in the graph. pub fn vertex_count(&self) -> usize { - self.as_storage_of::>().len() + self.core.vertices.len() } /// Gets an immutable view of the vertex with the given key. @@ -580,7 +580,8 @@ where // TODO: Return `Clone + Iterator`. /// Gets an iterator of immutable views over the vertices in the graph. pub fn vertices(&self) -> impl Iterator> { - self.as_storage_of::>() + self.core + .vertices .iter() .map(|(key, _)| key) .map(move |key| View::bind_unchecked(self, key)) @@ -589,7 +590,8 @@ where /// Gets an iterator of orphan views over the vertices in the graph. pub fn vertex_orphans(&mut self) -> impl Iterator> { - self.as_storage_mut_of::>() + self.core + .vertices .iter_mut() .map(|(key, data)| Orphan::bind_unchecked(data, key)) .map(From::from) @@ -597,7 +599,7 @@ where /// Gets the number of arcs in the graph. pub fn arc_count(&self) -> usize { - self.as_storage_of::>().len() + self.core.arcs.len() } /// Gets an immutable view of the arc with the given key. @@ -613,7 +615,8 @@ where // TODO: Return `Clone + Iterator`. /// Gets an iterator of immutable views over the arcs in the graph. pub fn arcs(&self) -> impl Iterator> { - self.as_storage_of::>() + self.core + .arcs .iter() .map(|(key, _)| key) .map(move |key| View::bind_unchecked(self, key)) @@ -622,7 +625,8 @@ where /// Gets an iterator of orphan views over the arcs in the graph. pub fn arc_orphans(&mut self) -> impl Iterator> { - self.as_storage_mut_of::>() + self.core + .arcs .iter_mut() .map(|(key, data)| Orphan::bind_unchecked(data, key)) .map(From::from) @@ -630,7 +634,7 @@ where /// Gets the number of edges in the graph. pub fn edge_count(&self) -> usize { - self.as_storage_of::>().len() + self.core.edges.len() } /// Gets an immutable view of the edge with the given key. @@ -646,7 +650,8 @@ where // TODO: Return `Clone + Iterator`. /// Gets an iterator of immutable views over the edges in the graph. pub fn edges(&self) -> impl Iterator> { - self.as_storage_of::>() + self.core + .edges .iter() .map(|(key, _)| key) .map(move |key| View::bind_unchecked(self, key)) @@ -655,7 +660,8 @@ where /// Gets an iterator of orphan views over the edges in the graph. pub fn edge_orphans(&mut self) -> impl Iterator> { - self.as_storage_mut_of::>() + self.core + .edges .iter_mut() .map(|(key, data)| Orphan::bind_unchecked(data, key)) .map(From::from) @@ -663,7 +669,7 @@ where /// Gets the number of faces in the graph. pub fn face_count(&self) -> usize { - self.as_storage_of::>().len() + self.core.faces.len() } /// Gets an immutable view of the face with the given key. @@ -679,7 +685,8 @@ where // TODO: Return `Clone + Iterator`. /// Gets an iterator of immutable views over the faces in the graph. pub fn faces(&self) -> impl Iterator> { - self.as_storage_of::>() + self.core + .faces .iter() .map(|(key, _)| key) .map(move |key| View::bind_unchecked(self, key)) @@ -688,7 +695,8 @@ where /// Gets an iterator of orphan views over the faces in the graph. pub fn face_orphans(&mut self) -> impl Iterator> { - self.as_storage_mut_of::>() + self.core + .faces .iter_mut() .map(|(key, data)| Orphan::bind_unchecked(data, key)) .map(From::from) @@ -746,7 +754,8 @@ where // better than using `FaceView::triangulate` until triangulation // is reworked. let keys = self - .as_storage_of::>() + .core + .faces .iter() .map(|(key, _)| key) .collect::>(); @@ -890,7 +899,8 @@ where /// ``` pub fn disjoint_subgraph_vertices(&self) -> impl ExactSizeIterator> { let keys = self - .as_storage_of::>() + .core + .vertices .iter() .map(|(key, _)| key) .collect::>(); @@ -909,6 +919,15 @@ where unimplemented!() } + /// Shrinks the capacity of the graph's underlying storage as much as + /// possible. + pub fn shrink_to_fit(&mut self) { + self.core.vertices.shrink_to_fit(); + self.core.arcs.shrink_to_fit(); + self.core.edges.shrink_to_fit(); + self.core.faces.shrink_to_fit(); + } + /// Creates a [`Buildable`] mesh data structure from the graph. /// /// The output is created from each unique vertex in the graph. No face data