From be1e84b988fceabcea4fc9e93f899539f0c81901 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 27 Jun 2018 10:58:26 -0700 Subject: Export of internal Abseil changes. -- 6befc69900cd77b5af55d1235b2c74dbeb37fbeb by Alex Strelnikov : move ExtendedParsedFormat PiperOrigin-RevId: 202336028 -- 3adf2a6358189b28e9bdf982c4fd3110e24fb84b by Alex Strelnikov : Internal change. PiperOrigin-RevId: 202330527 -- 8fdd1e8aee74992f5d4f3ab79739e6272feca7a8 by Alex Strelnikov : move ExtendedParsedFormat PiperOrigin-RevId: 202317418 -- c618725b2320d5c4292ea479b877c848ccdf2e6b by Alex Strelnikov : Release strings:numbers_benchmark PiperOrigin-RevId: 202297686 -- 7c9f6132034a5a32160f56ca0dd0a4edcd476928 by Matt Kulukundis : Internal change PiperOrigin-RevId: 202230893 -- bf1a0e1724e87e4fb55165997562efa9c8d081c5 by Juemin Yang : move ExtendedParsedFormat PiperOrigin-RevId: 202199857 GitOrigin-RevId: 6befc69900cd77b5af55d1235b2c74dbeb37fbeb Change-Id: Iafe46260e141de9f34b130b9d968a0ea542151d1 --- absl/strings/BUILD.bazel | 13 ++ absl/strings/numbers_benchmark.cc | 263 ++++++++++++++++++++++++++++++++++++++ absl/strings/str_format.h | 2 - 3 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 absl/strings/numbers_benchmark.cc (limited to 'absl') diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index 17831c208230..3b1e067597e2 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -408,6 +408,19 @@ cc_test( ], ) +cc_test( + name = "numbers_benchmark", + srcs = ["numbers_benchmark.cc"], + copts = ABSL_TEST_COPTS, + tags = ["benchmark"], + visibility = ["//visibility:private"], + deps = [ + ":strings", + "//absl/base", + "@com_github_google_benchmark//:benchmark_main", + ], +) + cc_test( name = "strip_test", size = "small", diff --git a/absl/strings/numbers_benchmark.cc b/absl/strings/numbers_benchmark.cc new file mode 100644 index 000000000000..8ef650b9a3ec --- /dev/null +++ b/absl/strings/numbers_benchmark.cc @@ -0,0 +1,263 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed 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 +#include +#include +#include +#include + +#include "benchmark/benchmark.h" +#include "absl/base/internal/raw_logging.h" +#include "absl/strings/numbers.h" + +namespace { + +template +void BM_FastIntToBuffer(benchmark::State& state) { + const int inc = state.range(0); + char buf[absl::numbers_internal::kFastToBufferSize]; + // Use the unsigned type to increment to take advantage of well-defined + // modular arithmetic. + typename std::make_unsigned::type x = 0; + for (auto _ : state) { + absl::numbers_internal::FastIntToBuffer(static_cast(x), buf); + x += inc; + } +} +BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int32_t)->Range(0, 1 << 15); +BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int64_t)->Range(0, 1 << 30); + +// Creates an integer that would be printed as `num_digits` repeated 7s in the +// given `base`. `base` must be greater than or equal to 8. +int64_t RepeatedSevens(int num_digits, int base) { + ABSL_RAW_CHECK(base >= 8, ""); + int64_t num = 7; + while (--num_digits) num = base * num + 7; + return num; +} + +void BM_safe_strto32_string(benchmark::State& state) { + const int digits = state.range(0); + const int base = state.range(1); + std::string str(digits, '7'); // valid in octal, decimal and hex + int32_t value = 0; + for (auto _ : state) { + benchmark::DoNotOptimize( + absl::numbers_internal::safe_strto32_base(str, &value, base)); + } + ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), ""); +} +BENCHMARK(BM_safe_strto32_string) + ->ArgPair(1, 8) + ->ArgPair(1, 10) + ->ArgPair(1, 16) + ->ArgPair(2, 8) + ->ArgPair(2, 10) + ->ArgPair(2, 16) + ->ArgPair(4, 8) + ->ArgPair(4, 10) + ->ArgPair(4, 16) + ->ArgPair(8, 8) + ->ArgPair(8, 10) + ->ArgPair(8, 16) + ->ArgPair(10, 8) + ->ArgPair(9, 10); + +void BM_safe_strto64_string(benchmark::State& state) { + const int digits = state.range(0); + const int base = state.range(1); + std::string str(digits, '7'); // valid in octal, decimal and hex + int64_t value = 0; + for (auto _ : state) { + benchmark::DoNotOptimize( + absl::numbers_internal::safe_strto64_base(str, &value, base)); + } + ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), ""); +} +BENCHMARK(BM_safe_strto64_string) + ->ArgPair(1, 8) + ->ArgPair(1, 10) + ->ArgPair(1, 16) + ->ArgPair(2, 8) + ->ArgPair(2, 10) + ->ArgPair(2, 16) + ->ArgPair(4, 8) + ->ArgPair(4, 10) + ->ArgPair(4, 16) + ->ArgPair(8, 8) + ->ArgPair(8, 10) + ->ArgPair(8, 16) + ->ArgPair(16, 8) + ->ArgPair(16, 10) + ->ArgPair(16, 16); + +void BM_safe_strtou32_string(benchmark::State& state) { + const int digits = state.range(0); + const int base = state.range(1); + std::string str(digits, '7'); // valid in octal, decimal and hex + uint32_t value = 0; + for (auto _ : state) { + benchmark::DoNotOptimize( + absl::numbers_internal::safe_strtou32_base(str, &value, base)); + } + ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), ""); +} +BENCHMARK(BM_safe_strtou32_string) + ->ArgPair(1, 8) + ->ArgPair(1, 10) + ->ArgPair(1, 16) + ->ArgPair(2, 8) + ->ArgPair(2, 10) + ->ArgPair(2, 16) + ->ArgPair(4, 8) + ->ArgPair(4, 10) + ->ArgPair(4, 16) + ->ArgPair(8, 8) + ->ArgPair(8, 10) + ->ArgPair(8, 16) + ->ArgPair(10, 8) + ->ArgPair(9, 10); + +void BM_safe_strtou64_string(benchmark::State& state) { + const int digits = state.range(0); + const int base = state.range(1); + std::string str(digits, '7'); // valid in octal, decimal and hex + uint64_t value = 0; + for (auto _ : state) { + benchmark::DoNotOptimize( + absl::numbers_internal::safe_strtou64_base(str, &value, base)); + } + ABSL_RAW_CHECK(value == RepeatedSevens(digits, base), ""); +} +BENCHMARK(BM_safe_strtou64_string) + ->ArgPair(1, 8) + ->ArgPair(1, 10) + ->ArgPair(1, 16) + ->ArgPair(2, 8) + ->ArgPair(2, 10) + ->ArgPair(2, 16) + ->ArgPair(4, 8) + ->ArgPair(4, 10) + ->ArgPair(4, 16) + ->ArgPair(8, 8) + ->ArgPair(8, 10) + ->ArgPair(8, 16) + ->ArgPair(16, 8) + ->ArgPair(16, 10) + ->ArgPair(16, 16); + +// Returns a vector of `num_strings` strings. Each std::string represents a +// floating point number with `num_digits` digits before the decimal point and +// another `num_digits` digits after. +std::vector MakeFloatStrings(int num_strings, int num_digits) { + // For convenience, use a random number generator to generate the test data. + // We don't actually need random properties, so use a fixed seed. + std::minstd_rand0 rng(1); + std::uniform_int_distribution random_digit('0', '9'); + + std::vector float_strings(num_strings); + for (std::string& s : float_strings) { + s.reserve(2 * num_digits + 1); + for (int i = 0; i < num_digits; ++i) { + s.push_back(static_cast(random_digit(rng))); + } + s.push_back('.'); + for (int i = 0; i < num_digits; ++i) { + s.push_back(static_cast(random_digit(rng))); + } + } + return float_strings; +} + +template +StringType GetStringAs(const std::string& s) { + return static_cast(s); +} +template <> +const char* GetStringAs(const std::string& s) { + return s.c_str(); +} + +template +std::vector GetStringsAs(const std::vector& strings) { + std::vector result; + result.reserve(strings.size()); + for (const std::string& s : strings) { + result.push_back(GetStringAs(s)); + } + return result; +} + +template +void BM_SimpleAtof(benchmark::State& state) { + const int num_strings = state.range(0); + const int num_digits = state.range(1); + std::vector backing_strings = + MakeFloatStrings(num_strings, num_digits); + std::vector inputs = GetStringsAs(backing_strings); + float value; + for (auto _ : state) { + for (const T& input : inputs) { + benchmark::DoNotOptimize(absl::SimpleAtof(input, &value)); + } + } +} +BENCHMARK_TEMPLATE(BM_SimpleAtof, absl::string_view) + ->ArgPair(10, 1) + ->ArgPair(10, 2) + ->ArgPair(10, 4) + ->ArgPair(10, 8); +BENCHMARK_TEMPLATE(BM_SimpleAtof, const char*) + ->ArgPair(10, 1) + ->ArgPair(10, 2) + ->ArgPair(10, 4) + ->ArgPair(10, 8); +BENCHMARK_TEMPLATE(BM_SimpleAtof, std::string) + ->ArgPair(10, 1) + ->ArgPair(10, 2) + ->ArgPair(10, 4) + ->ArgPair(10, 8); + +template +void BM_SimpleAtod(benchmark::State& state) { + const int num_strings = state.range(0); + const int num_digits = state.range(1); + std::vector backing_strings = + MakeFloatStrings(num_strings, num_digits); + std::vector inputs = GetStringsAs(backing_strings); + double value; + for (auto _ : state) { + for (const T& input : inputs) { + benchmark::DoNotOptimize(absl::SimpleAtod(input, &value)); + } + } +} +BENCHMARK_TEMPLATE(BM_SimpleAtod, absl::string_view) + ->ArgPair(10, 1) + ->ArgPair(10, 2) + ->ArgPair(10, 4) + ->ArgPair(10, 8); +BENCHMARK_TEMPLATE(BM_SimpleAtod, const char*) + ->ArgPair(10, 1) + ->ArgPair(10, 2) + ->ArgPair(10, 4) + ->ArgPair(10, 8); +BENCHMARK_TEMPLATE(BM_SimpleAtod, std::string) + ->ArgPair(10, 1) + ->ArgPair(10, 2) + ->ArgPair(10, 4) + ->ArgPair(10, 8); + +} // namespace diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index a3fb89c79581..70a811b75e2f 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -244,8 +244,6 @@ template using FormatSpec = typename str_format_internal::FormatSpecDeductionBarrier::type; -using absl::str_format_internal::ExtendedParsedFormat; - // ParsedFormat // // A `ParsedFormat` is a class template representing a preparsed `FormatSpec`, -- cgit 1.4.1