Skip to content

Commit

Permalink
SingleWriterFixedHashMap: Clean up tombstones when copying from a ful…
Browse files Browse the repository at this point in the history
…l 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
  • Loading branch information
magedm authored and facebook-github-bot committed Dec 3, 2020
1 parent 1213fc1 commit 755145b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion folly/experimental/SingleWriterFixedHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace folly {
/// - used()
/// - 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>
class SingleWriterFixedHashMap {
#if __cpp_lib_atomic_is_always_lock_free
Expand Down Expand Up @@ -86,7 +89,8 @@ class SingleWriterFixedHashMap {
return;
}
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));
used_ = o.used_;
setSize(o.size());
Expand Down
17 changes: 17 additions & 0 deletions folly/experimental/test/SingleWriterHashMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ TEST(SingleWriterFixedHashMap, drf) {
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

template <typename Func>
Expand Down

0 comments on commit 755145b

Please sign in to comment.