2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <folly/AtomicHashArray.h>
22 #include <folly/Conv.h>
23 #include <folly/Memory.h>
24 #include <folly/hash/Hash.h>
25 #include <folly/portability/GTest.h>
26 #include <folly/portability/SysMman.h>
29 using namespace folly;
36 typedef const T* const_pointer;
38 typedef const T& const_reference;
40 typedef ptrdiff_t difference_type;
41 typedef size_t size_type;
43 T* address(T& x) const {
44 return std::addressof(x);
47 const T* address(const T& x) const {
48 return std::addressof(x);
51 size_t max_size() const {
52 return std::numeric_limits<size_t>::max();
55 template <class U> struct rebind {
56 typedef MmapAllocator<U> other;
59 bool operator!=(const MmapAllocator<T>& other) const {
60 return !(*this == other);
63 bool operator==(const MmapAllocator<T>& /* other */) const { return true; }
65 template <class... Args>
66 void construct(T* p, Args&&... args) {
67 new (p) T(std::forward<Args>(args)...);
74 T *allocate(size_t n) {
75 void *p = mmap(nullptr, n * sizeof(T), PROT_READ | PROT_WRITE,
76 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
77 if (p == MAP_FAILED) throw std::bad_alloc();
81 void deallocate(T *p, size_t n) {
82 munmap(p, n * sizeof(T));
86 template <class KeyT, class ValueT>
87 pair<KeyT,ValueT> createEntry(int i) {
88 return pair<KeyT,ValueT>(to<KeyT>(folly::hash::jenkins_rev_mix32(i) % 1000),
95 class Allocator = std::allocator<char>,
96 class ProbeFcn = AtomicHashArrayLinearProbeFcn>
98 typedef AtomicHashArray<KeyT, ValueT, std::hash<KeyT>,
99 std::equal_to<KeyT>, Allocator, ProbeFcn> MyArr;
100 auto arr = MyArr::create(150);
101 map<KeyT, ValueT> ref;
102 for (int i = 0; i < 100; ++i) {
103 auto e = createEntry<KeyT, ValueT>(i);
104 auto ret = arr->insert(e);
105 EXPECT_EQ(!ref.count(e.first), ret.second); // succeed iff not in ref
107 EXPECT_EQ(ref.size(), arr->size());
108 if (ret.first == arr->end()) {
109 EXPECT_FALSE("AHA should not have run out of space.");
112 EXPECT_EQ(e.first, ret.first->first);
113 EXPECT_EQ(ref.find(e.first)->second, ret.first->second);
116 for (int i = 125; i > 0; i -= 10) {
117 auto e = createEntry<KeyT, ValueT>(i);
118 auto ret = arr->erase(e.first);
119 auto refRet = ref.erase(e.first);
120 EXPECT_EQ(ref.size(), arr->size());
121 EXPECT_EQ(refRet, ret);
124 for (int i = 155; i > 0; i -= 10) {
125 auto e = createEntry<KeyT, ValueT>(i);
126 auto ret = arr->insert(e);
127 auto refRet = ref.insert(e);
128 EXPECT_EQ(ref.size(), arr->size());
129 EXPECT_EQ(*refRet.first, *ret.first);
130 EXPECT_EQ(refRet.second, ret.second);
133 for (const auto& e : ref) {
134 auto ret = arr->find(e.first);
135 if (ret == arr->end()) {
136 EXPECT_FALSE("Key was not in AHA");
139 EXPECT_EQ(e.first, ret->first);
140 EXPECT_EQ(e.second, ret->second);
147 class Allocator = std::allocator<char>,
148 class ProbeFcn = AtomicHashArrayLinearProbeFcn>
149 void testNoncopyableMap() {
150 typedef AtomicHashArray<KeyT, std::unique_ptr<ValueT>, std::hash<KeyT>,
151 std::equal_to<KeyT>, Allocator, ProbeFcn> MyArr;
153 auto arr = MyArr::create(250);
154 for (int i = 0; i < 100; i++) {
155 arr->insert(make_pair(i,std::unique_ptr<ValueT>(new ValueT(i))));
157 for (int i = 100; i < 150; i++) {
158 arr->emplace(i,new ValueT(i));
160 for (int i = 150; i < 200; i++) {
161 arr->emplace(i,new ValueT(i),std::default_delete<ValueT>());
163 for (int i = 0; i < 200; i++) {
164 auto ret = arr->find(i);
165 EXPECT_EQ(*(ret->second), i);
170 TEST(Aha, InsertErase_i32_i32) {
171 testMap<int32_t, int32_t>();
172 testMap<int32_t, int32_t, MmapAllocator<char>>();
173 testMap<int32_t, int32_t,
174 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
175 testMap<int32_t, int32_t,
176 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
177 testNoncopyableMap<int32_t, int32_t>();
178 testNoncopyableMap<int32_t, int32_t, MmapAllocator<char>>();
179 testNoncopyableMap<int32_t, int32_t,
180 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
181 testNoncopyableMap<int32_t, int32_t,
182 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
184 TEST(Aha, InsertErase_i64_i32) {
185 testMap<int64_t, int32_t>();
186 testMap<int64_t, int32_t, MmapAllocator<char>>();
187 testMap<int64_t, int32_t,
188 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
189 testMap<int64_t, int32_t,
190 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
191 testNoncopyableMap<int64_t, int32_t>();
192 testNoncopyableMap<int64_t, int32_t, MmapAllocator<char>>();
193 testNoncopyableMap<int64_t, int32_t,
194 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
195 testNoncopyableMap<int64_t, int32_t,
196 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
198 TEST(Aha, InsertErase_i64_i64) {
199 testMap<int64_t, int64_t>();
200 testMap<int64_t, int64_t, MmapAllocator<char>>();
201 testMap<int64_t, int64_t,
202 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
203 testMap<int64_t, int64_t,
204 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
205 testNoncopyableMap<int64_t, int64_t>();
206 testNoncopyableMap<int64_t, int64_t, MmapAllocator<char>>();
207 testNoncopyableMap<int64_t, int64_t,
208 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
209 testNoncopyableMap<int64_t, int64_t,
210 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
212 TEST(Aha, InsertErase_i32_i64) {
213 testMap<int32_t, int64_t>();
214 testMap<int32_t, int64_t, MmapAllocator<char>>();
215 testMap<int32_t, int64_t,
216 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
217 testMap<int32_t, int64_t,
218 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
219 testNoncopyableMap<int32_t, int64_t>();
220 testNoncopyableMap<int32_t, int64_t, MmapAllocator<char>>();
221 testNoncopyableMap<int32_t, int64_t,
222 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
223 testNoncopyableMap<int32_t, int64_t,
224 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
226 TEST(Aha, InsertErase_i32_str) {
227 testMap<int32_t, string>();
228 testMap<int32_t, string, MmapAllocator<char>>();
229 testMap<int32_t, string,
230 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
231 testMap<int32_t, string,
232 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
234 TEST(Aha, InsertErase_i64_str) {
235 testMap<int64_t, string>();
236 testMap<int64_t, string, MmapAllocator<char>>();
237 testMap<int64_t, string,
238 std::allocator<char>, AtomicHashArrayQuadraticProbeFcn>();
239 testMap<int64_t, string,
240 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn>();
243 TEST(Aha, Create_cstr_i64) {
244 auto obj = AtomicHashArray<const char*, int64_t>::create(12);
247 static bool legalKey(char* a);
249 // Support two additional key lookup types (char and StringPiece) using
250 // one set of traits.
252 bool operator()(char* a, char* b) {
253 return legalKey(a) && (strcmp(a, b) == 0);
255 bool operator()(char* a, const char& b) {
256 return legalKey(a) && (a[0] != '\0') && (a[0] == b);
258 bool operator()(char* a, const StringPiece b) {
259 return legalKey(a) &&
260 (strlen(a) == b.size()) && (strncmp(a, b.begin(), b.size()) == 0);
265 size_t operator()(char* a) {
267 while (a[0] != 0) result += static_cast<size_t>(*(a++));
270 size_t operator()(const char& a) {
271 return static_cast<size_t>(a);
273 size_t operator()(const StringPiece a) {
275 for (const auto& ch : a) result += static_cast<size_t>(ch);
280 // Creates malloc'ed null-terminated strings.
281 struct KeyConvertTraits {
282 char* operator()(const char& a) {
283 return strndup(&a, 1);
285 char* operator()(const StringPiece a) {
286 return strndup(a.begin(), a.size());
290 typedef AtomicHashArray<char*, int64_t, HashTraits, EqTraits,
291 MmapAllocator<char>, AtomicHashArrayQuadraticProbeFcn,
294 AHACstrInt::Config cstrIntCfg;
296 static bool legalKey(char* a) {
297 return a != cstrIntCfg.emptyKey &&
298 a != cstrIntCfg.lockedKey &&
299 a != cstrIntCfg.erasedKey;
302 TEST(Aha, LookupAny) {
303 auto arr = AHACstrInt::create(12);
305 char* f_char = strdup("f");
306 SCOPE_EXIT { free(f_char); };
307 arr->insert(std::make_pair(f_char, 42));
309 EXPECT_EQ(42, arr->find("f")->second);
311 // Look up a single char, successfully.
312 auto it = arr->find('f');
313 EXPECT_EQ(42, it->second);
316 // Look up a single char, unsuccessfully.
317 auto it = arr->find('g');
318 EXPECT_TRUE(it == arr->end());
321 // Insert a new char key.
322 auto res = arr->emplace('h', static_cast<int64_t>(123));
323 EXPECT_TRUE(res.second);
324 EXPECT_TRUE(res.first != arr->end());
325 // Look up the string version.
326 EXPECT_EQ(123, arr->find("h")->second);
329 // Fail to emplace an existing key.
330 auto res = arr->emplace('f', static_cast<int64_t>(123));
331 EXPECT_FALSE(res.second);
332 EXPECT_TRUE(res.first != arr->end());
335 for (auto it : *arr) {
340 using AHAIntCInt = AtomicHashArray<int64_t, const int32_t>;
342 TEST(Aha, ConstValue) {
343 auto aha = AHAIntCInt::create(10);