forked from FastFilter/fastfilter_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterapi.h
More file actions
1284 lines (1182 loc) · 43.8 KB
/
filterapi.h
File metadata and controls
1284 lines (1182 loc) · 43.8 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef FILTERAPI_H
#define FILTERAPI_H
#include <climits>
#include <iomanip>
#include <map>
#include <set>
#include <stdexcept>
#include <stdio.h>
#include <vector>
// morton
#include "binaryfusefilter_singleheader.h"
#include "bloom.h"
#include "compressed_cuckoo_filter.h"
#include "counting_bloom.h"
#include "cuckoo_fuse.h"
#include "cuckoofilter.h"
#include "cuckoofilter_stable.h"
#include "gcs.h"
#include "morton_sample_configs.h"
#include "xor_binary_fuse_filter.h"
#include "xorfilter.h"
#include "xorfilter_plus.h"
#include "xorfilter_singleheader.h"
#ifdef __AVX2__
#include "gqf_cpp.h"
#include "simd-block.h"
#include "vqf_cpp.h"
#endif
#define __PF_AVX512__ \
(__AVX512BW__ & __AVX512VL__ & __AVX512CD__ & __AVX512DQ__)
#if __PF_AVX512__
#include "prefix/min_pd256.hpp"
#include "tc-shortcut/tc-shortcut.hpp"
#endif
#include "ribbon_impl.h"
#include "simd-block-fixed-fpp.h"
using namespace std;
using namespace hashing;
using namespace cuckoofilter;
using namespace cuckoofusefilter;
using namespace xorfilter;
using namespace xorfilter_plus;
using namespace bloomfilter;
using namespace counting_bloomfilter;
using namespace gcsfilter;
using namespace CompressedCuckoo; // Morton filter namespace
#ifdef __AVX2__
using namespace gqfilter;
using namespace vqfilter;
#endif
using namespace ribbon;
// Inlining the "contains" which are executed within a tight loop can be both
// very detrimental or very beneficial, and which ways it goes depends on the
// compiler. It is unclear whether we want to benchmark the inlining of
// Contains, as it depends very much on how "contains" is used. So it is maybe
// reasonable to benchmark it without inlining.
//
#define CONTAIN_ATTRIBUTES __attribute__((noinline))
template <typename Table> struct FilterAPI {};
template <typename ItemType, size_t bits_per_item,
template <size_t> class TableType, typename HashFamily>
struct FilterAPI<CuckooFilter<ItemType, bits_per_item, TableType, HashFamily>> {
using Table = CuckooFilter<ItemType, bits_per_item, TableType, HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t key, Table *table) {
if (0 != table->Add(key)) {
throw logic_error("The filter is too small to hold all of the elements");
}
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t key, Table *table) { table->Delete(key); }
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
template <typename ItemType, size_t bits_per_item,
template <size_t> class TableType, typename HashFamily>
struct FilterAPI<
CuckooFilterStable<ItemType, bits_per_item, TableType, HashFamily>> {
using Table =
CuckooFilterStable<ItemType, bits_per_item, TableType, HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t key, Table *table) {
if (0 != table->Add(key)) {
throw logic_error("The filter is too small to hold all of the elements");
}
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t key, Table *table) { table->Delete(key); }
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
template <typename ItemType, typename FingerprintType>
struct FilterAPI<CuckooFuseFilter<ItemType, FingerprintType>> {
using Table = CuckooFuseFilter<ItemType, FingerprintType>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t key, Table *table) {
if (0 != table->Add(key)) {
throw logic_error("The filter is too small to hold all of the elements");
}
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t key, Table *table) { table->Delete(key); }
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
#ifdef __aarch64__
template <typename HashFamily>
struct FilterAPI<SimdBlockFilterFixed<HashFamily>> {
using Table = SimdBlockFilterFixed<HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
Table ans(add_count);
return ans;
}
static void Add(uint64_t key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Find(key);
}
};
#endif
#ifdef __AVX2__
template <typename HashFamily> struct FilterAPI<SimdBlockFilter<HashFamily>> {
using Table = SimdBlockFilter<HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
Table ans(ceil(log2(add_count)));
return ans;
}
static void Add(uint64_t key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Find(key);
}
};
template <typename HashFamily>
struct FilterAPI<SimdBlockFilterFixed64<HashFamily>> {
using Table = SimdBlockFilterFixed64<HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
Table ans(add_count);
return ans;
}
static void Add(uint64_t key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Find(key);
}
};
template <typename HashFamily>
struct FilterAPI<SimdBlockFilterFixed<HashFamily>> {
using Table = SimdBlockFilterFixed<HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
Table ans(add_count);
return ans;
}
static void Add(uint64_t key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Find(key);
}
};
#endif
#if __PF_AVX512__
template <typename HashFamily> struct FilterAPI<TC_shortcut<HashFamily>> {
using Table = TC_shortcut<HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
constexpr float load = .935;
return Table(add_count, load);
}
static void Add(uint64_t key, Table *table) {
if (!table->insert(key)) {
std::cout << table->info() << std::endl;
throw std::logic_error(table->get_name() +
" is too small to hold all of the elements");
}
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static bool Add_attempt(uint64_t key, Table *table) {
if (!table->insert(key)) {
std::cout << "load when failed: \t" << table->get_effective_load()
<< std::endl;
std::cout << table->info() << std::endl;
return false;
}
return true;
}
static void Remove(uint64_t key, Table *table) { table->remove(key); }
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->lookup(key);
}
};
template <typename Table>
inline size_t get_l2_slots(size_t l1_items,
const double overflowing_items_ratio,
const float loads[2]) {
const double expected_items_reaching_next_level =
l1_items * overflowing_items_ratio;
size_t slots_in_l2 = (expected_items_reaching_next_level / loads[1]);
return slots_in_l2;
}
template <>
inline size_t get_l2_slots<cuckoofilter::CuckooFilterStable<u64, 12>>(
size_t l1_items, const double overflowing_items_ratio,
const float loads[2]) {
(void)loads;
(void)overflowing_items_ratio;
constexpr auto expected_items95 = 0.0586;
constexpr auto spare_workload = 0.94;
constexpr auto safety = 1.08;
constexpr auto factor95 = safety * expected_items95 / spare_workload;
const double expected_items_reaching_next_level = l1_items * factor95;
return expected_items_reaching_next_level;
}
template <>
inline size_t get_l2_slots<TC_shortcut<>>(size_t l1_items,
const double overflowing_items_ratio,
const float loads[2]) {
(void)loads;
(void)overflowing_items_ratio;
constexpr auto safety = 1.08;
constexpr auto expected_items95 = 0.0586;
constexpr auto spare_workload = 0.935;
constexpr auto factor95 = safety * expected_items95 / spare_workload;
const double expected_items_reaching_next_level = l1_items * factor95;
size_t slots_in_l2 = std::ceil(expected_items_reaching_next_level);
return slots_in_l2;
}
template <>
inline size_t
get_l2_slots<SimdBlockFilter<>>(size_t l1_items,
const double overflowing_items_ratio,
const float loads[2]) {
const double expected_items_reaching_next_level =
l1_items * overflowing_items_ratio;
size_t slots_in_l2 = (expected_items_reaching_next_level / loads[1]);
return slots_in_l2 * 4;
}
template <>
inline size_t
get_l2_slots<SimdBlockFilterFixed<>>(size_t l1_items,
const double overflowing_items_ratio,
const float loads[2]) {
const double expected_items_reaching_next_level =
l1_items * overflowing_items_ratio;
size_t slots_in_l2 = (expected_items_reaching_next_level / loads[1]);
return slots_in_l2 * 2;
}
template <typename Table,
typename HashFamily = hashing::TwoIndependentMultiplyShift>
class Prefix_Filter {
const size_t filter_max_capacity;
const size_t number_of_pd;
Table GenSpare;
hashing::TwoIndependentMultiplyShift Hasher, H0;
__m256i *pd_array;
size_t cap[2] = {0};
static double constexpr overflowing_items_ratio = 0.0586;
public:
Prefix_Filter(size_t max_items, const float loads[2])
: filter_max_capacity(max_items),
number_of_pd(
std::ceil(1.0 * max_items / (min_pd::MAX_CAP0 * loads[0]))),
GenSpare(FilterAPI<Table>::ConstructFromAddCount(
get_l2_slots<Table>(max_items, overflowing_items_ratio, loads))),
Hasher(), H0() {
int ok = posix_memalign((void **)&pd_array, 32, 32 * number_of_pd);
if (ok != 0) {
std::cout << "Space allocation failed!" << std::endl;
assert(false);
exit(-3);
}
constexpr uint64_t pd256_plus_init_header =
(((INT64_C(1) << min_pd::QUOTS) - 1) << 6) | 32;
for (size_t i = 0; i < number_of_pd; i++) {
pd_array[i] = __m256i{pd256_plus_init_header, 0, 0, 0};
}
}
~Prefix_Filter() { free(pd_array); }
__attribute__((always_inline)) inline static constexpr uint32_t
reduce32(uint32_t hash, uint32_t n) {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
return (uint32_t)(((uint64_t)hash * n) >> 32);
}
__attribute__((always_inline)) inline static constexpr uint16_t
fixed_reduce(uint16_t hash) {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
return (uint16_t)(((uint32_t)hash * 6400) >> 16);
}
inline auto Find(const u64 &item) const -> bool {
const u64 s = H0(item);
uint32_t out1 = s >> 32u, out2 = s;
const uint32_t pd_index = reduce32(out1, (uint32_t)number_of_pd);
const uint16_t qr = fixed_reduce(out2);
const int64_t quot = qr >> 8;
const uint8_t rem = qr;
// return min_pd::pd_find_25(quot, rem, &pd_array[pd_index]);
// return (!min_pd::cmp_qr1(qr, &pd_array[pd_index])) ?
// min_pd::pd_find_25(quot, rem, &pd_array[pd_index])
return (!min_pd::cmp_qr1(qr, &pd_array[pd_index]))
? min_pd::find_core(quot, rem, &pd_array[pd_index])
: incSpare_lookup(pd_index, qr);
}
inline auto incSpare_lookup(size_t pd_index, u16 qr) const -> bool {
const u64 data = (pd_index << 13u) | qr;
return FilterAPI<Table>::Contain(data, &GenSpare);
}
inline void incSpare_add(size_t pd_index, const min_pd::add_res &a_info) {
cap[1]++;
u16 qr = (((u16)a_info.quot) << 8u) | a_info.rem;
const u64 data = (pd_index << 13u) | qr;
return FilterAPI<Table>::Add(data, &GenSpare);
}
void Add(const u64 &item) {
const u64 s = H0(item);
constexpr u64 full_mask = (1ULL << 55);
uint32_t out1 = s >> 32u, out2 = s;
const uint32_t pd_index = reduce32(out1, (uint32_t)number_of_pd);
auto pd = pd_array + pd_index;
const uint64_t header = reinterpret_cast<const u64 *>(pd)[0];
const bool not_full = !(header & full_mask);
const uint16_t qr = fixed_reduce(out2);
const int64_t quot = qr >> 8;
const uint8_t rem = qr;
if (not_full) {
cap[0]++;
assert(!min_pd::is_pd_full(pd));
size_t end = min_pd::pd_select64(header >> 6, quot);
const size_t h_index = end + 6;
const u64 mask = _bzhi_u64(-1, h_index);
const u64 lo = header & mask;
const u64 hi = ((header & ~mask) << 1u); // & h_mask;
assert(!(lo & hi));
const u64 h7 = lo | hi;
memcpy(pd, &h7, 7);
const size_t body_index = end - quot;
min_pd::body_add_case0_avx(body_index, rem, pd);
assert(min_pd::find_core(quot, rem, pd));
assert(Find(item));
return;
} else {
auto add_res = min_pd::new_pd_swap_short(quot, rem, pd);
incSpare_add(pd_index, add_res);
assert(Find(item));
}
}
size_t SizeInBytes() const {
size_t l1 = sizeof(__m256i) * number_of_pd;
size_t l2 = GenSpare.SizeInBytes();
auto res = l1 + l2;
return res;
}
};
template <typename filterTable> struct FilterAPI<Prefix_Filter<filterTable>> {
using Table = Prefix_Filter<filterTable>;
static Table ConstructFromAddCount(size_t add_count) {
constexpr float loads[2] = {.95, .95};
return Table(add_count, loads);
}
static void Add(u64 key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(u64, Table *) { throw std::runtime_error("Unsupported"); }
CONTAIN_ATTRIBUTES static bool Contain(u64 key, const Table *table) {
return table->Find(key);
}
};
#endif
#ifdef __SSE4_1__
template <typename HashFamily>
struct FilterAPI<SimdBlockFilterFixed16<HashFamily>> {
using Table = SimdBlockFilterFixed16<HashFamily>;
static Table ConstructFromAddCount(size_t add_count) {
Table ans(add_count);
return ans;
}
static void Add(uint64_t key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
for (size_t i = start; i < end; i++) {
Add(keys[i], table);
}
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Find(key);
}
};
#endif
template <typename ItemType, typename FingerprintType>
struct FilterAPI<XorFilter<ItemType, FingerprintType>> {
using Table = XorFilter<ItemType, FingerprintType>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<ItemType> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
template <typename CoeffType, bool kHomog, uint32_t kNumColumns,
bool kSmash = false>
struct RibbonTS {
static constexpr bool kIsFilter = true;
static constexpr bool kHomogeneous = kHomog;
static constexpr bool kFirstCoeffAlwaysOne = true;
static constexpr bool kUseSmash = kSmash;
using CoeffRow = CoeffType;
using Hash = uint64_t;
using Key = uint64_t;
using Seed = uint32_t;
using Index = size_t;
using ResultRow = uint32_t;
static constexpr bool kAllowZeroStarts = false;
static constexpr uint32_t kFixedNumColumns = kNumColumns;
static Hash HashFn(const Hash &input, Seed raw_seed) {
// return input;
uint64_t h = input + raw_seed;
h ^= h >> 33;
h *= UINT64_C(0xff51afd7ed558ccd);
h ^= h >> 33;
h *= UINT64_C(0xc4ceb9fe1a85ec53);
h ^= h >> 33;
return h;
}
};
template <typename CoeffType, uint32_t kNumColumns,
uint32_t kMilliBitsPerKey = 7700>
class HomogRibbonFilter {
using TS = RibbonTS<CoeffType, /*kHomog*/ true, kNumColumns>;
IMPORT_RIBBON_IMPL_TYPES(TS);
size_t num_slots;
size_t bytes;
unique_ptr<char[]> ptr;
InterleavedSoln soln;
Hasher hasher;
public:
static constexpr double kFractionalCols =
kNumColumns == 0 ? kMilliBitsPerKey / 1000.0 : kNumColumns;
static double GetBestOverheadFactor() {
double overhead =
(4.0 + kFractionalCols * 0.25) / (8.0 * sizeof(CoeffType));
return 1.0 + overhead;
}
HomogRibbonFilter(size_t add_count)
: num_slots(InterleavedSoln::RoundUpNumSlots(
(size_t)(GetBestOverheadFactor() * add_count))),
bytes(static_cast<size_t>((num_slots * kFractionalCols + 7) / 8)),
ptr(new char[bytes]), soln(ptr.get(), bytes) {}
void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end) {
Banding b(num_slots);
(void)b.AddRange(keys.begin() + start, keys.begin() + end);
soln.BackSubstFrom(b);
}
bool Contain(uint64_t key) const { return soln.FilterQuery(key, hasher); }
size_t SizeInBytes() const { return bytes; }
};
template <typename CoeffType, uint32_t kNumColumns, uint32_t kMilliBitsPerKey>
struct FilterAPI<HomogRibbonFilter<CoeffType, kNumColumns, kMilliBitsPerKey>> {
using Table = HomogRibbonFilter<CoeffType, kNumColumns, kMilliBitsPerKey>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Contain(key);
}
};
template <typename CoeffType, uint32_t kNumColumns, uint32_t kMinPctOverhead,
uint32_t kMilliBitsPerKey = 7700>
class BalancedRibbonFilter {
using TS = RibbonTS<CoeffType, /*kHomog*/ false, kNumColumns>;
IMPORT_RIBBON_IMPL_TYPES(TS);
static constexpr uint32_t kBitsPerVshard = 8;
using BalancedBanding = ribbon::BalancedBanding<TS, kBitsPerVshard>;
using BalancedHasher = ribbon::BalancedHasher<TS, kBitsPerVshard>;
uint32_t log2_vshards;
size_t num_slots;
size_t bytes;
unique_ptr<char[]> ptr;
InterleavedSoln soln;
size_t meta_bytes;
unique_ptr<char[]> meta_ptr;
BalancedHasher hasher;
public:
static constexpr double kFractionalCols =
kNumColumns == 0 ? kMilliBitsPerKey / 1000.0 : kNumColumns;
static double GetNumSlots(size_t add_count, uint32_t log2_vshards) {
size_t add_per_vshard = add_count >> log2_vshards;
double overhead;
if (sizeof(CoeffType) == 8) {
overhead = 0.0000055 * add_per_vshard; // FIXME?
} else if (sizeof(CoeffType) == 4) {
overhead = 0.00005 * add_per_vshard;
} else if (sizeof(CoeffType) == 2) {
overhead = 0.00010 * add_per_vshard; // FIXME?
} else {
assert(sizeof(CoeffType) == 16);
overhead = 0.0000013 * add_per_vshard;
}
overhead = std::max(overhead, 0.01 * kMinPctOverhead);
return InterleavedSoln::RoundUpNumSlots(
(size_t)(add_count + overhead * add_count + add_per_vshard / 5));
}
BalancedRibbonFilter(size_t add_count)
: log2_vshards(
(uint32_t)FloorLog2((add_count + add_count / 3 + add_count / 5) /
(128 * sizeof(CoeffType)))),
num_slots(GetNumSlots(add_count, log2_vshards)),
bytes(static_cast<size_t>((num_slots * kFractionalCols + 7) / 8)),
ptr(new char[bytes]), soln(ptr.get(), bytes),
meta_bytes(BalancedHasher(log2_vshards, nullptr).GetMetadataLength()),
meta_ptr(new char[meta_bytes]), hasher(log2_vshards, meta_ptr.get()) {}
void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end) {
BalancedBanding b(log2_vshards);
b.BalancerAddRange(keys.begin() + start, keys.begin() + end);
if (!b.Balance(num_slots)) {
fprintf(stderr, "Failed!\n");
return;
}
soln.BackSubstFrom(b);
memcpy(meta_ptr.get(), b.GetMetadata(), b.GetMetadataLength());
}
bool Contain(uint64_t key) const { return soln.FilterQuery(key, hasher); }
size_t SizeInBytes() const { return bytes + meta_bytes; }
};
template <typename CoeffType, uint32_t kNumColumns, uint32_t kMinPctOverhead,
uint32_t kMilliBitsPerKey>
struct FilterAPI<BalancedRibbonFilter<CoeffType, kNumColumns, kMinPctOverhead,
kMilliBitsPerKey>> {
using Table = BalancedRibbonFilter<CoeffType, kNumColumns, kMinPctOverhead,
kMilliBitsPerKey>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Contain(key);
}
};
template <typename CoeffType, uint32_t kNumColumns, uint32_t kMinPctOverhead,
bool kUseSmash = false>
class StandardRibbonFilter {
using TS = RibbonTS<CoeffType, /*kHomog*/ false, kNumColumns, kUseSmash>;
IMPORT_RIBBON_IMPL_TYPES(TS);
size_t num_slots;
size_t bytes;
unique_ptr<char[]> ptr;
InterleavedSoln soln;
Hasher hasher;
public:
static constexpr double kFractionalCols =
kNumColumns == 0 ? 7.7 : kNumColumns;
static double GetNumSlots(size_t add_count) {
double overhead;
if (sizeof(CoeffType) == 8) {
overhead = -0.0251 + std::log(1.0 * add_count) * 1.4427 * 0.0083;
} else {
assert(sizeof(CoeffType) == 16);
overhead = -0.0176 + std::log(1.0 * add_count) * 1.4427 * 0.0038;
}
overhead = std::max(overhead, 0.01 * kMinPctOverhead);
return InterleavedSoln::RoundUpNumSlots(
(size_t)(add_count + overhead * add_count));
}
StandardRibbonFilter(size_t add_count)
: num_slots(GetNumSlots(add_count)),
bytes(static_cast<size_t>((num_slots * kFractionalCols + 7) / 8)),
ptr(new char[bytes]), soln(ptr.get(), bytes) {}
void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end) {
Banding b(num_slots);
if (!b.AddRange(keys.begin() + start, keys.begin() + end)) {
fprintf(stderr, "Failed!\n");
return;
}
soln.BackSubstFrom(b);
}
bool Contain(uint64_t key) const { return soln.FilterQuery(key, hasher); }
size_t SizeInBytes() const { return bytes; }
};
template <typename CoeffType, uint32_t kNumColumns, uint32_t kMinPctOverhead,
bool kUseSmash>
struct FilterAPI<
StandardRibbonFilter<CoeffType, kNumColumns, kMinPctOverhead, kUseSmash>> {
using Table =
StandardRibbonFilter<CoeffType, kNumColumns, kMinPctOverhead, kUseSmash>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return table->Contain(key);
}
};
template <typename ItemType, typename FingerprintType>
struct FilterAPI<
xorbinaryfusefilter_naive::XorBinaryFuseFilter<ItemType, FingerprintType>> {
using Table =
xorbinaryfusefilter_naive::XorBinaryFuseFilter<ItemType, FingerprintType>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<ItemType> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
template <typename ItemType, typename FingerprintType>
struct FilterAPI<xorbinaryfusefilter_lowmem::XorBinaryFuseFilter<
ItemType, FingerprintType>> {
using Table =
xorbinaryfusefilter_lowmem::XorBinaryFuseFilter<ItemType,
FingerprintType>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<ItemType> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
template <typename ItemType, typename FingerprintType>
struct FilterAPI<xorbinaryfusefilter_naive4wise::XorBinaryFuseFilter<
ItemType, FingerprintType>> {
using Table =
xorbinaryfusefilter_naive4wise::XorBinaryFuseFilter<ItemType,
FingerprintType>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<ItemType> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
template <typename ItemType, typename FingerprintType>
struct FilterAPI<xorbinaryfusefilter_lowmem4wise::XorBinaryFuseFilter<
ItemType, FingerprintType>> {
using Table =
xorbinaryfusefilter_lowmem4wise::XorBinaryFuseFilter<ItemType,
FingerprintType>;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(const vector<ItemType> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
return (0 == table->Contain(key));
}
};
class MortonFilter {
Morton3_8 *filter;
size_t size;
public:
MortonFilter(const size_t size) {
filter = new Morton3_8((size_t)(size / 0.95) + 64);
// filter = new Morton3_8((size_t) (2.1 * size) + 64);
this->size = size;
}
~MortonFilter() { delete filter; }
void Add(uint64_t key) { filter->insert(key); }
void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end) {
size_t size = end - start;
::std::vector<uint64_t> k(size);
::std::vector<bool> status(size);
for (size_t i = start; i < end; i++) {
k[i - start] = keys[i];
}
// TODO return value and status is ignored currently
filter->insert_many(k, status, size);
}
inline bool Contain(uint64_t &item) { return filter->likely_contains(item); };
size_t SizeInBytes() const {
// according to morton_sample_configs.h:
// Morton3_8 - 3-slot buckets with 8-bit fingerprints: 11.7 bits/item
// (load factor = 0.95)
// so in theory we could just hardcode the size here,
// and don't measure it
// return (size_t)((size * 11.7) / 8);
return filter->SizeInBytes();
}
};
template <> struct FilterAPI<MortonFilter> {
using Table = MortonFilter;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t key, Table *table) { table->Add(key); }
static void AddAll(const vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys, start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, Table *table) {
return table->Contain(key);
}
};
class XorSingle {
public:
xor8_s filter; // let us expose the struct. to avoid indirection
explicit XorSingle(const size_t size) {
if (!xor8_allocate(size, &filter)) {
throw ::std::runtime_error("Allocation failed");
}
}
~XorSingle() { xor8_free(&filter); }
bool AddAll(uint64_t *data, const size_t start, const size_t end) {
return xor8_buffered_populate(data + start, end - start, &filter);
}
inline bool Contain(uint64_t &item) const {
return xor8_contain(item, &filter);
}
inline size_t SizeInBytes() const { return xor8_size_in_bytes(&filter); }
XorSingle(XorSingle &&o) : filter(o.filter) {
o.filter.fingerprints = nullptr; // we take ownership for the data
}
private:
XorSingle(const XorSingle &o) = delete;
};
template <> struct FilterAPI<XorSingle> {
using Table = XorSingle;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {
table->AddAll(keys.data(), start, end);
}
static void Remove(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
CONTAIN_ATTRIBUTES static bool Contain(uint64_t key, const Table *table) {
// some compilers are not smart enough to do the inlining properly
return xor8_contain(key, &table->filter);
}
};
class BinaryFuseSingle {
public:
binary_fuse8_t filter; // let us expose the struct. to avoid indirection
explicit BinaryFuseSingle(const size_t size) {
if (!binary_fuse8_allocate(size, &filter)) {
throw ::std::runtime_error("Allocation failed");
}
}
~BinaryFuseSingle() { binary_fuse8_free(&filter); }
bool AddAll(uint64_t *data, const size_t start, const size_t end) {
return binary_fuse8_populate(data + start, end - start, &filter);
}
inline bool Contain(uint64_t &item) const {
return binary_fuse8_contain(item, &filter);
}
inline size_t SizeInBytes() const {
return binary_fuse8_size_in_bytes(&filter);
}
BinaryFuseSingle(BinaryFuseSingle &&o) : filter(o.filter) {
o.filter.Fingerprints = nullptr; // we take ownership for the data
}
private:
BinaryFuseSingle(const BinaryFuseSingle &o) = delete;
};
template <> struct FilterAPI<BinaryFuseSingle> {
using Table = BinaryFuseSingle;
static Table ConstructFromAddCount(size_t add_count) {
return Table(add_count);
}
static void Add(uint64_t, Table *) {
throw std::runtime_error("Unsupported");
}
static void AddAll(vector<uint64_t> &keys, const size_t start,
const size_t end, Table *table) {