-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdata_structure_benchmarks.cpp
241 lines (203 loc) · 7.76 KB
/
data_structure_benchmarks.cpp
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
#include <benchmark/benchmark.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/SmallSet.h>
#include <llvm/ADT/DenseMap.h>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <random> // for std::default_random_engine
#include "data_types.h"
#include "ArrayTypes.h"
static inline int rand(int min, int max) { return min + rand() % (max - min + 1); }
#define BENCHMARK_VECTORS 1
#define BENCHMARK_MAPS 1
#define BENCHMARK_SETS 1
#define DO_BENCHMARK_TEMPLATE(bm_function, container) \
BENCHMARK_TEMPLATE(bm_function, container<int>, int )->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Arg(4096)->Arg(8192)->Arg(16384); \
BENCHMARK_TEMPLATE(bm_function, container<size_16>, size_16)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Arg(4096)->Arg(8192)->Arg(16384); \
BENCHMARK_TEMPLATE(bm_function, container<size_64>, size_64)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)->Arg(2048)->Arg(4096)->Arg(8192)->Arg(16384);
#define BENCHMARK_TEMPLATES_3(bm_function, container0, container1, container2) \
DO_BENCHMARK_TEMPLATE(bm_function, container0); \
DO_BENCHMARK_TEMPLATE(bm_function, container1); \
DO_BENCHMARK_TEMPLATE(bm_function, container2);
#define BENCHMARK_TEMPLATES_4(bm_function, container0, container1, container2, container3) \
DO_BENCHMARK_TEMPLATE(bm_function, container0); \
DO_BENCHMARK_TEMPLATE(bm_function, container1); \
DO_BENCHMARK_TEMPLATE(bm_function, container2); \
DO_BENCHMARK_TEMPLATE(bm_function, container3);
#define BENCHMARK_TEMPLATES_5(bm_function, container0, container1, container2, container3, container4) \
DO_BENCHMARK_TEMPLATE(bm_function, container0); \
DO_BENCHMARK_TEMPLATE(bm_function, container1); \
DO_BENCHMARK_TEMPLATE(bm_function, container2); \
DO_BENCHMARK_TEMPLATE(bm_function, container3); \
DO_BENCHMARK_TEMPLATE(bm_function, container4);
/**
* Give you a random ordering of [size] indices. Use this ordering to test random access of indices.
* This ordering will be stored, and the same ordering returned for *all* future calls with this size.
* This allows us to make a true apples-to-apples comparison between container types.
* (You wouldn't want the randomized indices to change between different containers, lest you get
* really lucky with one container and really unlucky with another.)
*/
const std::vector<int> & randomize_lookup_indices(size_t size);
#if BENCHMARK_VECTORS
template <typename Data> using SmallVec8 = llvm::SmallVector<Data, 8>;
template <typename Data> using SmallVec16 = llvm::SmallVector<Data, 16>;
template <typename Data> using SmallVec1024 = llvm::SmallVector<Data, 1024>;
template <typename Data> using FixedArray = FixedMaps::FixedArray<Data>;
template<class ContainerT, class ValueT>
void BM_vector_emplace_back(benchmark::State &state) {
for(auto _ : state)
{
ContainerT container;
container.reserve(state.range(0));
for(int i = 0; i < state.range(0); ++i)
{
container.emplace_back(generate_value<ValueT>(i));
benchmark::DoNotOptimize(container.data());
}
benchmark::ClobberMemory();
}
}
//BENCHMARK_TEMPLATES_4(BM_vector_emplace_back, vector, SmallVec8, SmallVec16, SmallVec1024);
template<class ContainerT, class ValueT>
void BM_vector_sequential_read(benchmark::State& state) {
std::vector<ValueT> client;
client.reserve(state.range(0));
for(int i = 0; i < state.range(0); ++i)
{
client.emplace_back(generate_value<ValueT>(i));
}
ContainerT container(client.begin(), client.end());
ValueT val;
for(auto _ : state)
{
for(const auto &item : container)
{
benchmark::DoNotOptimize(val = item);
}
benchmark::ClobberMemory();
}
}
BENCHMARK_TEMPLATES_5(BM_vector_sequential_read, std::vector, SmallVec8, SmallVec16, SmallVec1024, FixedArray);
template<class ContainerT, class ValueT>
void BM_vector_rand_read(benchmark::State& state) {
std::vector<ValueT> client;
client.reserve(state.range(0));
for(int i = 0; i < state.range(0); ++i)
{
client.emplace_back(generate_value<ValueT>(i));
}
const std::vector<int> &indices_to_lookup = randomize_lookup_indices(state.range(0));
ContainerT container(client.begin(), client.end());
ValueT val;
for(auto _ : state)
{
for(const auto &idx : indices_to_lookup)
{
benchmark::DoNotOptimize(val = *(container.begin() + idx));
}
benchmark::ClobberMemory();
}
}
BENCHMARK_TEMPLATES_5(BM_vector_rand_read, std::vector, SmallVec8, SmallVec16, SmallVec1024, FixedArray);
#endif // BENCHMARK_VECTORS
#if BENCHMARK_MAPS
template <typename Data> using std_map = std::map<intptr_t, Data>;
template <typename Data> using std_unordered_map = std::unordered_map<intptr_t, Data>;
template <typename Data> using DenseMap = llvm::DenseMap<intptr_t, Data>;
template <typename Data> using ArrayMap = FixedMaps::ArrayMap<intptr_t, Data>;
template<class ContainerT, class ValueT>
void BM_map_insert(benchmark::State &state) {
for(auto _ : state)
{
ContainerT container;
for(int i = 0; i < state.range(0); ++i)
{
container[i] = generate_value<ValueT>(i);
}
benchmark::ClobberMemory();
}
}
BENCHMARK_TEMPLATES_3(BM_map_insert, std_map, std_unordered_map, DenseMap);
template<class ContainerT, class ValueT>
void BM_map_lookup(benchmark::State &state) {
std::map<intptr_t, ValueT> client;
for(int i = 0; i < state.range(0); ++i)
{
client[i] = generate_value<ValueT>(i);
}
const std::vector<int> &keys_to_lookup = randomize_lookup_indices(state.range(0));
ContainerT container(client.begin(), client.end());
benchmark::ClobberMemory();
ValueT val;
for(auto _ : state)
{
for(const auto &key : keys_to_lookup)
{
benchmark::DoNotOptimize(val = container[key]);
}
benchmark::ClobberMemory();
}
}
BENCHMARK_TEMPLATES_4(BM_map_lookup, std_map, std_unordered_map, DenseMap, ArrayMap);
#endif // BENCHMARK_MAPS
#if BENCHMARK_SETS
template <typename Data> using SmallSet8 = llvm::SmallSet<Data, 8>;
template <typename Data> using SmallSet16 = llvm::SmallSet<Data, 16>;
template <typename Data> using ArraySet = FixedMaps::ArraySet<Data>;
template<class ContainerT, class ValueT>
void BM_set_insert(benchmark::State &state) {
for(auto _ : state)
{
ContainerT container;
for(int i = 0; i < state.range(0); ++i)
{
container.insert(generate_value<ValueT>(i));
}
benchmark::ClobberMemory();
}
}
BENCHMARK_TEMPLATES_3(BM_set_insert, std::set, SmallSet8, SmallSet16);
template<class ContainerT, class ValueT>
void BM_set_read(benchmark::State& state) {
std::vector<ValueT> vals_to_lookup;
vals_to_lookup.reserve(state.range(0));
std::set<ValueT> client;
for(int i = 0; i < state.range(0); ++i)
{
client.insert(generate_value<ValueT>(i));
vals_to_lookup.push_back(generate_value<ValueT>(i));
}
shuffle(vals_to_lookup.begin(), vals_to_lookup.end(), std::default_random_engine(0));
ContainerT container(client.begin(), client.end());
int count = 0;
for(auto _ : state)
{
for(const auto &val : vals_to_lookup)
{
benchmark::DoNotOptimize(count += container.count(val));
}
benchmark::ClobberMemory();
}
}
BENCHMARK_TEMPLATES_4(BM_set_read, std::set, SmallSet8, SmallSet16, ArraySet);
#endif // BENCHMARK_SETS
BENCHMARK_MAIN();
const std::vector<int> & randomize_lookup_indices(size_t size)
{
static llvm::DenseMap<int, std::vector<int>> s_existing_orderings;
auto it = s_existing_orderings.find(size);
if(it == s_existing_orderings.end())
{
std::vector<int> new_ordering;
new_ordering.reserve(size);
for(int i = 0; i < size; ++i)
{
new_ordering.push_back(i);
}
shuffle(new_ordering.begin(), new_ordering.end(), std::default_random_engine(0));
it = s_existing_orderings.insert(make_pair(size, new_ordering)).first;
}
return it->second;
}