Skip to content

Commit

Permalink
fix StringToFloatBenchmark without floating-point std::from_chars
Browse files Browse the repository at this point in the history
Summary: Some versions of libc++ still in use do not yet have floating-point overloads of `std::from_chars`, so the benchmark program must take steps to avoid compile failure.

Reviewed By: skrueger

Differential Revision: D64772779

fbshipit-source-id: c50b667e79f1c0c9ff87dd7aedb9aab008633ae8
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Oct 23, 2024
1 parent 472151b commit fc34259
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions folly/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ cpp_binary(
"fbsource//third-party/fast_float:fast_float",
"//folly:benchmark",
"//folly:stop_watch",
"//folly:traits",
],
external_deps = [
"double_conversion",
Expand Down
24 changes: 22 additions & 2 deletions folly/test/StringToFloatBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,33 @@
#include <cmath>
#include <iostream>
#include <random>
#include <utility>

#include <folly/Benchmark.h>
#include <folly/Traits.h>
#include <folly/stop_watch.h>

#include <double-conversion/double-conversion.h>
#include <fast_float/fast_float.h> // @manual=fbsource//third-party/fast_float:fast_float

template <typename T, typename... A>
using detect_std_from_chars = decltype(void(std::from_chars(
nullptr, nullptr, std::declval<T&>(), std::declval<A const&>()...)));

/// invoke_std_from_chars
///
/// Invokes std::from_chars if the requested overload is available.
///
/// Cannot use if-constexpr directly within benchmarks to accomplish this since
/// benchmarks are not function templates.
template <typename T, typename... A>
void invoke_std_from_chars(
char const* first, char const* last, T& value, A const&... a) {
if constexpr (folly::is_detected_v<detect_std_from_chars, T, A...>) {
std::from_chars(first, last, value, a...);
}
}

const char* kInputZero = "0";

/// An arbitrary hardcoded decimal number to benchmark
Expand Down Expand Up @@ -125,7 +145,7 @@ BENCHMARK(hardcoded_decimal_notation_STRTOFL_COPY, n) {
BENCHMARK(hardcoded_decimal_notation_STD_FROM_CHARS, n) {
double value{};
for (unsigned int i = 0; i < n; ++i) {
std::from_chars(
invoke_std_from_chars(
kDecimalStr.data(), kDecimalStr.data() + kDecimalStr.size(), value);
}
}
Expand Down Expand Up @@ -175,7 +195,7 @@ BENCHMARK(hardcoded_exponential_notation_STRTOFL_COPY, n) {
BENCHMARK(hardcoded_exponential_notation_STD_FROM_CHARS, n) {
for (unsigned int i = 0; i < n; ++i) {
double value{};
std::from_chars(
invoke_std_from_chars(
kexponentialNotationStr.data(),
kexponentialNotationStr.data() + kexponentialNotationStr.size(),
value);
Expand Down

0 comments on commit fc34259

Please sign in to comment.