Skip to content

Commit

Permalink
buffer: use compiler version rather than other platform ifdefs to det…
Browse files Browse the repository at this point in the history
…ermine library call to use to serialize doubles (envoyproxy#29532)

Signed-off-by: Joshua Marantz <[email protected]>
  • Loading branch information
jmarantz authored Sep 11, 2023
1 parent accc571 commit 154219f
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions source/common/buffer/buffer_util.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "source/common/buffer/buffer_util.h"

#include <charconv>
#include <cstddef>

#include "source/common/common/macros.h"

Expand All @@ -21,13 +22,9 @@ void Util::serializeDouble(double number, Buffer::Instance& buffer) {
// generate the string_view, and does not work on all platforms yet.
//
// The accuracy is checked in buffer_util_test.
#if defined(__APPLE__) || defined(GCC_COMPILER)
// On Apple and gcc, std::to_chars does not work with 'double', so we revert
// to the next fastest correct implementation.
buffer.addFragments({fmt::to_string(number)});
#else
// This version is awkward, and doesn't work on Apple as of August 2023, but
// it is the fastest correct option on other platforms.
#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 14000
// This version is awkward, and doesn't work on all platforms used in Envoy CI
// as of August 2023, but it is the fastest correct option on modern compilers.
char buf[100];
std::to_chars_result result = std::to_chars(buf, buf + sizeof(buf), number);
ENVOY_BUG(result.ec == std::errc{}, std::make_error_code(result.ec).message());
Expand All @@ -36,6 +33,11 @@ void Util::serializeDouble(double number, Buffer::Instance& buffer) {
// Note: there is room to speed this up further by serializing the number directly
// into the buffer. However, buffer does not currently make it easy and fast
// to get (say) 100 characters of raw buffer to serialize into.
#else
// On older compilers, such as those found on Apple, and gcc, std::to_chars
// does not work with 'double', so we revert to the next fastest correct
// implementation.
buffer.addFragments({fmt::to_string(number)});
#endif
}

Expand Down

0 comments on commit 154219f

Please sign in to comment.