Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RawTable::vacuum to clean up DELETED entries #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,18 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
}
}

/// Cleans the table to ensure the maximum usable capacity in its current allocation,
/// rehashing items that need to move around previously-deleted entries.
#[cfg(feature = "raw")]
#[cfg_attr(feature = "inline-more", inline)]
pub fn vacuum(&mut self, hasher: impl Fn(&T) -> u64) {
let full_capacity = bucket_mask_to_capacity(self.table.bucket_mask);
if self.capacity() < full_capacity {
// Rehash in-place to clean up DELETED entries.
self.rehash_in_place(hasher);
}
}

/// Ensures that at least `additional` items can be inserted into the table
/// without reallocation.
#[cfg_attr(feature = "inline-more", inline)]
Expand Down Expand Up @@ -2259,4 +2271,33 @@ mod test_map {
assert!(table.find(i + 100, |x| *x == i + 100).is_none());
}
}

#[cfg(feature = "raw")]
#[test]
fn vacuum() {
let mut table = RawTable::with_capacity(100);
let full_capacity = table.capacity();

let hasher = |i: &u64| *i;
for i in 0..100 {
table.insert(i, i, hasher);
}
assert_eq!(table.capacity(), full_capacity);

// Remove items to get DELETED entries, so we lose capacity.
for i in 0..50 {
table.remove_entry(i, |&x| x == i);
}
assert!(table.capacity() < full_capacity);

// Vacuuming should recover the full capacity.
table.vacuum(hasher);
assert_eq!(table.capacity(), full_capacity);

// Make sure the table still contains what we expect.
assert_eq!(table.len(), 50);
for i in 50..100 {
assert_eq!(table.get(i, |&x| x == i), Some(&i));
}
}
}