1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/HashTableUtils.h"
#include <functional>
namespace td {
template <class KeyT, class ValueT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
class WaitFreeHashMap {
static constexpr size_t MAX_STORAGE_COUNT = 1 << 8;
static_assert((MAX_STORAGE_COUNT & (MAX_STORAGE_COUNT - 1)) == 0);
static constexpr uint32 DEFAULT_STORAGE_SIZE = 1 << 12;
FlatHashMap<KeyT, ValueT, HashT, EqT> default_map_;
struct WaitFreeStorage {
WaitFreeHashMap maps_[MAX_STORAGE_COUNT];
};
unique_ptr<WaitFreeStorage> wait_free_storage_;
uint32 hash_mult_ = 1;
uint32 max_storage_size_ = DEFAULT_STORAGE_SIZE;
uint32 get_wait_free_index(const KeyT &key) const {
return randomize_hash(HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1);
}
WaitFreeHashMap &get_wait_free_storage(const KeyT &key) {
return wait_free_storage_->maps_[get_wait_free_index(key)];
}
const WaitFreeHashMap &get_wait_free_storage(const KeyT &key) const {
return wait_free_storage_->maps_[get_wait_free_index(key)];
}
void split_storage() {
CHECK(wait_free_storage_ == nullptr);
wait_free_storage_ = make_unique<WaitFreeStorage>();
uint32 next_hash_mult = hash_mult_ * 1000000007;
for (uint32 i = 0; i < MAX_STORAGE_COUNT; i++) {
auto &map = wait_free_storage_->maps_[i];
map.hash_mult_ = next_hash_mult;
map.max_storage_size_ = DEFAULT_STORAGE_SIZE + i * next_hash_mult % DEFAULT_STORAGE_SIZE;
}
for (auto &it : default_map_) {
get_wait_free_storage(it.first).set(it.first, std::move(it.second));
}
default_map_.clear();
}
public:
void set(const KeyT &key, ValueT value) {
if (wait_free_storage_ != nullptr) {
return get_wait_free_storage(key).set(key, std::move(value));
}
default_map_[key] = std::move(value);
if (default_map_.size() == max_storage_size_) {
split_storage();
}
}
ValueT get(const KeyT &key) const {
if (wait_free_storage_ != nullptr) {
return get_wait_free_storage(key).get(key);
}
auto it = default_map_.find(key);
if (it == default_map_.end()) {
return {};
}
return it->second;
}
size_t count(const KeyT &key) const {
if (wait_free_storage_ != nullptr) {
return get_wait_free_storage(key).count(key);
}
return default_map_.count(key);
}
// specialization for WaitFreeHashMap<..., unique_ptr<T>>
template <class T = ValueT>
typename T::element_type *get_pointer(const KeyT &key) {
if (wait_free_storage_ != nullptr) {
return get_wait_free_storage(key).get_pointer(key);
}
auto it = default_map_.find(key);
if (it == default_map_.end()) {
return nullptr;
}
return it->second.get();
}
template <class T = ValueT>
const typename T::element_type *get_pointer(const KeyT &key) const {
if (wait_free_storage_ != nullptr) {
return get_wait_free_storage(key).get_pointer(key);
}
auto it = default_map_.find(key);
if (it == default_map_.end()) {
return nullptr;
}
return it->second.get();
}
ValueT &operator[](const KeyT &key) {
if (wait_free_storage_ == nullptr) {
ValueT &result = default_map_[key];
if (default_map_.size() != max_storage_size_) {
return result;
}
split_storage();
}
return get_wait_free_storage(key)[key];
}
size_t erase(const KeyT &key) {
if (wait_free_storage_ != nullptr) {
return get_wait_free_storage(key).erase(key);
}
return default_map_.erase(key);
}
void foreach(const std::function<void(const KeyT &key, ValueT &value)> &callback) {
if (wait_free_storage_ == nullptr) {
for (auto &it : default_map_) {
callback(it.first, it.second);
}
return;
}
for (auto &it : wait_free_storage_->maps_) {
it.foreach(callback);
}
}
void foreach(const std::function<void(const KeyT &key, const ValueT &value)> &callback) const {
if (wait_free_storage_ == nullptr) {
for (auto &it : default_map_) {
callback(it.first, it.second);
}
return;
}
for (auto &it : wait_free_storage_->maps_) {
it.foreach(callback);
}
}
template <class F>
bool remove_if(const F &f) {
if (wait_free_storage_ == nullptr) {
return default_map_.remove_if(f);
}
bool is_removed = false;
for (auto &it : wait_free_storage_->maps_) {
if (it.remove_if(f)) {
is_removed = true;
}
}
return is_removed;
}
size_t calc_size() const {
if (wait_free_storage_ == nullptr) {
return default_map_.size();
}
size_t result = 0;
for (size_t i = 0; i < MAX_STORAGE_COUNT; i++) {
result += wait_free_storage_->maps_[i].calc_size();
}
return result;
}
bool empty() const {
if (wait_free_storage_ == nullptr) {
return default_map_.empty();
}
for (size_t i = 0; i < MAX_STORAGE_COUNT; i++) {
if (!wait_free_storage_->maps_[i].empty()) {
return false;
}
}
return true;
}
};
} // namespace td
|