proxygen
EliasFanoCodingTest.cpp File Reference
#include <algorithm>
#include <limits>
#include <numeric>
#include <random>
#include <vector>
#include <folly/Benchmark.h>
#include <folly/experimental/EliasFanoCoding.h>
#include <folly/experimental/Select64.h>
#include <folly/experimental/test/CodingTestUtils.h>
#include <folly/init/Init.h>

Go to the source code of this file.

Classes

class  EliasFanoCodingTest
 

Namespaces

 bm
 

Functions

 TEST (EliasFanoCoding, defaultNumLowerBits)
 
 TEST_F (EliasFanoCodingTest, Empty)
 
 TEST_F (EliasFanoCodingTest, Simple)
 
 TEST_F (EliasFanoCodingTest, SkipPointers)
 
 TEST_F (EliasFanoCodingTest, ForwardPointers)
 
 TEST_F (EliasFanoCodingTest, SkipForwardPointers)
 
 TEST_F (EliasFanoCodingTest, BugLargeGapInUpperBits)
 
void bm::init ()
 
void bm::free ()
 
 BENCHMARK (Next, iters)
 
size_t Skip_ForwardQ128 (size_t iters, size_t logAvgSkip)
 
 BENCHMARK (Jump_ForwardQ128, iters)
 
 BENCHMARK_DRAW_LINE ()
 
size_t SkipTo_SkipQ128 (size_t iters, size_t logAvgSkip)
 
 BENCHMARK (JumpTo_SkipQ128, iters)
 
 BENCHMARK (Encode_10)
 
 BENCHMARK (Encode)
 
 BENCHMARK (defaultNumLowerBits, iters)
 
 BENCHMARK (slowDefaultNumLowerBits, iters)
 
int main (int argc, char **argv)
 

Variables

std::vector< std::pair< size_t, size_t > > bm::numLowerBitsInput
 

Function Documentation

BENCHMARK ( Next  ,
iters   
)

Definition at line 198 of file EliasFanoCodingTest.cpp.

References bm::data, folly::compression::dispatchInstructions(), and bm::list.

198  {
199  dispatchInstructions([&](auto instructions) {
200  bmNext<EliasFanoReader<bm::Encoder, decltype(instructions)>>(
201  bm::list, bm::data, iters);
202  });
203 }
std::vector< uint32_t > data
auto dispatchInstructions(F &&f) -> decltype(f(std::declval< instructions::Default >()))
Encoder::MutableCompressedList list
BENCHMARK ( Jump_ForwardQ128  ,
iters   
)

Definition at line 221 of file EliasFanoCodingTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), bm::data, folly::compression::dispatchInstructions(), bm::list, and bm::order.

221  {
222  dispatchInstructions([&](auto instructions) {
223  bmJump<EliasFanoReader<bm::Encoder, decltype(instructions)>>(
224  bm::list, bm::data, bm::order, iters);
225  });
226 }
std::vector< uint32_t > data
auto dispatchInstructions(F &&f) -> decltype(f(std::declval< instructions::Default >()))
std::vector< size_t > order
Encoder::MutableCompressedList list
BENCHMARK ( JumpTo_SkipQ128  ,
iters   
)

Definition at line 246 of file EliasFanoCodingTest.cpp.

References folly::BENCHMARK_DRAW_LINE(), bm::data, folly::compression::dispatchInstructions(), bm::list, and bm::order.

246  {
247  dispatchInstructions([&](auto instructions) {
248  bmJumpTo<EliasFanoReader<bm::Encoder, decltype(instructions)>>(
249  bm::list, bm::data, bm::order, iters);
250  });
251 }
std::vector< uint32_t > data
auto dispatchInstructions(F &&f) -> decltype(f(std::declval< instructions::Default >()))
std::vector< size_t > order
Encoder::MutableCompressedList list
BENCHMARK ( Encode_10  )

Definition at line 255 of file EliasFanoCodingTest.cpp.

References folly::test::begin(), folly::compression::BitVectorEncoder< Value, SkipValue, kSkipQuantum, kForwardQuantum >::encode(), bm::encodeSmallData, folly::test::end(), folly::compression::BitVectorCompressedListBase< Pointer >::free(), and bm::list.

