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 <cstdint>
16 #include <random>
17 #include <string>
18 #include <type_traits>
19 #include <vector>
20 
21 #include "benchmark/benchmark.h"
22 #include "absl/base/internal/raw_logging.h"
23 #include "absl/random/distributions.h"
24 #include "absl/random/random.h"
25 #include "absl/strings/numbers.h"
26 
27 namespace {
28 
29 template <typename T>
BM_FastIntToBuffer(benchmark::State & state)30 void BM_FastIntToBuffer(benchmark::State& state) {
31   const int inc = state.range(0);
32   char buf[absl::numbers_internal::kFastToBufferSize];
33   // Use the unsigned type to increment to take advantage of well-defined
34   // modular arithmetic.
35   typename std::make_unsigned<T>::type x = 0;
36   for (auto _ : state) {
37     absl::numbers_internal::FastIntToBuffer(static_cast<T>(x), buf);
38     x += inc;
39   }
40 }
41 BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int32_t)->Range(0, 1 << 15);
42 BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int64_t)->Range(0, 1 << 30);
43 
44 // Creates an integer that would be printed as `num_digits` repeated 7s in the
45 // given `base`. `base` must be greater than or equal to 8.
RepeatedSevens(int num_digits,int base)46 int64_t RepeatedSevens(int num_digits, int base) {
47   ABSL_RAW_CHECK(base >= 8, "");
48   int64_t num = 7;
49   while (--num_digits) num = base * num + 7;
50   return num;
51 }
52 
BM_safe_strto32_string(benchmark::State & state)53 void BM_safe_strto32_string(benchmark::State& state) {
54   const int digits = state.range(0);
55   const int base = state.range(1);
56   std::string str(digits, '7');  // valid in octal, decimal and hex
57   int32_t value = 0;
58   for (auto _ : state) {
59     benchmark::DoNotOptimize(
60         absl::numbers_internal::safe_strto32_base(str, &value, base));
61   }
62   ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), "");
63 }
64 BENCHMARK(BM_safe_strto32_string)
65     ->ArgPair(1, 8)
66     ->ArgPair(1, 10)
67     ->ArgPair(1, 16)
68     ->ArgPair(2, 8)
69     ->ArgPair(2, 10)
70     ->ArgPair(2, 16)
71     ->ArgPair(4, 8)
72     ->ArgPair(4, 10)
73     ->ArgPair(4, 16)
74     ->ArgPair(8, 8)
75     ->ArgPair(8, 10)
76     ->ArgPair(8, 16)
77     ->ArgPair(10, 8)
78     ->ArgPair(9, 10);
79 
BM_safe_strto64_string(benchmark::State & state)80 void BM_safe_strto64_string(benchmark::State& state) {
81   const int digits = state.range(0);
82   const int base = state.range(1);
83   std::string str(digits, '7');  // valid in octal, decimal and hex
84   int64_t value = 0;
85   for (auto _ : state) {
86     benchmark::DoNotOptimize(
87         absl::numbers_internal::safe_strto64_base(str, &value, base));
88   }
89   ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), "");
90 }
91 BENCHMARK(BM_safe_strto64_string)
92     ->ArgPair(1, 8)
93     ->ArgPair(1, 10)
94     ->ArgPair(1, 16)
95     ->ArgPair(2, 8)
96     ->ArgPair(2, 10)
97     ->ArgPair(2, 16)
98     ->ArgPair(4, 8)
99     ->ArgPair(4, 10)
100     ->ArgPair(4, 16)
101     ->ArgPair(8, 8)
102     ->ArgPair(8, 10)
103     ->ArgPair(8, 16)
104     ->ArgPair(16, 8)
105     ->ArgPair(16, 10)
106     ->ArgPair(16, 16);
107 
BM_safe_strtou32_string(benchmark::State & state)108 void BM_safe_strtou32_string(benchmark::State& state) {
109   const int digits = state.range(0);
110   const int base = state.range(1);
111   std::string str(digits, '7');  // valid in octal, decimal and hex
112   uint32_t value = 0;
113   for (auto _ : state) {
114     benchmark::DoNotOptimize(
115         absl::numbers_internal::safe_strtou32_base(str, &value, base));
116   }
117   ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), "");
118 }
119 BENCHMARK(BM_safe_strtou32_string)
120     ->ArgPair(1, 8)
121     ->ArgPair(1, 10)
122     ->ArgPair(1, 16)
123     ->ArgPair(2, 8)
124     ->ArgPair(2, 10)
125     ->ArgPair(2, 16)
126     ->ArgPair(4, 8)
127     ->ArgPair(4, 10)
128     ->ArgPair(4, 16)
129     ->ArgPair(8, 8)
130     ->ArgPair(8, 10)
131     ->ArgPair(8, 16)
132     ->ArgPair(10, 8)
133     ->ArgPair(9, 10);
134 
BM_safe_strtou64_string(benchmark::State & state)135 void BM_safe_strtou64_string(benchmark::State& state) {
136   const int digits = state.range(0);
137   const int base = state.range(1);
138   std::string str(digits, '7');  // valid in octal, decimal and hex
139   uint64_t value = 0;
140   for (auto _ : state) {
141     benchmark::DoNotOptimize(
142         absl::numbers_internal::safe_strtou64_base(str, &value, base));
143   }
144   ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), "");
145 }
146 BENCHMARK(BM_safe_strtou64_string)
147     ->ArgPair(1, 8)
148     ->ArgPair(1, 10)
149     ->ArgPair(1, 16)
150     ->ArgPair(2, 8)
151     ->ArgPair(2, 10)
152     ->ArgPair(2, 16)
153     ->ArgPair(4, 8)
154     ->ArgPair(4, 10)
155     ->ArgPair(4, 16)
156     ->ArgPair(8, 8)
157     ->ArgPair(8, 10)
158     ->ArgPair(8, 16)
159     ->ArgPair(16, 8)
160     ->ArgPair(16, 10)
161     ->ArgPair(16, 16);
162 
163 // Returns a vector of `num_strings` strings. Each string represents a
164 // floating point number with `num_digits` digits before the decimal point and
165 // another `num_digits` digits after.
MakeFloatStrings(int num_strings,int num_digits)166 std::vector<std::string> MakeFloatStrings(int num_strings, int num_digits) {
167   // For convenience, use a random number generator to generate the test data.
168   // We don't actually need random properties, so use a fixed seed.
169   std::minstd_rand0 rng(1);
170   std::uniform_int_distribution<int> random_digit('0', '9');
171 
172   std::vector<std::string> float_strings(num_strings);
173   for (std::string& s : float_strings) {
174     s.reserve(2 * num_digits + 1);
175     for (int i = 0; i < num_digits; ++i) {
176       s.push_back(static_cast<char>(random_digit(rng)));
177     }
178     s.push_back('.');
179     for (int i = 0; i < num_digits; ++i) {
180       s.push_back(static_cast<char>(random_digit(rng)));
181     }
182   }
183   return float_strings;
184 }
185 
186 template <typename StringType>
GetStringAs(const std::string & s)187 StringType GetStringAs(const std::string& s) {
188   return static_cast<StringType>(s);
189 }
190 template <>
GetStringAs(const std::string & s)191 const char* GetStringAs<const char*>(const std::string& s) {
192   return s.c_str();
193 }
194 
195 template <typename StringType>
GetStringsAs(const std::vector<std::string> & strings)196 std::vector<StringType> GetStringsAs(const std::vector<std::string>& strings) {
197   std::vector<StringType> result;
198   result.reserve(strings.size());
199   for (const std::string& s : strings) {
200     result.push_back(GetStringAs<StringType>(s));
201   }
202   return result;
203 }
204 
205 template <typename T>
BM_SimpleAtof(benchmark::State & state)206 void BM_SimpleAtof(benchmark::State& state) {
207   const int num_strings = state.range(0);
208   const int num_digits = state.range(1);
209   std::vector<std::string> backing_strings =
210       MakeFloatStrings(num_strings, num_digits);
211   std::vector<T> inputs = GetStringsAs<T>(backing_strings);
212   float value;
213   for (auto _ : state) {
214     for (const T& input : inputs) {
215       benchmark::DoNotOptimize(absl::SimpleAtof(input, &value));
216     }
217   }
218 }
219 BENCHMARK_TEMPLATE(BM_SimpleAtof, absl::string_view)
220     ->ArgPair(10, 1)
221     ->ArgPair(10, 2)
222     ->ArgPair(10, 4)
223     ->ArgPair(10, 8);
224 BENCHMARK_TEMPLATE(BM_SimpleAtof, const char*)
225     ->ArgPair(10, 1)
226     ->ArgPair(10, 2)
227     ->ArgPair(10, 4)
228     ->ArgPair(10, 8);
229 BENCHMARK_TEMPLATE(BM_SimpleAtof, std::string)
230     ->ArgPair(10, 1)
231     ->ArgPair(10, 2)
232     ->ArgPair(10, 4)
233     ->ArgPair(10, 8);
234 
235 template <typename T>
BM_SimpleAtod(benchmark::State & state)236 void BM_SimpleAtod(benchmark::State& state) {
237   const int num_strings = state.range(0);
238   const int num_digits = state.range(1);
239   std::vector<std::string> backing_strings =
240       MakeFloatStrings(num_strings, num_digits);
241   std::vector<T> inputs = GetStringsAs<T>(backing_strings);
242   double value;
243   for (auto _ : state) {
244     for (const T& input : inputs) {
245       benchmark::DoNotOptimize(absl::SimpleAtod(input, &value));
246     }
247   }
248 }
249 BENCHMARK_TEMPLATE(BM_SimpleAtod, absl::string_view)
250     ->ArgPair(10, 1)
251     ->ArgPair(10, 2)
252     ->ArgPair(10, 4)
253     ->ArgPair(10, 8);
254 BENCHMARK_TEMPLATE(BM_SimpleAtod, const char*)
255     ->ArgPair(10, 1)
256     ->ArgPair(10, 2)
257     ->ArgPair(10, 4)
258     ->ArgPair(10, 8);
259 BENCHMARK_TEMPLATE(BM_SimpleAtod, std::string)
260     ->ArgPair(10, 1)
261     ->ArgPair(10, 2)
262     ->ArgPair(10, 4)
263     ->ArgPair(10, 8);
264 
BM_FastHexToBufferZeroPad16(benchmark::State & state)265 void BM_FastHexToBufferZeroPad16(benchmark::State& state) {
266   absl::BitGen rng;
267   std::vector<uint64_t> nums;
268   nums.resize(1000);
269   auto min = std::numeric_limits<uint64_t>::min();
270   auto max = std::numeric_limits<uint64_t>::max();
271   for (auto& num : nums) {
272     num = absl::LogUniform(rng, min, max);
273   }
274 
275   char buf[16];
276   while (state.KeepRunningBatch(nums.size())) {
277     for (auto num : nums) {
278       auto digits = absl::numbers_internal::FastHexToBufferZeroPad16(num, buf);
279       benchmark::DoNotOptimize(digits);
280       benchmark::DoNotOptimize(buf);
281     }
282   }
283 }
284 BENCHMARK(BM_FastHexToBufferZeroPad16);
285 
286 }  // namespace
287