proxygen
SparseByteSetBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present 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 /***
18  * A benchmark comparing SparseByteSet to bitset<256> and bool[256].
19  */
20 
21 #include <folly/Benchmark.h>
22 #include <folly/Format.h>
25 #include <bitset>
26 #include <random>
27 #include <vector>
28 
29 using namespace std;
30 using namespace folly;
31 
32 namespace {
33 
34 // Interface-identical to SparseByteSet. So that we can do compile-time
35 // polymorphism.
36 class BitSetWrapper {
37  public:
38  inline bool add(uint8_t i) {
39  auto r = !contains(i);
40  if (r) {
41  rep_[i] = true;
42  }
43  return r;
44  }
45  inline bool contains(uint8_t i) {
46  return rep_[i];
47  }
48 
49  private:
50  bitset<256> rep_;
51 };
52 class BoolArraySet {
53  public:
54  BoolArraySet() {
55  memset(rep_, 0, sizeof(rep_));
56  }
57  inline bool add(uint8_t i) {
58  auto r = !contains(i);
59  if (r) {
60  rep_[i] = true;
61  }
62  return r;
63  }
64  inline bool contains(uint8_t i) {
65  return rep_[i];
66  }
67 
68  private:
69  bool rep_[256];
70 };
71 
72 template <typename Coll>
73 void rand_bench(int iters, size_t size_add, size_t size_contains) {
74  BenchmarkSuspender braces;
75  vector<uint8_t> seq_add;
76  vector<uint8_t> seq_contains;
77  mt19937 rng;
78  uniform_int_distribution<uint8_t> dist;
79  for (size_t i = 0; i < size_add; ++i) {
80  seq_add.push_back(dist(rng));
81  }
82  for (size_t i = 0; i < size_contains; ++i) {
83  seq_contains.push_back(dist(rng));
84  }
85  braces.dismissing([&] {
86  while (iters--) {
87  Coll coll;
88  for (auto b : seq_add) {
89  coll.add(b);
90  }
91  bool q{};
92  for (auto b : seq_contains) {
93  q ^= coll.contains(b);
94  }
96  }
97  });
98 }
99 
100 void setup_rand_bench() {
101  vector<pair<size_t, size_t>> rand_bench_params = {
102  {4, 4},
103  {4, 16},
104  {4, 64},
105  {4, 256},
106  {16, 4},
107  {16, 16},
108  {16, 64},
109  {16, 256},
110  {64, 4},
111  {64, 16},
112  {64, 64},
113  {64, 256},
114  {256, 4},
115  {256, 16},
116  {256, 64},
117  {256, 256},
118  };
119  for (auto kvp : rand_bench_params) {
120  size_t size_add, size_contains;
121  tie(size_add, size_contains) = kvp;
122  addBenchmark(
123  __FILE__,
124  sformat("bitset_rand_bench({}, {})", size_add, size_contains).c_str(),
125  [=](int iters) {
126  rand_bench<BitSetWrapper>(iters, size_add, size_contains);
127  return iters;
128  });
129  addBenchmark(
130  __FILE__,
131  sformat("%bool_array_set_rand_bench({}, {})", size_add, size_contains)
132  .c_str(),
133  [=](int iters) {
134  rand_bench<BoolArraySet>(iters, size_add, size_contains);
135  return iters;
136  });
137  addBenchmark(
138  __FILE__,
139  sformat("%sparse_byte_set_rand_bench({}, {})", size_add, size_contains)
140  .c_str(),
141  [=](int iters) {
142  rand_bench<SparseByteSet>(iters, size_add, size_contains);
143  return iters;
144  });
145  addBenchmark(__FILE__, "-", [](int) { return 0; });
146  }
147 }
148 
149 } // namespace
150 
151 int main(int argc, char** argv) {
152  gflags::ParseCommandLineFlags(&argc, &argv, true);
153  setup_rand_bench();
154  runBenchmarks();
155  return 0;
156 }
char b
auto add
Definition: BaseTest.cpp:70
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
auto rng
Definition: CollectTest.cpp:31
char ** argv
Contains contains(Needle &&needle)
Definition: Base.h:831
std::uniform_int_distribution< milliseconds::rep > dist
int main(int argc, char **argv)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
std::enable_if< boost::function_types::function_arity< decltype(&Lambda::operator())>::value==2 >::type addBenchmark(const char *file, const char *name, Lambda &&lambda)
Definition: Benchmark.h:172
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258