forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_benchmark.cc
More file actions
454 lines (367 loc) · 13.7 KB
/
Copy pathbuilder_benchmark.cc
File metadata and controls
454 lines (367 loc) · 13.7 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <algorithm>
#include <cstdint>
#include <limits>
#include <numeric>
#include <random>
#include <string>
#include <string_view>
#include <vector>
#include "benchmark/benchmark.h"
#include "arrow/builder.h"
#include "arrow/memory_pool.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/decimal.h"
namespace arrow {
using ValueType = int64_t;
using VectorType = std::vector<ValueType>;
constexpr int64_t kNumberOfElements = 256 * 512;
static VectorType AlmostU8CompressibleVector() {
VectorType data(kNumberOfElements, 64);
// Insert an element late in the game that does not fit in the 8bit
// representation. This forces AdaptiveIntBuilder's to resize.
data[kNumberOfElements - 2] = 1L << 13;
return data;
}
constexpr int64_t kRounds = 256;
static VectorType kData = AlmostU8CompressibleVector();
constexpr int64_t kBytesProcessPerRound = kNumberOfElements * sizeof(ValueType);
constexpr int64_t kBytesProcessed = kRounds * kBytesProcessPerRound;
static const char* kBinaryString = "12345678";
static std::string_view kBinaryView(kBinaryString);
static void BuildIntArrayNoNulls(benchmark::State& state) { // NOLINT non-const reference
for (auto _ : state) {
Int64Builder builder;
for (int i = 0; i < kRounds; i++) {
ABORT_NOT_OK(builder.AppendValues(kData.data(), kData.size(), nullptr));
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildAdaptiveIntNoNulls(
benchmark::State& state) { // NOLINT non-const reference
for (auto _ : state) {
AdaptiveIntBuilder builder;
for (int i = 0; i < kRounds; i++) {
ABORT_NOT_OK(builder.AppendValues(kData.data(), kData.size(), nullptr));
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildAdaptiveIntNoNullsScalarAppend(
benchmark::State& state) { // NOLINT non-const reference
for (auto _ : state) {
AdaptiveIntBuilder builder;
for (int i = 0; i < kRounds; i++) {
for (size_t j = 0; j < kData.size(); j++) {
ABORT_NOT_OK(builder.Append(kData[i]))
}
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildBooleanArrayNoNulls(
benchmark::State& state) { // NOLINT non-const reference
size_t n_bytes = kBytesProcessPerRound;
const uint8_t* data = reinterpret_cast<const uint8_t*>(kData.data());
for (auto _ : state) {
BooleanBuilder builder;
for (int i = 0; i < kRounds; i++) {
ABORT_NOT_OK(builder.AppendValues(data, n_bytes));
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildBinaryArray(benchmark::State& state) { // NOLINT non-const reference
for (auto _ : state) {
BinaryBuilder builder;
for (int64_t i = 0; i < kRounds * kNumberOfElements; i++) {
ABORT_NOT_OK(builder.Append(kBinaryView));
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildChunkedBinaryArray(
benchmark::State& state) { // NOLINT non-const reference
// 1MB chunks
const int32_t kChunkSize = 1 << 20;
for (auto _ : state) {
internal::ChunkedBinaryBuilder builder(kChunkSize);
for (int64_t i = 0; i < kRounds * kNumberOfElements; i++) {
ABORT_NOT_OK(builder.Append(kBinaryView));
}
ArrayVector out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildFixedSizeBinaryArray(
benchmark::State& state) { // NOLINT non-const reference
auto type = fixed_size_binary(static_cast<int32_t>(kBinaryView.size()));
for (auto _ : state) {
FixedSizeBinaryBuilder builder(type);
for (int64_t i = 0; i < kRounds * kNumberOfElements; i++) {
ABORT_NOT_OK(builder.Append(kBinaryView));
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
static void BuildDecimalArray(benchmark::State& state) { // NOLINT non-const reference
auto type = decimal(10, 5);
Decimal128 value;
int32_t precision = 0;
int32_t scale = 0;
ABORT_NOT_OK(Decimal128::FromString("1234.1234", &value, &precision, &scale));
for (auto _ : state) {
Decimal128Builder builder(type);
for (int64_t i = 0; i < kRounds * kNumberOfElements; i++) {
ABORT_NOT_OK(builder.Append(value));
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
state.SetBytesProcessed(state.iterations() * kRounds * kNumberOfElements * 16);
}
// ----------------------------------------------------------------------
// DictionaryBuilder benchmarks
size_t kDistinctElements = kNumberOfElements / 100;
// Testing with different distributions of integer values helps stress
// the hash table's robustness.
// Make a vector out of `n_distinct` sequential int values
template <class Integer = ValueType>
static std::vector<Integer> MakeSequentialIntDictFodder() {
std::default_random_engine gen(42);
std::vector<Integer> values(kNumberOfElements);
{
std::uniform_int_distribution<Integer> values_dist(0, kDistinctElements - 1);
std::generate(values.begin(), values.end(), [&]() { return values_dist(gen); });
}
return values;
}
// Make a vector out of `n_distinct` int values with potentially colliding hash
// entries as only their highest bits differ.
template <class Integer = ValueType>
static std::vector<Integer> MakeSimilarIntDictFodder() {
std::default_random_engine gen(42);
std::vector<Integer> values(kNumberOfElements);
{
std::uniform_int_distribution<Integer> values_dist(0, kDistinctElements - 1);
auto max_int = std::numeric_limits<Integer>::max();
auto multiplier =
static_cast<Integer>(bit_util::NextPower2(max_int / kDistinctElements / 2));
std::generate(values.begin(), values.end(),
[&]() { return multiplier * values_dist(gen); });
}
return values;
}
// Make a vector out of `n_distinct` random int values
template <class Integer = ValueType>
static std::vector<Integer> MakeRandomIntDictFodder() {
std::default_random_engine gen(42);
std::vector<Integer> values_dict(kDistinctElements);
std::vector<Integer> values(kNumberOfElements);
{
std::uniform_int_distribution<Integer> values_dist(
0, std::numeric_limits<Integer>::max());
std::generate(values_dict.begin(), values_dict.end(),
[&]() { return static_cast<Integer>(values_dist(gen)); });
}
{
std::uniform_int_distribution<int32_t> indices_dist(
0, static_cast<int32_t>(kDistinctElements - 1));
std::generate(values.begin(), values.end(),
[&]() { return values_dict[indices_dist(gen)]; });
}
return values;
}
// Make a vector out of `kDistinctElements` string values
static std::vector<std::string> MakeStringDictFodder() {
std::default_random_engine gen(42);
std::vector<std::string> values_dict(kDistinctElements);
std::vector<std::string> values(kNumberOfElements);
{
auto it = values_dict.begin();
// Add empty string
*it++ = "";
// Add a few similar strings
*it++ = "abc";
*it++ = "abcdef";
*it++ = "abcfgh";
// Add random strings
std::uniform_int_distribution<int32_t> length_dist(2, 20);
std::independent_bits_engine<std::default_random_engine, 8, uint16_t> bytes_gen(42);
std::generate(it, values_dict.end(), [&] {
auto length = length_dist(gen);
std::string s(length, 'X');
for (int32_t i = 0; i < length; ++i) {
s[i] = static_cast<char>(bytes_gen());
}
return s;
});
}
{
std::uniform_int_distribution<int32_t> indices_dist(
0, static_cast<int32_t>(kDistinctElements - 1));
std::generate(values.begin(), values.end(),
[&] { return values_dict[indices_dist(gen)]; });
}
return values;
}
template <class DictionaryBuilderType, class Scalar>
static void BenchmarkDictionaryArray(
benchmark::State& state, // NOLINT non-const reference
const std::vector<Scalar>& fodder, size_t fodder_nbytes = 0) {
for (auto _ : state) {
DictionaryBuilderType builder(default_memory_pool());
for (int64_t i = 0; i < kRounds; i++) {
for (const auto& value : fodder) {
ABORT_NOT_OK(builder.Append(value));
}
}
std::shared_ptr<Array> out;
ABORT_NOT_OK(builder.Finish(&out));
}
if (fodder_nbytes == 0) {
fodder_nbytes = fodder.size() * sizeof(Scalar);
}
state.SetBytesProcessed(state.iterations() * fodder_nbytes * kRounds);
}
static void BuildInt64DictionaryArrayRandom(
benchmark::State& state) { // NOLINT non-const reference
const auto fodder = MakeRandomIntDictFodder();
BenchmarkDictionaryArray<DictionaryBuilder<Int64Type>>(state, fodder);
}
static void BuildInt64DictionaryArraySequential(
benchmark::State& state) { // NOLINT non-const reference
const auto fodder = MakeSequentialIntDictFodder();
BenchmarkDictionaryArray<DictionaryBuilder<Int64Type>>(state, fodder);
}
static void BuildInt64DictionaryArraySimilar(
benchmark::State& state) { // NOLINT non-const reference
const auto fodder = MakeSimilarIntDictFodder();
BenchmarkDictionaryArray<DictionaryBuilder<Int64Type>>(state, fodder);
}
static void BuildStringDictionaryArray(
benchmark::State& state) { // NOLINT non-const reference
const auto fodder = MakeStringDictFodder();
auto fodder_nbytes =
std::accumulate(fodder.begin(), fodder.end(), 0ULL,
[&](size_t acc, const std::string& s) { return acc + s.size(); });
BenchmarkDictionaryArray<BinaryDictionaryBuilder>(state, fodder, fodder_nbytes);
}
static void ArrayDataConstructDestruct(
benchmark::State& state) { // NOLINT non-const reference
std::vector<std::shared_ptr<ArrayData>> arrays;
const int kNumArrays = 1000;
auto InitArrays = [&]() {
for (int i = 0; i < kNumArrays; ++i) {
arrays.emplace_back(new ArrayData);
}
};
for (auto _ : state) {
InitArrays();
arrays.clear();
}
}
// ----------------------------------------------------------------------
// BufferBuilder benchmarks
static void BenchmarkBufferBuilder(
const std::string& datum,
benchmark::State& state) { // NOLINT non-const reference
const void* raw_data = datum.data();
int64_t raw_nbytes = static_cast<int64_t>(datum.size());
// Write approx. 256 MB to BufferBuilder
int64_t num_raw_values = (1 << 28) / raw_nbytes;
for (auto _ : state) {
BufferBuilder builder;
std::shared_ptr<Buffer> buf;
for (int64_t i = 0; i < num_raw_values; ++i) {
ABORT_NOT_OK(builder.Append(raw_data, raw_nbytes));
}
ABORT_NOT_OK(builder.Finish(&buf));
}
state.SetBytesProcessed(int64_t(state.iterations()) * num_raw_values * raw_nbytes);
}
static void BufferBuilderTinyWrites(
benchmark::State& state) { // NOLINT non-const reference
// A 8-byte datum
return BenchmarkBufferBuilder("abdefghi", state);
}
static void BufferBuilderSmallWrites(
benchmark::State& state) { // NOLINT non-const reference
// A 700-byte datum
std::string datum;
for (int i = 0; i < 100; ++i) {
datum += "abcdefg";
}
return BenchmarkBufferBuilder(datum, state);
}
static void BufferBuilderLargeWrites(
benchmark::State& state) { // NOLINT non-const reference
// A 1.5MB datum
std::string datum(1500000, 'x');
return BenchmarkBufferBuilder(datum, state);
}
BENCHMARK(BufferBuilderTinyWrites)->UseRealTime();
BENCHMARK(BufferBuilderSmallWrites)->UseRealTime();
BENCHMARK(BufferBuilderLargeWrites)->UseRealTime();
// ----------------------------------------------------------------------
// Benchmark declarations
//
#ifdef ARROW_WITH_BENCHMARKS_REFERENCE
// This benchmarks acts as a reference to the native std::vector
// implementation. It appends kRounds chunks into a vector.
static void ReferenceBuildVectorNoNulls(
benchmark::State& state) { // NOLINT non-const reference
for (auto _ : state) {
std::vector<int64_t> builder;
for (int i = 0; i < kRounds; i++) {
builder.insert(builder.end(), kData.cbegin(), kData.cend());
}
}
state.SetBytesProcessed(state.iterations() * kBytesProcessed);
}
BENCHMARK(ReferenceBuildVectorNoNulls);
#endif
BENCHMARK(BuildBooleanArrayNoNulls);
BENCHMARK(BuildIntArrayNoNulls);
BENCHMARK(BuildAdaptiveIntNoNulls);
BENCHMARK(BuildAdaptiveIntNoNullsScalarAppend);
BENCHMARK(BuildBinaryArray);
BENCHMARK(BuildChunkedBinaryArray);
BENCHMARK(BuildFixedSizeBinaryArray);
BENCHMARK(BuildDecimalArray);
BENCHMARK(BuildInt64DictionaryArrayRandom);
BENCHMARK(BuildInt64DictionaryArraySequential);
BENCHMARK(BuildInt64DictionaryArraySimilar);
BENCHMARK(BuildStringDictionaryArray);
BENCHMARK(ArrayDataConstructDestruct);
} // namespace arrow