Skip to content

Commit

Permalink
Add calling vacuum to shrink_to and shrink_to_fit
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Brian Caswell committed Jul 15, 2024
1 parent ce656cf commit e69e4fa
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,18 @@ impl<K: PartialEq + Eq + Hash, V> ExpiringMap<K, V> {
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);
}
}
Expand Down Expand Up @@ -278,6 +280,18 @@ impl<K: PartialEq + Eq + Hash> ExpiringSet<K> {
.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<K: PartialEq + Eq + Hash, V> Default for ExpiringMap<K, V> {
Expand Down

0 comments on commit e69e4fa

Please sign in to comment.