Dead shift in ConcurrentHashMapSegment
authorPhil Willoughby <philwill@fb.com>
Mon, 11 Sep 2017 19:20:45 +0000 (12:20 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 11 Sep 2017 19:40:26 +0000 (12:40 -0700)
Summary: Original problem detected by compiling with `-Werror,-Wunused-value`. On further inspection the only place which uses this detail class ensures that the `max_size` parameter is a power of two already, so we can discard the logic to manipulate `max_size` and put a `CHECK` clause in its place to guard against future changes to the caller that break this assumption.

Reviewed By: yfeldblum

Differential Revision: D5799110

fbshipit-source-id: d21ed9ff196d54ef91e38254df8b1b88bbf29275

folly/concurrency/ConcurrentHashMap.h
folly/concurrency/detail/ConcurrentHashMap-detail.h

index 0bc4d16b9bb695a4320cc3038dd615fcfb042d87..f261ac98f4039e755dbb93fdce461460ca30975e 100644 (file)
@@ -467,7 +467,7 @@ class ConcurrentHashMap {
     auto seg = segments_[i].load(std::memory_order_acquire);
     if (!seg) {
       auto newseg = (SegmentT*)Allocator().allocate(sizeof(SegmentT));
-      new (newseg)
+      newseg = new (newseg)
           SegmentT(size_ >> ShardBits, load_factor_, max_size_ >> ShardBits);
       if (!segments_[i].compare_exchange_strong(seg, newseg)) {
         // seg is updated with new value, delete ours.
index ccef419d9fbf258e532656df83bb419420a384ea..7b9da9c0204e347f595c2b4ccdd4e496fb162375 100644 (file)
@@ -215,17 +215,13 @@ class FOLLY_ALIGNED(64) ConcurrentHashMapSegment {
       size_t initial_buckets,
       float load_factor,
       size_t max_size)
-      : load_factor_(load_factor) {
+      : load_factor_(load_factor), max_size_(max_size) {
     auto buckets = (Buckets*)Allocator().allocate(sizeof(Buckets));
     initial_buckets = folly::nextPowTwo(initial_buckets);
-    if (max_size != 0) {
-      max_size_ = folly::nextPowTwo(max_size);
-    }
-    if (max_size_ > max_size) {
-      max_size_ >> 1;
-    }
-
-    CHECK(max_size_ == 0 || (folly::popcount(max_size_ - 1) + ShardBits <= 32));
+    DCHECK(
+        max_size_ == 0 ||
+        (isPowTwo(max_size_) &&
+         (folly::popcount(max_size_ - 1) + ShardBits <= 32)));
     new (buckets) Buckets(initial_buckets);
     buckets_.store(buckets, std::memory_order_release);
     load_factor_nodes_ = initial_buckets * load_factor_;
@@ -734,7 +730,7 @@ class FOLLY_ALIGNED(64) ConcurrentHashMapSegment {
   float load_factor_;
   size_t load_factor_nodes_;
   size_t size_{0};
-  size_t max_size_{0};
+  size_t const max_size_;
   Atom<Buckets*> buckets_{nullptr};
   Mutex m_;
 };