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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
#include "compact_hash.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/generic/bitmap.h>
#include <util/generic/ylimits.h>
#include <util/generic/hash_set.h>
#include <util/generic/maybe.h>
#include <util/generic/xrange.h>
#include <util/random/shuffle.h>
using namespace NKikimr;
using namespace NKikimr::NCHash;
Y_UNIT_TEST_SUITE(TCompactHashTest) {
template <typename TItem>
void TestListPoolPagesImpl(size_t listSize, ui16 countOfLists, size_t expectedListCapacity, ui32 expectedMark) {
using TPool = TListPool<TItem>;
TAlignedPagePool pagePool(__LOCATION__);
TPool pool(pagePool);
UNIT_ASSERT(countOfLists > 1);
THashSet<TItem*> lists;
void* pageAddr = nullptr;
for (size_t i = 0; i < countOfLists / 2; ++i) {
TItem* l = pool.template GetList<TItem>(listSize);
UNIT_ASSERT_VALUES_EQUAL(expectedMark, TPool::GetMark(l));
UNIT_ASSERT_VALUES_EQUAL(listSize, TListPoolBase::GetPartListSize(l));
UNIT_ASSERT_VALUES_EQUAL(expectedListCapacity, TListPoolBase::GetListCapacity(l));
if (0 == i) {
pageAddr = TAlignedPagePool::GetPageStart(l);
} else {
// All lists are from the same page
UNIT_ASSERT_VALUES_EQUAL(pageAddr, TAlignedPagePool::GetPageStart(l));
}
UNIT_ASSERT(lists.insert(l).second);
}
// Return all lists except one
while (lists.size() > 1) {
auto it = lists.begin();
pool.ReturnList(*it);
lists.erase(it);
}
for (size_t i = 1; i < countOfLists; ++i) {
TItem* l = pool.template GetList<TItem>(listSize);
UNIT_ASSERT_VALUES_EQUAL(expectedMark, TPool::GetMark(l));
UNIT_ASSERT_VALUES_EQUAL(listSize, TListPoolBase::GetPartListSize(l));
UNIT_ASSERT_VALUES_EQUAL(expectedListCapacity, TListPoolBase::GetListCapacity(l));
UNIT_ASSERT_VALUES_EQUAL(pageAddr, TAlignedPagePool::GetPageStart(l));
UNIT_ASSERT(lists.insert(l).second);
}
for (auto l: lists) {
pool.ReturnList(l);
}
THashSet<TItem*> lists2;
for (size_t i = 0; i < countOfLists; ++i) {
TItem* l = pool.template GetList<TItem>(listSize);
// All lists are from the same page
UNIT_ASSERT_VALUES_EQUAL(pageAddr, TAlignedPagePool::GetPageStart(l));
UNIT_ASSERT(1 == lists.erase(l));
UNIT_ASSERT(lists2.insert(l).second);
}
TItem* l = pool.template GetList<TItem>(listSize); // New page
UNIT_ASSERT_VALUES_UNEQUAL(pageAddr, TAlignedPagePool::GetPageStart(l));
UNIT_ASSERT_VALUES_EQUAL(listSize, TListPoolBase::GetPartListSize(l));
UNIT_ASSERT_VALUES_EQUAL(expectedListCapacity, TListPoolBase::GetListCapacity(l));
pool.ReturnList(l);
for (auto l: lists2) {
pool.ReturnList(l);
}
}
template <typename TItem>
void TestListPoolLargeImpl() {
using TPool = TListPool<TItem>;
TAlignedPagePool pagePool(__LOCATION__);
TPool pool(pagePool);
const size_t listSize = TListPoolBase::GetMaxListSize<TItem>();
TItem* l = pool.template GetList<TItem>(listSize);
pool.template IncrementList<TItem>(l);
UNIT_ASSERT_VALUES_EQUAL((ui32)TListPoolBase::LARGE_MARK, TListPoolBase::GetMark(l));
UNIT_ASSERT_VALUES_EQUAL(listSize + 1, TListPoolBase::GetPartListSize(l));
TListPoolBase::SetPartListSize(l, TListPoolBase::GetListCapacity(l));
pool.template IncrementList<TItem>(l);
UNIT_ASSERT_VALUES_EQUAL(1, TListPoolBase::GetPartListSize(l));
UNIT_ASSERT_VALUES_EQUAL(1 + TListPoolBase::GetListCapacity(l), TListPoolBase::GetFullListSize(l));
pool.ReturnList(l);
}
Y_UNIT_TEST(TestListPoolSmallPagesByte) {
ui16 count = TListPoolBase::GetSmallPageCapacity<ui8>(2);
TestListPoolPagesImpl<ui8>(2, count, 2, TListPoolBase::SMALL_MARK);
}
Y_UNIT_TEST(TestListPoolMediumPagesByte) {
size_t listSize = TListPoolBase::MAX_SMALL_LIST_SIZE + 1;
ui16 count = TListPoolBase::GetMediumPageCapacity<ui8>(listSize);
TestListPoolPagesImpl<ui8>(listSize, count, FastClp2(listSize), TListPoolBase::MEDIUM_MARK);
}
Y_UNIT_TEST(TestListPoolLargPagesByte) {
TestListPoolLargeImpl<ui8>();
}
Y_UNIT_TEST(TestListPoolSmallPagesUi64) {
ui16 count = TListPoolBase::GetSmallPageCapacity<ui64>(2);
TestListPoolPagesImpl<ui64>(2, count, 2, TListPoolBase::SMALL_MARK);
}
Y_UNIT_TEST(TestListPoolMediumPagesUi64) {
size_t listSize = TListPoolBase::MAX_SMALL_LIST_SIZE + 1;
ui16 count = TListPoolBase::GetMediumPageCapacity<ui64>(listSize);
TestListPoolPagesImpl<ui64>(listSize, count, FastClp2(listSize), TListPoolBase::MEDIUM_MARK);
}
Y_UNIT_TEST(TestListPoolLargPagesUi64) {
TestListPoolLargeImpl<ui64>();
}
struct TItem {
ui8 A[256];
};
Y_UNIT_TEST(TestListPoolSmallPagesObj) {
ui16 count = TListPoolBase::GetSmallPageCapacity<TItem>(2);
TestListPoolPagesImpl<TItem>(2, count, 2, TListPoolBase::SMALL_MARK);
}
Y_UNIT_TEST(TestListPoolMediumPagesObj) {
size_t listSize = TListPoolBase::MAX_SMALL_LIST_SIZE + 1;
ui16 count = TListPoolBase::GetMediumPageCapacity<TItem>(listSize);
TestListPoolPagesImpl<TItem>(listSize, count, FastClp2(listSize), TListPoolBase::MEDIUM_MARK);
}
Y_UNIT_TEST(TestListPoolLargPagesObj) {
TestListPoolLargeImpl<TItem>();
}
struct TItemHash {
template <typename T>
size_t operator() (T num) const {
return num % 13;
}
};
template <typename TItem>
void TestHashImpl() {
const ui32 elementsCount = 32;
const ui64 sumKeysTarget = elementsCount * (elementsCount - 1) / 2;
const ui32 addition = 20;
const ui64 sumValuesTarget = sumKeysTarget + addition * elementsCount;
TAlignedPagePool pagePool(__LOCATION__);
TCompactHash<TItem, TItem, TItemHash> hash(pagePool);
TVector<TItem> elements(elementsCount);
std::iota(elements.begin(), elements.end(), 0);
Shuffle(elements.begin(), elements.end());
for (TItem i: elements) {
hash.Insert(i, i + addition);
}
{
decltype(hash) hash2(std::move(hash));
decltype(hash) hash3(pagePool);
hash3.Swap(hash2);
hash = hash3;
}
for (TItem i: elements) {
UNIT_ASSERT(hash.Has(i));
UNIT_ASSERT(hash.Find(i).Ok());
UNIT_ASSERT_VALUES_EQUAL(i + addition, hash.Find(i).Get().second);
}
UNIT_ASSERT(!hash.Has(elementsCount + 1));
UNIT_ASSERT(!hash.Has(elementsCount + 10));
UNIT_ASSERT_VALUES_EQUAL(elementsCount, hash.Size());
UNIT_ASSERT_VALUES_EQUAL(elementsCount, hash.UniqSize());
ui64 sumKeys = 0;
ui64 sumValues = 0;
for (auto it = hash.Iterate(); it.Ok(); ++it) {
UNIT_ASSERT_VALUES_EQUAL(it.Get().first + addition, it.Get().second);
sumKeys += it.Get().first;
sumValues += it.Get().second;
}
UNIT_ASSERT_VALUES_EQUAL(sumKeys, sumKeysTarget);
UNIT_ASSERT_VALUES_EQUAL(sumValues, sumValuesTarget);
}
template <typename TItem>
void TestMultiHashImpl() {
const ui32 keysCount = 10;
const ui64 elementsCount = keysCount * (keysCount + 1) / 2;
TAlignedPagePool pagePool(__LOCATION__);
TCompactMultiHash<TItem, TItem, TItemHash> hash(pagePool);
TVector<TItem> keys(keysCount);
std::iota(keys.begin(), keys.end(), 0);
Shuffle(keys.begin(), keys.end());
ui64 sumKeysTarget = 0;
ui64 sumValuesTarget = 0;
for (TItem k: keys) {
sumKeysTarget += k;
for (TItem i = 0; i < k + 1; ++i) {
hash.Insert(k, i);
sumValuesTarget += i;
}
}
{
decltype(hash) hash2(std::move(hash));
decltype(hash) hash3(pagePool);
hash3.Swap(hash2);
hash = hash3;
}
for (TItem k: keys) {
UNIT_ASSERT(hash.Has(k));
UNIT_ASSERT_VALUES_EQUAL(k + 1, hash.Count(k));
auto it = hash.Find(k);
UNIT_ASSERT(it.Ok());
TDynBitMap set;
for (; it.Ok(); ++it) {
UNIT_ASSERT_VALUES_EQUAL(k, it.GetKey());
UNIT_ASSERT(!set.Test(it.GetValue()));
set.Set(it.GetValue());
}
UNIT_ASSERT_VALUES_EQUAL(set.Count(), k + 1);
}
UNIT_ASSERT(!hash.Has(keysCount + 1));
UNIT_ASSERT(!hash.Has(keysCount + 10));
UNIT_ASSERT(!hash.Find(keysCount + 1).Ok());
UNIT_ASSERT_VALUES_EQUAL(elementsCount, hash.Size());
UNIT_ASSERT_VALUES_EQUAL(keysCount, hash.UniqSize());
ui64 sumKeys = 0;
ui64 sumValues = 0;
TMaybe<TItem> prevKey;
for (auto it = hash.Iterate(); it.Ok(); ++it) {
const auto key = it.GetKey();
if (prevKey != key) {
sumKeys += key;
for (auto valIt = it.MakeCurrentKeyIter(); valIt.Ok(); ++valIt) {
UNIT_ASSERT_VALUES_EQUAL(key, valIt.GetKey());
sumValues += valIt.GetValue();
}
prevKey = key;
}
}
UNIT_ASSERT_VALUES_EQUAL(sumKeys, sumKeysTarget);
UNIT_ASSERT_VALUES_EQUAL(sumValues, sumValuesTarget);
// Test large lists
TItem val = 0;
for (size_t i = 0; i < TListPoolBase::GetLargePageCapacity<TItem>(); ++i) {
hash.Insert(keysCount, val++);
}
TItem check = 0;
for (auto i = hash.Find(keysCount); i.Ok(); ++i) {
UNIT_ASSERT_VALUES_EQUAL(keysCount, i.GetKey());
UNIT_ASSERT_VALUES_EQUAL(check++, i.GetValue());
}
UNIT_ASSERT_VALUES_EQUAL(check, val);
UNIT_ASSERT_VALUES_EQUAL(TListPoolBase::GetLargePageCapacity<TItem>(), hash.Count(keysCount));
for (size_t i = 0; i < TListPoolBase::GetLargePageCapacity<TItem>() + 1; ++i) {
hash.Insert(keysCount, val++);
}
{
decltype(hash) hash2(std::move(hash));
decltype(hash) hash3(pagePool);
hash3.Swap(hash2);
hash = hash3;
}
check = 0;
for (auto i = hash.Find(keysCount); i.Ok(); ++i) {
UNIT_ASSERT_VALUES_EQUAL(keysCount, i.GetKey());
UNIT_ASSERT_VALUES_EQUAL(check++, i.GetValue());
}
UNIT_ASSERT_VALUES_EQUAL(check, val);
UNIT_ASSERT_VALUES_EQUAL(2 * TListPoolBase::GetLargePageCapacity<TItem>() + 1, hash.Count(keysCount));
}
template <typename TItem>
void TestSetImpl() {
const ui32 elementsCount = 32;
const ui64 sumKeysTarget = elementsCount * (elementsCount - 1) / 2;
TAlignedPagePool pagePool(__LOCATION__);
TCompactHashSet<TItem, TItemHash> hash(pagePool);
TVector<TItem> elements(elementsCount);
std::iota(elements.begin(), elements.end(), 0);
Shuffle(elements.begin(), elements.end());
for (TItem i: elements) {
hash.Insert(i);
}
{
decltype(hash) hash2(std::move(hash));
decltype(hash) hash3(pagePool);
hash3.Swap(hash2);
hash = hash3;
}
for (TItem i: elements) {
UNIT_ASSERT(hash.Has(i));
}
UNIT_ASSERT(!hash.Has(elementsCount + 1));
UNIT_ASSERT(!hash.Has(elementsCount + 10));
UNIT_ASSERT_VALUES_EQUAL(elementsCount, hash.Size());
UNIT_ASSERT_VALUES_EQUAL(elementsCount, hash.UniqSize());
ui64 sumKeys = 0;
for (auto i = hash.Iterate(); i.Ok(); ++i) {
sumKeys += *i;
}
UNIT_ASSERT_VALUES_EQUAL(sumKeys, sumKeysTarget);
}
Y_UNIT_TEST(TestHashByte) {
TestHashImpl<ui8>();
}
Y_UNIT_TEST(TestMultiHashByte) {
TestMultiHashImpl<ui8>();
}
Y_UNIT_TEST(TestSetByte) {
TestSetImpl<ui8>();
}
Y_UNIT_TEST(TestHashUi16) {
TestHashImpl<ui16>();
}
Y_UNIT_TEST(TestMultiHashUi16) {
TestMultiHashImpl<ui16>();
}
Y_UNIT_TEST(TestSetUi16) {
TestSetImpl<ui16>();
}
Y_UNIT_TEST(TestHashUi64) {
TestHashImpl<ui64>();
}
Y_UNIT_TEST(TestMultiHashUi64) {
TestMultiHashImpl<ui64>();
}
Y_UNIT_TEST(TestSetUi64) {
TestSetImpl<ui64>();
}
struct TStressHash {
TStressHash(size_t param)
: Param(param)
{
}
template <typename T>
size_t operator() (T num) const {
return num % Param;
}
const size_t Param;
};
Y_UNIT_TEST(TestStressSmallLists) {
TAlignedPagePool pagePool(__LOCATION__);
for (size_t listSize: xrange<size_t>(2, 17, 1)) {
const size_t backets = TListPoolBase::GetSmallPageCapacity<ui64>(listSize);
const size_t elementsCount = backets * listSize;
for (size_t count: xrange<size_t>(1, elementsCount + 1, elementsCount / 16)) {
TCompactHashSet<ui64, TStressHash> hash(pagePool, elementsCount, TStressHash(backets));
for (auto i: xrange(count)) {
hash.Insert(i);
}
UNIT_ASSERT_VALUES_EQUAL(count, hash.Size());
UNIT_ASSERT_VALUES_EQUAL(count, hash.UniqSize());
//hash.PrintStat(Cerr);
}
}
}
}
|