Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THRIFT-5709: Drastically improve to_string() performance. #2806

Merged
merged 2 commits into from
Jun 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/cpp/src/thrift/TToString.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@
namespace apache {
namespace thrift {

// unnamed namespace to enforce internal linkage - could be done with 'inline' when once have C++17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: when/once is duplicate here.

namespace {
const auto default_locale = std::locale("C");
}

template <typename T>
std::string to_string(const T& t) {
std::ostringstream o;
o.imbue(std::locale("C"));
o.imbue(default_locale);
o << t;
return o.str();
}
Expand All @@ -44,23 +49,23 @@ std::string to_string(const T& t) {
// is enabled.
inline std::string to_string(const float& t) {
std::ostringstream o;
o.imbue(std::locale("C"));
o.imbue(default_locale);
o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<float>::digits * std::log10(2.0f) + 1))));
o << t;
return o.str();
}

inline std::string to_string(const double& t) {
std::ostringstream o;
o.imbue(std::locale("C"));
o.imbue(default_locale);
o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<double>::digits * std::log10(2.0f) + 1))));
o << t;
return o.str();
}

inline std::string to_string(const long double& t) {
std::ostringstream o;
o.imbue(std::locale("C"));
o.imbue(default_locale);
o.precision(static_cast<std::streamsize>(std::ceil(static_cast<double>(std::numeric_limits<long double>::digits * std::log10(2.0f) + 1))));
o << t;
return o.str();
Expand Down