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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// 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)
//
#include "td/utils/benchmark.h"
#include "td/utils/common.h"
#include "td/utils/ConcurrentHashTable.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/misc.h"
#include "td/utils/port/Mutex.h"
#include "td/utils/port/thread.h"
#include "td/utils/SpinLock.h"
#include "td/utils/tests.h"
#include <atomic>
#if !TD_THREAD_UNSUPPORTED
#if TD_HAVE_ABSL
#include <absl/container/flat_hash_map.h>
#else
#include <unordered_map>
#endif
#if TD_WITH_LIBCUCKOO
#include <third-party/libcuckoo/libcuckoo/cuckoohash_map.hh>
#endif
#if TD_WITH_JUNCTION
#include <junction/ConcurrentMap_Grampa.h>
#include <junction/ConcurrentMap_Leapfrog.h>
#include <junction/ConcurrentMap_Linear.h>
#endif
// Non resizable HashMap. Just an example
template <class KeyT, class ValueT>
class ArrayHashMap {
public:
explicit ArrayHashMap(std::size_t n) : array_(n) {
}
static td::string get_name() {
return "ArrayHashMap";
}
KeyT empty_key() const {
return KeyT{};
}
void insert(KeyT key, ValueT value) {
array_.with_value(key, true, [&](auto &node_value) { node_value.store(value, std::memory_order_release); });
}
ValueT find(KeyT key, ValueT value) {
array_.with_value(key, false, [&](auto &node_value) { value = node_value.load(std::memory_order_acquire); });
return value;
}
private:
td::AtomicHashArray<KeyT, std::atomic<ValueT>> array_;
};
template <class KeyT, class ValueT>
class ConcurrentHashMapMutex {
public:
explicit ConcurrentHashMapMutex(std::size_t) {
}
static td::string get_name() {
return "ConcurrentHashMapMutex";
}
void insert(KeyT key, ValueT value) {
auto guard = mutex_.lock();
hash_map_.emplace(key, value);
}
ValueT find(KeyT key, ValueT default_value) {
auto guard = mutex_.lock();
auto it = hash_map_.find(key);
if (it == hash_map_.end()) {
return default_value;
}
return it->second;
}
private:
td::Mutex mutex_;
#if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else
std::unordered_map<KeyT, ValueT, td::Hash<KeyT>> hash_map_;
#endif
};
template <class KeyT, class ValueT>
class ConcurrentHashMapSpinlock {
public:
explicit ConcurrentHashMapSpinlock(size_t) {
}
static td::string get_name() {
return "ConcurrentHashMapSpinlock";
}
void insert(KeyT key, ValueT value) {
auto guard = spinlock_.lock();
hash_map_.emplace(key, value);
}
ValueT find(KeyT key, ValueT default_value) {
auto guard = spinlock_.lock();
auto it = hash_map_.find(key);
if (it == hash_map_.end()) {
return default_value;
}
return it->second;
}
private:
td::SpinLock spinlock_;
#if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else
std::unordered_map<KeyT, ValueT, td::Hash<KeyT>> hash_map_;
#endif
};
#if TD_WITH_LIBCUCKOO
template <class KeyT, class ValueT>
class ConcurrentHashMapLibcuckoo {
public:
explicit ConcurrentHashMapLibcuckoo(size_t) {
}
static td::string get_name() {
return "ConcurrentHashMapLibcuckoo";
}
void insert(KeyT key, ValueT value) {
hash_map_.insert(key, value);
}
ValueT find(KeyT key, ValueT default_value) {
hash_map_.find(key, default_value);
return default_value;
}
private:
cuckoohash_map<KeyT, ValueT> hash_map_;
};
#endif
#if TD_WITH_JUNCTION
template <class KeyT, class ValueT>
class ConcurrentHashMapJunction {
public:
explicit ConcurrentHashMapJunction(std::size_t size) : hash_map_() {
}
static td::string get_name() {
return "ConcurrentHashMapJunction";
}
void insert(KeyT key, ValueT value) {
hash_map_.assign(key, value);
}
ValueT find(KeyT key, ValueT default_value) {
return hash_map_.get(key);
}
ConcurrentHashMapJunction(const ConcurrentHashMapJunction &) = delete;
ConcurrentHashMapJunction &operator=(const ConcurrentHashMapJunction &) = delete;
ConcurrentHashMapJunction(ConcurrentHashMapJunction &&) = delete;
ConcurrentHashMapJunction &operator=(ConcurrentHashMapJunction &&) = delete;
~ConcurrentHashMapJunction() {
junction::DefaultQSBR.flush();
}
private:
junction::ConcurrentMap_Leapfrog<KeyT, ValueT> hash_map_;
};
#endif
template <class HashMap>
class HashMapBenchmark final : public td::Benchmark {
struct Query {
int key;
int value;
};
td::vector<Query> queries;
td::unique_ptr<HashMap> hash_map;
std::size_t threads_n = 16;
static constexpr std::size_t MUL = 7273; //1000000000 + 7;
int n_ = 0;
public:
explicit HashMapBenchmark(std::size_t threads_n) : threads_n(threads_n) {
}
td::string get_description() const final {
return HashMap::get_name();
}
void start_up_n(int n) final {
n *= static_cast<int>(threads_n);
n_ = n;
hash_map = td::make_unique<HashMap>(n * 2);
}
void run(int n) final {
n = n_;
for (int count = 0; count < 1000; count++) {
td::vector<td::thread> threads;
for (std::size_t i = 0; i < threads_n; i++) {
std::size_t l = n * i / threads_n;
std::size_t r = n * (i + 1) / threads_n;
threads.emplace_back([l, r, this] {
for (size_t i = l; i < r; i++) {
auto x = td::narrow_cast<int>((i + 1) * MUL % n_) + 3;
auto y = td::narrow_cast<int>(i + 2);
hash_map->insert(x, y);
}
});
}
for (auto &thread : threads) {
thread.join();
}
}
}
void tear_down() final {
for (int i = 0; i < n_; i++) {
auto x = td::narrow_cast<int>((i + 1) * MUL % n_) + 3;
auto y = td::narrow_cast<int>(i + 2);
ASSERT_EQ(y, hash_map->find(x, -1));
}
queries.clear();
hash_map.reset();
}
};
template <class HashMap>
static void bench_hash_map() {
td::bench(HashMapBenchmark<HashMap>(16));
td::bench(HashMapBenchmark<HashMap>(1));
}
TEST(ConcurrentHashMap, Benchmark) {
bench_hash_map<td::ConcurrentHashMap<td::int32, td::int32>>();
bench_hash_map<ArrayHashMap<td::int32, td::int32>>();
bench_hash_map<ConcurrentHashMapSpinlock<td::int32, td::int32>>();
bench_hash_map<ConcurrentHashMapMutex<td::int32, td::int32>>();
#if TD_WITH_LIBCUCKOO
bench_hash_map<ConcurrentHashMapLibcuckoo<td::int32, td::int32>>();
#endif
#if TD_WITH_JUNCTION
bench_hash_map<ConcurrentHashMapJunction<td::int32, td::int32>>();
#endif
}
#endif
|