Implement a BitVector-based list
[folly.git] / folly / experimental / test / EliasFanoCodingTest.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <algorithm>
18 #include <numeric>
19 #include <random>
20 #include <vector>
21
22 #include <folly/Benchmark.h>
23 #include <folly/experimental/EliasFanoCoding.h>
24 #include <folly/experimental/Select64.h>
25 #include <folly/experimental/test/CodingTestUtils.h>
26
27 using namespace folly::compression;
28
29 #ifndef EF_TEST_ARCH
30 #define EF_TEST_ARCH Default
31 #endif  // EF_TEST_ARCH
32
33 class EliasFanoCodingTest : public ::testing::Test {
34  public:
35   void doTestEmpty() {
36     typedef EliasFanoEncoderV2<uint32_t, size_t> Encoder;
37     typedef EliasFanoReader<Encoder> Reader;
38     testEmpty<Reader, Encoder>();
39   }
40
41   template <size_t kSkipQuantum, size_t kForwardQuantum>
42   void doTestAll() {
43     typedef EliasFanoEncoderV2<
44       uint32_t, uint32_t, kSkipQuantum, kForwardQuantum> Encoder;
45     typedef EliasFanoReader<Encoder, instructions::EF_TEST_ARCH> Reader;
46     testAll<Reader, Encoder>(generateRandomList(100 * 1000, 10 * 1000 * 1000));
47     testAll<Reader, Encoder>(generateSeqList(1, 100000, 100));
48   }
49 };
50
51 TEST_F(EliasFanoCodingTest, Empty) {
52   doTestEmpty();
53 }
54
55 TEST_F(EliasFanoCodingTest, Simple) {
56   doTestAll<0, 0>();
57 }
58
59 TEST_F(EliasFanoCodingTest, SkipPointers) {
60   doTestAll<128, 0>();
61 }
62
63 TEST_F(EliasFanoCodingTest, ForwardPointers) {
64   doTestAll<0, 128>();
65 }
66
67 TEST_F(EliasFanoCodingTest, SkipForwardPointers) {
68   doTestAll<128, 128>();
69 }
70
71 namespace bm {
72
73 constexpr size_t k1M = 1000000;
74
75 typedef EliasFanoEncoderV2<uint32_t, uint32_t, 128, 128> Encoder;
76 typedef EliasFanoReader<Encoder> Reader;
77
78 std::vector<uint32_t> data;
79 std::vector<size_t> order;
80
81 std::vector<uint32_t> encodeSmallData;
82 std::vector<uint32_t> encodeLargeData;
83
84 typename Encoder::CompressedList list;
85
86 void init() {
87   std::mt19937 gen;
88
89   data = generateRandomList(100 * 1000, 10 * 1000 * 1000, gen);
90   list = Encoder::encode(data.begin(), data.end());
91
92   order.resize(data.size());
93   std::iota(order.begin(), order.end(), size_t());
94   std::shuffle(order.begin(), order.end(), gen);
95
96   encodeSmallData = generateRandomList(10, 100 * 1000, gen);
97   encodeLargeData = generateRandomList(1000 * 1000, 100 * 1000 * 1000, gen);
98 }
99
100 void free() {
101   list.free();
102 }
103
104 }  // namespace bm
105
106 BENCHMARK(Next, iters) {
107   bmNext<bm::Reader>(bm::list, bm::data, iters);
108 }
109
110 size_t Skip_ForwardQ128(size_t iters, size_t logAvgSkip) {
111   bmSkip<bm::Reader>(bm::list, bm::data, logAvgSkip, iters);
112   return iters;
113 }
114
115 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 1, 0)
116 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 2, 1)
117 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 4_pm_1, 2)
118 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 16_pm_4, 4)
119 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 64_pm_16, 6)
120 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 256_pm_64, 8)
121 BENCHMARK_NAMED_PARAM_MULTI(Skip_ForwardQ128, 1024_pm_256, 10)
122
123 BENCHMARK(Jump_ForwardQ128, iters) {
124   bmJump<bm::Reader>(bm::list, bm::data, bm::order, iters);
125 }
126
127 BENCHMARK_DRAW_LINE();
128
129 size_t SkipTo_SkipQ128(size_t iters, size_t logAvgSkip) {
130   bmSkipTo<bm::Reader>(bm::list, bm::data, logAvgSkip, iters);
131   return iters;
132 }
133
134 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 1, 0)
135 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 2, 1)
136 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 4_pm_1, 2)
137 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 16_pm_4, 4)
138 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 64_pm_16, 6)
139 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 256_pm_64, 8)
140 BENCHMARK_NAMED_PARAM_MULTI(SkipTo_SkipQ128, 1024_pm_256, 10)
141
142 BENCHMARK(JumpTo_SkipQ128, iters) {
143   bmJumpTo<bm::Reader>(bm::list, bm::data, bm::order, iters);
144 }
145
146 BENCHMARK_DRAW_LINE();
147
148 BENCHMARK(Encode_10) {
149   auto list = bm::Encoder::encode(bm::encodeSmallData.begin(),
150                                   bm::encodeSmallData.end());
151   list.free();
152 }
153
154 BENCHMARK(Encode) {
155   auto list = bm::Encoder::encode(bm::encodeLargeData.begin(),
156                                   bm::encodeLargeData.end());
157   list.free();
158 }
159
160 BENCHMARK_DRAW_LINE();
161
162 BENCHMARK(Select64, iters) {
163   typedef instructions::EF_TEST_ARCH instr;
164   constexpr uint64_t kPrime = uint64_t(-59);
165   for (uint64_t x = kPrime, i = 0; i < iters; x *= kPrime, i += 1) {
166     size_t w = instr::popcount(x);
167     folly::doNotOptimizeAway(folly::select64<instr>(x, w - 1));
168   }
169 }
170
171 #if 0
172 Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz (turbo off),
173 using instructions::Haswell and GCC 4.9 with --bm_min_usec 100000.
174 ============================================================================
175 folly/experimental/test/EliasFanoCodingTest.cpp relative  time/iter  iters/s
176 ============================================================================
177 Next                                                         2.52ns  397.28M
178 Skip_ForwardQ128(1)                                          3.92ns  255.28M
179 Skip_ForwardQ128(2)                                          5.08ns  197.04M
180 Skip_ForwardQ128(4_pm_1)                                     7.04ns  142.02M
181 Skip_ForwardQ128(16_pm_4)                                   19.68ns   50.82M
182 Skip_ForwardQ128(64_pm_16)                                  27.58ns   36.26M
183 Skip_ForwardQ128(256_pm_64)                                 32.49ns   30.78M
184 Skip_ForwardQ128(1024_pm_256)                               33.39ns   29.95M
185 Jump_ForwardQ128                                            34.05ns   29.37M
186 ----------------------------------------------------------------------------
187 SkipTo_SkipQ128(1)                                           4.42ns  226.49M
188 SkipTo_SkipQ128(2)                                           8.58ns  116.55M
189 SkipTo_SkipQ128(4_pm_1)                                     11.43ns   87.50M
190 SkipTo_SkipQ128(16_pm_4)                                    31.19ns   32.06M
191 SkipTo_SkipQ128(64_pm_16)                                   43.88ns   22.79M
192 SkipTo_SkipQ128(256_pm_64)                                  49.08ns   20.37M
193 SkipTo_SkipQ128(1024_pm_256)                                52.24ns   19.14M
194 JumpTo_SkipQ128                                             54.61ns   18.31M
195 ----------------------------------------------------------------------------
196 Encode_10                                                  117.24ns    8.53M
197 Encode                                                       5.64ms   177.15
198 ----------------------------------------------------------------------------
199 Select64                                                     8.04ns  124.35M
200 ============================================================================
201 #endif
202
203 int main(int argc, char** argv) {
204   testing::InitGoogleTest(&argc, argv);
205   gflags::ParseCommandLineFlags(&argc, &argv, true);
206
207   auto ret = RUN_ALL_TESTS();
208   if (ret == 0 && FLAGS_benchmark) {
209     bm::init();
210     folly::runBenchmarks();
211     bm::free();
212   }
213
214   return ret;
215 }