-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBucketCuckooTest.cpp
361 lines (330 loc) · 6.68 KB
/
BucketCuckooTest.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "common.h"
#include "gtest/gtest.h"
namespace BucketCuckoo
{
struct Bucket
{
// 0 = empty, 1 = node, 2 = extending another node, 3 = slot reserved for insert
//
int exist[4];
uint64_t value[4];
Bucket() { memset(exist, 0, sizeof exist); }
bool Exist(uint64_t v)
{
rep(i, 0, 3)
{
if (exist[i] == 1 && value[i] == v)
{
return true;
}
}
return false;
}
int GetNumEmptyOrResevedForInsert()
{
int ret = 0;
rep(i, 0, 3) if (exist[i] == 0 || exist[i] == 3) ret++;
return ret;
}
int GetSize(int k)
{
int size = 1;
k++;
while (k < 4 && exist[k] == 2) k++, size++;
return size;
}
pair<uint64_t, int> ClearItemForInsert(int which)
{
int size = GetSize(which);
uint64_t v = value[which];
rep(k, which, which + size - 1)
{
exist[k] = 3;
}
return make_pair(v, size);
}
pair<uint64_t, int> PopItemForInsert(uint64_t value, int size)
{
assert(GetNumEmptyOrResevedForInsert() < size);
assert(1 <= size && size <= 3);
// size == 1: always pop smallest size item
// size == 2: if contains size = 3 item, pop it. Otherwise pop smallest size item
// size == 3: pop largest size item
//
if (size == 1)
{
int minSize = 10000;
int which = -1;
rep(i, 0, 3)
{
if (exist[i] == 1 && GetSize(i) < minSize)
{
minSize = GetSize(i);
which = i;
}
}
assert(which != -1);
return ClearItemForInsert(which);
}
if (size == 2)
{
int minSize = 10000;
int which = -1;
rep(i, 0, 3)
{
if (exist[i] == 1)
{
int x = GetSize(i);
if (x == 3)
{
return ClearItemForInsert(i);
}
if (x < minSize)
{
minSize = GetSize(i);
which = i;
}
}
}
assert(which != -1);
return ClearItemForInsert(which);
}
if (size == 3)
{
int maxSize = 0;
int which = -1;
rep(i, 0, 3)
{
if (exist[i] == 1 && GetSize(i) > maxSize)
{
maxSize = GetSize(i);
which = i;
}
}
assert(which != -1);
return ClearItemForInsert(which);
}
}
void InsertItem(uint64_t v, int size)
{
assert(GetNumEmptyOrResevedForInsert() >= size);
int c = 0;
int k = 0;
while (k < 4)
{
if (exist[k] == 1)
{
int t = GetSize(k);
exist[c] = exist[k];
value[c] = value[k];
c++;
rep(i, 1, t-1)
{
exist[c] = 2;
c++;
}
k += t;
}
else
{
k++;
}
}
assert(c+size-1 <= 3);
rep(i, c, 3)
{
exist[i] = 0;
}
exist[c] = 1;
value[c] = v;
rep(i, c+1, c+size-1)
{
exist[i] = 2;
}
}
};
namespace XXH
{
static const uint32_t XXH_SEED1 = 1192827283U;
static const uint32_t XXH_SEED2 = 534897851U;
static const uint32_t PRIME32_1 = 2654435761U;
static const uint32_t PRIME32_2 = 2246822519U;
static const uint32_t PRIME32_3 = 3266489917U;
static const uint32_t PRIME32_4 = 668265263U;
static const uint32_t PRIME32_5 = 374761393U;
#define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
uint32_t XXH32_avalanche(uint32_t h32)
{
h32 ^= h32 >> 15;
h32 *= PRIME32_2;
h32 ^= h32 >> 13;
h32 *= PRIME32_3;
h32 ^= h32 >> 16;
return h32;
}
uint32_t XXH32_CoreLogic(uint64_t key, uint32_t len, uint32_t seed, uint32_t multiplier)
{
key >>= (8-len)*8;
key <<= (8-len)*8;
uint32_t low = key;
uint32_t high = key >> 32;
uint32_t h32 = PRIME32_5 + seed + len;
h32 ^= high * multiplier;
h32 = XXH_rotl32(h32, 17) * PRIME32_4;
if (len > 4)
{
h32 ^= low * multiplier;
h32 = XXH_rotl32(h32, 17) * PRIME32_4;
}
return XXH32_avalanche(h32);
}
uint32_t HashFn1(uint64_t key, uint32_t len)
{
return XXH32_CoreLogic(key, len, XXH_SEED1, PRIME32_1);
}
uint32_t HashFn2(uint64_t key, uint32_t len)
{
return XXH32_CoreLogic(key, len, XXH_SEED2, PRIME32_3);
}
uint32_t HashFn3(uint64_t key, uint32_t len)
{
return XXH32_CoreLogic(key, len, 0 /*seed*/, PRIME32_3);
}
} // namespace XXH
const int HtSize = 8388608;
Bucket ht[HtSize];
int CuckooDisplacement(uint32_t pos, uint64_t value, int size, int round, bool& failed)
{
if (round > 1000)
{
failed = true;
return 0;
}
int sumMoves = 0;
while (ht[pos].GetNumEmptyOrResevedForInsert() < size)
{
sumMoves++;
pair<uint64_t, int> victim = ht[pos].PopItemForInsert(value, size);
uint32_t h1 = XXH::HashFn1(victim.first, 8) % HtSize;
uint32_t h2 = XXH::HashFn2(victim.first, 8) % HtSize;
if (h1 == h2) { h2 = (h2 + 1) % HtSize; }
assert(h1 == pos || h2 == pos);
if (h1 == pos) h1 = h2;
assert(h1 != pos);
sumMoves += CuckooDisplacement(h1, victim.first, victim.second, round + 1, failed);
if (failed)
{
return 0;
}
}
ht[pos].InsertItem(value, size);
return sumMoves;
}
int histogram[1010];
void Insert(uint64_t value, int size, bool& exist, bool& failed)
{
failed = false;
exist = false;
uint32_t h1 = XXH::HashFn1(value, 8) % HtSize;
uint32_t h2 = XXH::HashFn2(value, 8) % HtSize;
if (h1 == h2) { h2 = (h2 + 1) % HtSize; }
if (ht[h1].Exist(value) || ht[h2].Exist(value))
{
exist = true;
return;
}
int h1x = ht[h1].GetNumEmptyOrResevedForInsert();
int h2x = ht[h2].GetNumEmptyOrResevedForInsert();
if (h1x >= size)
{
ht[h1].InsertItem(value, size);
histogram[0]++;
return;
}
if (h2x >= size)
{
ht[h2].InsertItem(value, size);
histogram[0]++;
return;
}
if (h1x < h2x)
{
h1 = h2;
}
int numMoves = CuckooDisplacement(h1, value, size, 0, failed);
if (failed)
{
return;
}
histogram[numMoves]++;
return;
}
int numInsertions;
int numSlotsUsed;
int numEquivalentSetSize;
double prob[3] = {0.88, 0.1, 0.02};
int equivSetSize[3] = {1, 5, 20};
void GenerateAndInsertData()
{
double x = double(rand()) / RAND_MAX;
int sz;
rep(k, 0, 2)
{
if (x < prob[k] || k == 2)
{
sz = k+1;
break;
}
else
{
x -= prob[k];
}
}
uint64_t value = 0;
rep(k, 0, 7)
{
value = value * 256 + rand() % 256;
}
bool exist, failed;
Insert(value, sz, exist, failed);
if (failed)
{
printf("failed at %d slots used, lf = %.3lf%%, %d equivalent set size\n",
numSlotsUsed, double(numSlotsUsed) / HtSize / 4, numEquivalentSetSize / 2);
exit(0);
}
if (exist) return;
numInsertions++;
numSlotsUsed += sz;
numEquivalentSetSize += equivSetSize[sz-1];
}
TEST(BucketCuckooTest, InsertLimitTest)
{
numInsertions = 0;
numSlotsUsed = 0;
numEquivalentSetSize = 0;
while (1)
{
GenerateAndInsertData();
}
}
TEST(BucketCuckooTest, InsertDisplacementHistogram)
{
numSlotsUsed = 0;
numEquivalentSetSize = 0;
while (numSlotsUsed < 0.9 * HtSize * 4)
{
GenerateAndInsertData();
}
printf("num insertions = %d, num slots used = %d\n", numInsertions, numSlotsUsed);
int k = 1000;
while (histogram[k] == 0) k--;
LL sum = 0;
rep(i, 0, k)
{
printf("%d: %d\n", i, histogram[i]);
sum += LL(i) * histogram[i];
}
printf("avg = %.3lf\n", double(sum) / numInsertions);
}
} // namespace BucketCuckoo