Skip to content

Commit

Permalink
fix size()
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Oct 14, 2024
1 parent 12e1856 commit 41c251c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions include/ylt/util/map_sharded.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ class map_sharded_t {
.try_emplace_with_op(key, std::forward<Op>(func),
std::forward<Args>(args)...);
if (ret.second) {
size_.fetch_add(1, std::memory_order_relaxed);
size_.fetch_add(1);
}
return ret;
}

size_t size() const { // this value is approx
auto val = size_.load(std::memory_order_relaxed);
if (val < 0) { // may happen when insert & deleted frequently
int64_t val = size_.load();
if (val < 0) [[unlikely]] { // may happen when insert & deleted frequently
val = 0;
}
return val;
Expand All @@ -153,7 +153,7 @@ class map_sharded_t {
size_t erase(const key_type& key) {
auto result = get_sharded(Hash{}(key)).erase(key);
if (result) {
size_.fetch_sub(result, std::memory_order_relaxed);
size_.fetch_sub(result);
}
return result;
}
Expand All @@ -164,7 +164,7 @@ class map_sharded_t {
for (auto& map : shards_) {
auto result = map.erase_if(std::forward<Func>(op));
total += result;
size_.fetch_sub(result, std::memory_order_relaxed);
size_.fetch_sub(result);
}
return total;
}
Expand All @@ -176,7 +176,7 @@ class map_sharded_t {
auto result = map.erase_if(std::forward<Func>(op));
if (result) {
total += result;
size_.fetch_sub(result, std::memory_order_relaxed);
size_.fetch_sub(result);
break;
}
}
Expand Down

0 comments on commit 41c251c

Please sign in to comment.