From e69e4faf94d2dbcdc46bd8bbe778682dcb325028 Mon Sep 17 00:00:00 2001 From: Brian Caswell Date: Mon, 15 Jul 2024 11:50:36 -0400 Subject: [PATCH] Add calling `vacuum` to shrink_to and shrink_to_fit HashMap::shrink_to_fit states "Shrinks the capacity of the map as much as possible.". Often, this is called to free up unused memory allocations. By calling vacuum here, this aligns with the intent This also adds shrink_to and shrink_to_fit to ExpiringSet --- src/lib.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f252ccc..dcbf060 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -225,16 +225,18 @@ impl ExpiringMap { self.inner.reserve(addtional); } - /// Shrink the internal map to the minimum allowable size - /// in accordance with the resize policy + /// Remove all of the expired entries and shrink the map to the minimum + /// allowable size in accordance with the resize policy pub fn shrink_to_fit(&mut self) { + self.vacuum(); self.inner.shrink_to_fit(); } - /// Shrink the internal map to the minimum of the - /// minimum allowable size and the `min_capacity` - /// in accordance with the resize policy + /// Remove all of the expired entries and shrink the map to the minimum of + /// the minimum allowable size and the `min_capacity` in accordance with the + /// resize policy pub fn shrink_to(&mut self, min_capacity: usize) { + self.vacuum(); self.inner.shrink_to(min_capacity); } } @@ -278,6 +280,18 @@ impl ExpiringSet { .filter(|(_, v)| v.not_expired()) .map(|v| v.0) } + + /// Shrink the set to the minimum allowable size in accordance with the + /// resize policy + pub fn shrink_to_fit(&mut self) { + self.0.shrink_to_fit(); + } + + /// Shrink the set to the minimum of the minimum allowable size and the + /// `min_capacity` in accordance with the resize policy + pub fn shrink_to(&mut self, min_capacity: usize) { + self.0.shrink_to(min_capacity); + } } impl Default for ExpiringMap {