255  {
256  auto list = bm::Encoder::encode(
258  list.free();
259 }
std::vector< uint32_t > encodeSmallData
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
static MutableCompressedList encode(RandomAccessIterator begin, RandomAccessIterator end)
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
Encoder::MutableCompressedList list
auto free() -> decltype(::free(T(nullptr)))
BENCHMARK ( Encode  )

Definition at line 261 of file EliasFanoCodingTest.cpp.

References folly::test::begin(), folly::BENCHMARK_DRAW_LINE(), folly::compression::BitVectorEncoder< Value, SkipValue, kSkipQuantum, kForwardQuantum >::encode(), bm::encodeLargeData, folly::test::end(), folly::compression::BitVectorCompressedListBase< Pointer >::free(), and bm::list.

261  {
262  auto list = bm::Encoder::encode(
264  list.free();
265 }
std::vector< uint32_t > encodeLargeData
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
static MutableCompressedList encode(RandomAccessIterator begin, RandomAccessIterator end)
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
Encoder::MutableCompressedList list
auto free() -> decltype(::free(T(nullptr)))
BENCHMARK ( defaultNumLowerBits  ,
iters   
)

Definition at line 269 of file EliasFanoCodingTest.cpp.

References folly::doNotOptimizeAway(), i, bm::numLowerBitsInput, and folly::size().

269  {
271 
272  size_t i = 0;
273  while (iters--) {
274  const auto& p = bm::numLowerBitsInput[i];
275  folly::doNotOptimizeAway(Encoder::defaultNumLowerBits(p.first, p.second));
276  if (++i == bm::numLowerBitsInput.size()) {
277  i = 0;
278  }
279  }
280 }
BitVectorEncoder< uint32_t, uint32_t, 128, 128 > Encoder
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
std::vector< std::pair< size_t, size_t > > numLowerBitsInput
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( slowDefaultNumLowerBits  ,
iters   
)

Definition at line 282 of file EliasFanoCodingTest.cpp.

References folly::doNotOptimizeAway(), i, bm::numLowerBitsInput, s, folly::size(), Skip_ForwardQ128(), SkipTo_SkipQ128(), and folly::detail::distributed_mutex::time().

282  {
283  size_t i = 0;
284  while (iters--) {
285  const auto& p = bm::numLowerBitsInput[i];
286  folly::doNotOptimizeAway(slowDefaultNumLowerBits(p.first, p.second));
287  if (++i == bm::numLowerBitsInput.size()) {
288  i = 0;
289  }
290  }
291 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
std::vector< std::pair< size_t, size_t > > numLowerBitsInput
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_DRAW_LINE ( )
int main ( int  argc,
char **  argv 
)

Definition at line 327 of file EliasFanoCodingTest.cpp.

References bm::free(), folly::init(), bm::init(), testing::InitGoogleTest(), RUN_ALL_TESTS(), and folly::runBenchmarks().

327  {
329  folly::init(&argc, &argv);
330  gflags::ParseCommandLineFlags(&argc, &argv, true);
331 
332  auto ret = RUN_ALL_TESTS();
333  if (ret == 0 && FLAGS_benchmark) {
334  bm::init();
336  bm::free();
337  }
338 
339  return ret;
340 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
void runBenchmarks()
Definition: Benchmark.cpp:456
void init()
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
void free()
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
size_t Skip_ForwardQ128 ( size_t  iters,
size_t  logAvgSkip 
)

Definition at line 205 of file EliasFanoCodingTest.cpp.

References BENCHMARK_NAMED_PARAM_MULTI, bm::data, folly::compression::dispatchInstructions(), and bm::list.

Referenced by BENCHMARK().

205  {
206  dispatchInstructions([&](auto instructions) {
207  bmSkip<EliasFanoReader<bm::Encoder, decltype(instructions)>>(
208  bm::list, bm::data, logAvgSkip, iters);
209  });
210  return iters;
211 }
std::vector< uint32_t > data
auto dispatchInstructions(F &&f) -> decltype(f(std::declval< instructions::Default >()))
Encoder::MutableCompressedList list
size_t SkipTo_SkipQ128 ( size_t  iters,
size_t  logAvgSkip 
)

Definition at line 230 of file EliasFanoCodingTest.cpp.

References BENCHMARK_NAMED_PARAM_MULTI, bm::data, folly::compression::dispatchInstructions(), and bm::list.

Referenced by BENCHMARK().

230  {
231  dispatchInstructions([&](auto instructions) {
232  bmSkipTo<EliasFanoReader<bm::Encoder, decltype(instructions)>>(
233  bm::list, bm::data, logAvgSkip, iters);
234  });
235  return iters;
236 }
std::vector< uint32_t > data
auto dispatchInstructions(F &&f) -> decltype(f(std::declval< instructions::Default >()))
Encoder::MutableCompressedList list
TEST ( EliasFanoCoding  ,
defaultNumLowerBits   
)

Definition at line 43 of file EliasFanoCodingTest.cpp.

References a, b, folly::gen::batch(), EXPECT_EQ, i, max, min, and folly::size().

43  {
44  // Verify that slowDefaultNumLowerBits and optimized
45  // Encoder::defaultNumLowerBits agree.
46  static constexpr size_t kNumIterations = 2500;
47  auto compare = [](size_t upperBound, size_t size) {
49  EXPECT_EQ(
50  int(slowDefaultNumLowerBits(upperBound, size)),
51  int(Encoder::defaultNumLowerBits(upperBound, size)))
52  << upperBound << " " << size;
53  };
54  auto batch = [&compare](size_t initialUpperBound) {
55  for (size_t upperBound = initialUpperBound, i = 0; i < kNumIterations;
56  ++i, --upperBound) {
57  // Test "size" values close to "upperBound".
58  for (size_t size = upperBound, j = 0; j < kNumIterations; ++j, --size) {
59  compare(upperBound, size);
60  }
61  // Sample "size" values between [0, upperBound].
62  for (size_t size = upperBound; size > 1 + upperBound / kNumIterations;
63  size -= 1 + upperBound / kNumIterations) {
64  compare(upperBound, size);
65  }
66  // Test "size" values close to 0.
67  for (size_t size = 0; size < kNumIterations; ++size) {
68  compare(upperBound, size);
69  }
70  }
71  };
73  batch(kNumIterations + 1312213123);
74  batch(kNumIterations);
75 
76  std::mt19937 gen;
77  std::uniform_int_distribution<size_t> distribution;
78  for (size_t i = 0; i < kNumIterations; ++i) {
79  const auto a = distribution(gen);
80  const auto b = distribution(gen);
81  compare(std::max(a, b), std::min(a, b));
82  }
83 }
BitVectorEncoder< uint32_t, uint32_t, 128, 128 > Encoder
char b
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
detail::Batch batch(size_t batchSize)
Definition: Base-inl.h:2602
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
LogLevel min
Definition: LogLevel.cpp:30
char a
TEST_F ( EliasFanoCodingTest  ,
Empty   
)

Definition at line 109 of file EliasFanoCodingTest.cpp.

109  {
110  doTestEmpty();
111 }
TEST_F ( EliasFanoCodingTest  ,
Simple   
)

Definition at line 113 of file EliasFanoCodingTest.cpp.

113  {
114  doTestAll<0, 0, uint32_t>();
115  doTestAll<0, 0, size_t>();
116 }
TEST_F ( EliasFanoCodingTest  ,
SkipPointers   
)

Definition at line 118 of file EliasFanoCodingTest.cpp.

118  {
119  doTestAll<128, 0, uint32_t>();
120  doTestAll<128, 0, size_t>();
121 }
TEST_F ( EliasFanoCodingTest  ,
ForwardPointers   
)

Definition at line 123 of file EliasFanoCodingTest.cpp.

123  {
124  doTestAll<0, 128, uint32_t>();
125  doTestAll<0, 128, size_t>();
126 }
TEST_F ( EliasFanoCodingTest  ,
SkipForwardPointers   
)

Definition at line 128 of file EliasFanoCodingTest.cpp.

128  {
129  doTestAll<128, 128, uint32_t>();
130  doTestAll<128, 128, size_t>();
131 }
TEST_F ( EliasFanoCodingTest  ,
BugLargeGapInUpperBits   
)

Definition at line 133 of file EliasFanoCodingTest.cpp.

References ASSERT_EQ, ASSERT_TRUE, folly::data(), encode(), bm::encodeLargeData, bm::encodeSmallData, folly::compression::BitVectorCompressedListBase< Pointer >::free(), i, bm::list, order, and uint32_t.

133  { // t16274876
136  constexpr uint32_t kLargeValue = 127;
137 
138  // Build a list where the upper bits have a large gap after the
139  // first element, so that we need to reposition in the upper bits
140  // using skips to position the iterator on the second element.
141  std::vector<uint32_t> data = {0, kLargeValue};
142  for (uint32_t i = 0; i < kLargeValue; ++i) {
143  data.push_back(data.back() + 1);
144  }
145  auto list = Encoder::encode(data.begin(), data.end());
146 
147  {
148  Reader reader(list);
149  ASSERT_TRUE(reader.skipTo(kLargeValue - 1));
150  ASSERT_EQ(kLargeValue, reader.value());
151  ASSERT_EQ(0, reader.previousValue());
152  }
153 
154  list.free();
155 }
unique_ptr< IOBuf > encode(vector< HPACKHeader > &headers, HPACKEncoder &encoder)
BitVectorEncoder< uint32_t, uint32_t, 128, 128 > Encoder
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
Encoder::MutableCompressedList list
auto free() -> decltype(::free(T(nullptr)))
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43