xref: /aosp_15_r20/external/abseil-cpp/absl/strings/str_split_benchmark.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/str_split.h"
16 
17 #include <cstddef>
18 #include <iterator>
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include "benchmark/benchmark.h"
25 #include "absl/base/internal/raw_logging.h"
26 #include "absl/strings/string_view.h"
27 
28 namespace {
29 
MakeTestString(int desired_length)30 std::string MakeTestString(int desired_length) {
31   static const int kAverageValueLen = 25;
32   std::string test(desired_length * kAverageValueLen, 'x');
33   for (int i = 1; i < test.size(); i += kAverageValueLen) {
34     test[i] = ';';
35   }
36   return test;
37 }
38 
BM_Split2StringView(benchmark::State & state)39 void BM_Split2StringView(benchmark::State& state) {
40   std::string test = MakeTestString(state.range(0));
41   for (auto _ : state) {
42     std::vector<absl::string_view> result = absl::StrSplit(test, ';');
43     benchmark::DoNotOptimize(result);
44   }
45 }
46 BENCHMARK_RANGE(BM_Split2StringView, 0, 1 << 20);
47 
48 static const absl::string_view kDelimiters = ";:,.";
49 
MakeMultiDelimiterTestString(int desired_length)50 std::string MakeMultiDelimiterTestString(int desired_length) {
51   static const int kAverageValueLen = 25;
52   std::string test(desired_length * kAverageValueLen, 'x');
53   for (int i = 0; i * kAverageValueLen < test.size(); ++i) {
54     // Cycle through a variety of delimiters.
55     test[i * kAverageValueLen] = kDelimiters[i % kDelimiters.size()];
56   }
57   return test;
58 }
59 
60 // Measure StrSplit with ByAnyChar with four delimiters to choose from.
BM_Split2StringViewByAnyChar(benchmark::State & state)61 void BM_Split2StringViewByAnyChar(benchmark::State& state) {
62   std::string test = MakeMultiDelimiterTestString(state.range(0));
63   for (auto _ : state) {
64     std::vector<absl::string_view> result =
65         absl::StrSplit(test, absl::ByAnyChar(kDelimiters));
66     benchmark::DoNotOptimize(result);
67   }
68 }
69 BENCHMARK_RANGE(BM_Split2StringViewByAnyChar, 0, 1 << 20);
70 
BM_Split2StringViewLifted(benchmark::State & state)71 void BM_Split2StringViewLifted(benchmark::State& state) {
72   std::string test = MakeTestString(state.range(0));
73   std::vector<absl::string_view> result;
74   for (auto _ : state) {
75     result = absl::StrSplit(test, ';');
76   }
77   benchmark::DoNotOptimize(result);
78 }
79 BENCHMARK_RANGE(BM_Split2StringViewLifted, 0, 1 << 20);
80 
BM_Split2String(benchmark::State & state)81 void BM_Split2String(benchmark::State& state) {
82   std::string test = MakeTestString(state.range(0));
83   for (auto _ : state) {
84     std::vector<std::string> result = absl::StrSplit(test, ';');
85     benchmark::DoNotOptimize(result);
86   }
87 }
88 BENCHMARK_RANGE(BM_Split2String, 0, 1 << 20);
89 
90 // This benchmark is for comparing Split2 to Split1 (SplitStringUsing). In
91 // particular, this benchmark uses SkipEmpty() to match SplitStringUsing's
92 // behavior.
BM_Split2SplitStringUsing(benchmark::State & state)93 void BM_Split2SplitStringUsing(benchmark::State& state) {
94   std::string test = MakeTestString(state.range(0));
95   for (auto _ : state) {
96     std::vector<std::string> result =
97         absl::StrSplit(test, ';', absl::SkipEmpty());
98     benchmark::DoNotOptimize(result);
99   }
100 }
101 BENCHMARK_RANGE(BM_Split2SplitStringUsing, 0, 1 << 20);
102 
BM_SplitStringToUnorderedSet(benchmark::State & state)103 void BM_SplitStringToUnorderedSet(benchmark::State& state) {
104   const int len = state.range(0);
105   std::string test(len, 'x');
106   for (int i = 1; i < len; i += 2) {
107     test[i] = ';';
108   }
109   for (auto _ : state) {
110     std::unordered_set<std::string> result =
111         absl::StrSplit(test, ':', absl::SkipEmpty());
112     benchmark::DoNotOptimize(result);
113   }
114 }
115 BENCHMARK_RANGE(BM_SplitStringToUnorderedSet, 0, 1 << 20);
116 
BM_SplitStringToUnorderedMap(benchmark::State & state)117 void BM_SplitStringToUnorderedMap(benchmark::State& state) {
118   const int len = state.range(0);
119   std::string test(len, 'x');
120   for (int i = 1; i < len; i += 2) {
121     test[i] = ';';
122   }
123   for (auto _ : state) {
124     std::unordered_map<std::string, std::string> result =
125         absl::StrSplit(test, ':', absl::SkipEmpty());
126     benchmark::DoNotOptimize(result);
127   }
128 }
129 BENCHMARK_RANGE(BM_SplitStringToUnorderedMap, 0, 1 << 20);
130 
BM_SplitStringAllowEmpty(benchmark::State & state)131 void BM_SplitStringAllowEmpty(benchmark::State& state) {
132   const int len = state.range(0);
133   std::string test(len, 'x');
134   for (int i = 1; i < len; i += 2) {
135     test[i] = ';';
136   }
137   for (auto _ : state) {
138     std::vector<std::string> result = absl::StrSplit(test, ';');
139     benchmark::DoNotOptimize(result);
140   }
141 }
142 BENCHMARK_RANGE(BM_SplitStringAllowEmpty, 0, 1 << 20);
143 
144 struct OneCharLiteral {
operator ()__anon089010420111::OneCharLiteral145   char operator()() const { return 'X'; }
146 };
147 
148 struct OneCharStringLiteral {
operator ()__anon089010420111::OneCharStringLiteral149   const char* operator()() const { return "X"; }
150 };
151 
152 template <typename DelimiterFactory>
BM_SplitStringWithOneChar(benchmark::State & state)153 void BM_SplitStringWithOneChar(benchmark::State& state) {
154   const auto delimiter = DelimiterFactory()();
155   std::vector<absl::string_view> pieces;
156   size_t v = 0;
157   for (auto _ : state) {
158     pieces = absl::StrSplit("The quick brown fox jumps over the lazy dog",
159                             delimiter);
160     v += pieces.size();
161   }
162   ABSL_RAW_CHECK(v == state.iterations(), "");
163 }
164 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharLiteral);
165 BENCHMARK_TEMPLATE(BM_SplitStringWithOneChar, OneCharStringLiteral);
166 
167 template <typename DelimiterFactory>
BM_SplitStringWithOneCharNoVector(benchmark::State & state)168 void BM_SplitStringWithOneCharNoVector(benchmark::State& state) {
169   const auto delimiter = DelimiterFactory()();
170   size_t v = 0;
171   for (auto _ : state) {
172     auto splitter = absl::StrSplit(
173         "The quick brown fox jumps over the lazy dog", delimiter);
174     v += std::distance(splitter.begin(), splitter.end());
175   }
176   ABSL_RAW_CHECK(v == state.iterations(), "");
177 }
178 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharLiteral);
179 BENCHMARK_TEMPLATE(BM_SplitStringWithOneCharNoVector, OneCharStringLiteral);
180 
181 }  // namespace
182