Commit 755145b6 authored by Maged Michael's avatar Maged Michael Committed by Facebook GitHub Bot

SingleWriterFixedHashMap: Clean up tombstones when copying from a full map

Summary:
Clean up tombstones when copying from a full map.
Equal capacity maps are copied with tombstones for speed but that optimization should not be used if it results in a full map.

Add guarantee that a copy from a map with tombstones will have at least one empty element.

Reviewed By: A5he

Differential Revision: D25230678

fbshipit-source-id: 900b734cc526be8da8be4e70137d19645f994f19
parent 1213fc18
...@@ -48,6 +48,9 @@ namespace folly { ...@@ -48,6 +48,9 @@ namespace folly {
/// - used() /// - used()
/// - available() /// - available()
/// ///
/// This implementation guarantees that a copy from a map with
/// tombstones will have at least one available empty element.
///
template <typename Key, typename Value> template <typename Key, typename Value>
class SingleWriterFixedHashMap { class SingleWriterFixedHashMap {
#if __cpp_lib_atomic_is_always_lock_free #if __cpp_lib_atomic_is_always_lock_free
...@@ -86,7 +89,8 @@ class SingleWriterFixedHashMap { ...@@ -86,7 +89,8 @@ class SingleWriterFixedHashMap {
return; return;
} }
elem_ = std::make_unique<Elem[]>(capacity_); elem_ = std::make_unique<Elem[]>(capacity_);
if (capacity_ == o.capacity_) { if (capacity_ == o.capacity_ &&
(o.used_ < o.capacity_ || o.size() == o.capacity_)) {
memcpy(elem_.get(), o.elem_.get(), capacity_ * sizeof(Elem)); memcpy(elem_.get(), o.elem_.get(), capacity_ * sizeof(Elem));
used_ = o.used_; used_ = o.used_;
setSize(o.size()); setSize(o.size());
......
...@@ -151,6 +151,23 @@ TEST(SingleWriterFixedHashMap, drf) { ...@@ -151,6 +151,23 @@ TEST(SingleWriterFixedHashMap, drf) {
drf_test(); drf_test();
} }
void copy_tombstones_test() {
SWFHM* m = new SWFHM(4);
for (int i = 0; i < 100; ++i) {
SWFHM* m2 = new SWFHM(4, *m);
delete m;
ASSERT_LT(m2->used(), m2->capacity());
m2->insert(i, i);
m2->erase(i);
m = m2;
}
delete m;
}
TEST(SingleWriterFixedHashMap, copy_tombstones) {
copy_tombstones_test();
}
// Benchmarks // Benchmarks
template <typename Func> template <typename Func>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment