From 926efb6fc8c5eff915201bb01fe7f8435e4efc2f Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 16 Dec 2015 17:12:20 +0100 Subject: [PATCH] docs, tests and bugfixes for squeeze --- src/squeeze.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/squeeze.rs b/src/squeeze.rs index ddec2a3a9..e81a13793 100644 --- a/src/squeeze.rs +++ b/src/squeeze.rs @@ -1,3 +1,35 @@ +//! Helper module that should be used to randomly squeeze +//! caches to a given size in bytes +//! +//! ``` +//! extern crate heapsize; +//! extern crate ethcore_util as util; +//! use std::collections::HashMap; +//! use std::mem::size_of; +//! use heapsize::HeapSizeOf; +//! use util::squeeze::Squeeze; +//! +//! fn main() { +//! let initial_size = 60; +//! let mut map: HashMap = HashMap::with_capacity(initial_size); +//! assert!(map.capacity() >= initial_size); +//! for i in 0..initial_size { +//! map.insert(i as u8, i as u8); +//! } +//! +//! assert_eq!(map.heap_size_of_children(), map.capacity() * 2 * size_of::()); +//! assert_eq!(map.len(), initial_size); +//! let initial_heap_size = map.heap_size_of_children(); +//! +//! // squeeze it to size of key and value +//! map.squeeze(2 * size_of::()); +//! assert_eq!(map.len(), 1); +//! +//! // its likely that heap size was reduced, but we can't be 100% sure +//! assert!(initial_heap_size >= map.heap_size_of_children()); +//! } +//! ``` + use std::collections::HashMap; use std::hash::Hash; use heapsize::HeapSizeOf; @@ -14,9 +46,10 @@ impl Squeeze for HashMap where K: Eq + Hash + Clone + HeapSizeOf, T: } let size_of_entry = self.heap_size_of_children() / self.capacity(); - let mut shrinked_size = size_of_entry * self.len(); + let all_entries = size_of_entry * self.len(); + let mut shrinked_size = all_entries; - while self.len() > 0 || shrinked_size > size { + while self.len() > 0 && shrinked_size > size { // could be optimized let key = self.keys().next().unwrap().clone(); self.remove(&key); @@ -25,9 +58,10 @@ impl Squeeze for HashMap where K: Eq + Hash + Clone + HeapSizeOf, T: self.shrink_to_fit(); - // if we havent shrinked enough, squeeze again - if self.heap_size_of_children() > size { + // if we squeezed something, but not enough, squeeze again + if all_entries != shrinked_size && self.heap_size_of_children() > size { self.squeeze(size); } } } +