From 5e70b5cfd6031c589fe1f966c8efd1583e91e476 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Mon, 28 Jun 2021 16:04:36 -0600 Subject: [PATCH 1/2] Add function to estimate dynamic memory usage of frontiers. Also clean up Clippy complaints. --- src/bridgetree.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/bridgetree.rs b/src/bridgetree.rs index 722329c..87c1760 100644 --- a/src/bridgetree.rs +++ b/src/bridgetree.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fmt::Debug; use std::hash::Hash; +use std::mem::size_of; use super::{Altitude, Hashable, Recording, Tree}; @@ -301,6 +302,19 @@ impl Frontier { pub fn position(&self) -> Option { self.frontier.as_ref().map(|f| f.position) } + + pub fn dynamic_memory_usage(&self) -> usize { + size_of::>>() + + self.frontier.as_ref().map_or(0, |f| { + 2 * size_of::() + f.parents.capacity() * size_of::() + }) + } +} + +impl Default for Frontier { + fn default() -> Self { + Self::new() + } } impl crate::Frontier for Frontier { @@ -1167,8 +1181,8 @@ mod tests { t.witness(); t.append(&"b".to_string()); t.append(&"c".to_string()); - assert_eq!(t.rewind(), false); - assert_eq!(t.drop_oldest_checkpoint(), true); + assert!(!t.rewind()); + assert!(t.drop_oldest_checkpoint()); } #[test] @@ -1179,7 +1193,7 @@ mod tests { t.checkpoint(); t.append(&"c".to_string()); t.witness(); - assert_eq!(t.rewind(), false); + assert!(!t.rewind()); let mut t = BridgeTree::::new(100); t.append(&"a".to_string()); @@ -1187,7 +1201,7 @@ mod tests { t.checkpoint(); t.witness(); t.witness(); - assert_eq!(t.rewind(), true); + assert!(t.rewind()); } #[test] From 530c94a40f04094e1fb36b2600638f714b8be78b Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Mon, 28 Jun 2021 16:16:59 -0600 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: str4d --- src/bridgetree.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/bridgetree.rs b/src/bridgetree.rs index 87c1760..49b2399 100644 --- a/src/bridgetree.rs +++ b/src/bridgetree.rs @@ -295,19 +295,23 @@ pub struct Frontier { } impl Frontier { + /// Construct a new empty frontier. pub fn new() -> Self { Frontier { frontier: None } } + /// Return the position of latest leaf appended to the frontier, + /// if the frontier is nonempty. pub fn position(&self) -> Option { self.frontier.as_ref().map(|f| f.position) } + /// Return the amount of memory dynamically allocated for parent + /// values within the frontier. pub fn dynamic_memory_usage(&self) -> usize { - size_of::>>() - + self.frontier.as_ref().map_or(0, |f| { - 2 * size_of::() + f.parents.capacity() * size_of::() - }) + self.frontier.as_ref().map_or(0, |f| { + 2 * size_of::() + f.parents.capacity() * size_of::>() + }) } }