]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/util/int_util_benchmark.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / util / int_util_benchmark.cc
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include "benchmark/benchmark.h"
19
20 #include <cstdint>
21 #include <vector>
22
23 #include "arrow/array/array_base.h"
24 #include "arrow/testing/gtest_util.h"
25 #include "arrow/testing/random.h"
26 #include "arrow/util/benchmark_util.h"
27 #include "arrow/util/int_util.h"
28
29 namespace arrow {
30 namespace internal {
31
32 constexpr auto kSeed = 0x94378165;
33
34 std::vector<uint64_t> GetUIntSequence(int n_values, uint64_t addend = 0) {
35 std::vector<uint64_t> values(n_values);
36 for (int i = 0; i < n_values; ++i) {
37 values[i] = static_cast<uint64_t>(i) + addend;
38 }
39 return values;
40 }
41
42 std::vector<int64_t> GetIntSequence(int n_values, uint64_t addend = 0) {
43 std::vector<int64_t> values(n_values);
44 for (int i = 0; i < n_values; ++i) {
45 values[i] = static_cast<int64_t>(i) + addend;
46 }
47 return values;
48 }
49
50 std::vector<uint8_t> GetValidBytes(int n_values) {
51 std::vector<uint8_t> valid_bytes(n_values);
52 for (int i = 0; i < n_values; ++i) {
53 valid_bytes[i] = (i % 3 == 0) ? 1 : 0;
54 }
55 return valid_bytes;
56 }
57
58 static void DetectUIntWidthNoNulls(
59 benchmark::State& state) { // NOLINT non-const reference
60 const auto values = GetUIntSequence(0x12345);
61
62 while (state.KeepRunning()) {
63 auto result = DetectUIntWidth(values.data(), static_cast<int64_t>(values.size()));
64 benchmark::DoNotOptimize(result);
65 }
66 state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
67 }
68
69 static void DetectUIntWidthNulls(benchmark::State& state) { // NOLINT non-const reference
70 const auto values = GetUIntSequence(0x12345);
71 const auto valid_bytes = GetValidBytes(0x12345);
72
73 while (state.KeepRunning()) {
74 auto result = DetectUIntWidth(values.data(), valid_bytes.data(),
75 static_cast<int64_t>(values.size()));
76 benchmark::DoNotOptimize(result);
77 }
78 state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
79 }
80
81 static void DetectIntWidthNoNulls(
82 benchmark::State& state) { // NOLINT non-const reference
83 const auto values = GetIntSequence(0x12345, -0x1234);
84
85 while (state.KeepRunning()) {
86 auto result = DetectIntWidth(values.data(), static_cast<int64_t>(values.size()));
87 benchmark::DoNotOptimize(result);
88 }
89 state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
90 }
91
92 static void DetectIntWidthNulls(benchmark::State& state) { // NOLINT non-const reference
93 const auto values = GetIntSequence(0x12345, -0x1234);
94 const auto valid_bytes = GetValidBytes(0x12345);
95
96 while (state.KeepRunning()) {
97 auto result = DetectIntWidth(values.data(), valid_bytes.data(),
98 static_cast<int64_t>(values.size()));
99 benchmark::DoNotOptimize(result);
100 }
101 state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
102 }
103
104 static void CheckIndexBoundsInt32(
105 benchmark::State& state) { // NOLINT non-const reference
106 GenericItemsArgs args(state);
107 random::RandomArrayGenerator rand(kSeed);
108 auto arr = rand.Int32(args.size, 0, 100000, args.null_proportion);
109 for (auto _ : state) {
110 ABORT_NOT_OK(CheckIndexBounds(*arr->data(), 100001));
111 }
112 }
113
114 static void CheckIndexBoundsUInt32(
115 benchmark::State& state) { // NOLINT non-const reference
116 GenericItemsArgs args(state);
117 random::RandomArrayGenerator rand(kSeed);
118 auto arr = rand.UInt32(args.size, 0, 100000, args.null_proportion);
119 for (auto _ : state) {
120 ABORT_NOT_OK(CheckIndexBounds(*arr->data(), 100001));
121 }
122 }
123
124 BENCHMARK(DetectUIntWidthNoNulls);
125 BENCHMARK(DetectUIntWidthNulls);
126 BENCHMARK(DetectIntWidthNoNulls);
127 BENCHMARK(DetectIntWidthNulls);
128
129 std::vector<int64_t> g_data_sizes = {kL1Size, kL2Size};
130
131 void BoundsCheckSetArgs(benchmark::internal::Benchmark* bench) {
132 for (int64_t size : g_data_sizes) {
133 for (auto nulls : std::vector<ArgsType>({1000, 10, 2, 1, 0})) {
134 bench->Args({static_cast<ArgsType>(size), nulls});
135 }
136 }
137 }
138
139 BENCHMARK(CheckIndexBoundsInt32)->Apply(BoundsCheckSetArgs);
140 BENCHMARK(CheckIndexBoundsUInt32)->Apply(BoundsCheckSetArgs);
141
142 } // namespace internal
143 } // namespace arrow