From 0610ed0ef095536b8f7a9477eeb070d2f5c957b9 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Thu, 9 May 2024 12:25:46 -0400 Subject: [PATCH] common/Formatter: use CachedStackStringStream for efficiency A stringstream is incredibly expensive to construct, use a cached one. Signed-off-by: Patrick Donnelly --- src/common/Formatter.cc | 67 ++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index b43e04a8bd09f..e2bbdd0104f84 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -16,6 +16,7 @@ #include "HTMLFormatter.h" #include "common/escape.h" +#include "common/StackStringStream.h" #include "include/buffer.h" #include @@ -29,27 +30,39 @@ namespace ceph { std::string fixed_u_to_string(uint64_t num, int scale) { - std::ostringstream t; + CachedStackStringStream css; - t.fill('0'); - t.width(scale + 1); - t << num; - int len = t.str().size(); - return t.str().substr(0,len - scale) + "." + t.str().substr(len - scale); + css->fill('0'); + css->width(scale + 1); + *css << num; + auto len = css->strv().size(); + + CachedStackStringStream css2; + *css2 << css->strv().substr(0, len - scale) + << "." + << css->strv().substr(len - scale); + return css2->str(); } std::string fixed_to_string(int64_t num, int scale) { - std::ostringstream t; - bool neg = num < 0; - if (neg) num = -num; + CachedStackStringStream css; + + bool neg = num < 0; + if (neg) num = -num; + + css->fill('0'); + css->width(scale + 1); + *css << num; + auto len = css->strv().size(); - t.fill('0'); - t.width(scale + 1); - t << num; - int len = t.str().size(); - return (neg ? "-" : "") + t.str().substr(0,len - scale) + "." + t.str().substr(len - scale); + CachedStackStringStream css2; + *css2 << (neg ? "-" : "") + << css->strv().substr(0, len - scale) + << "." + << css->strv().substr(len - scale); + return css2->str(); } /* @@ -116,9 +129,9 @@ Formatter *Formatter::create(std::string_view type, void Formatter::flush(bufferlist &bl) { - std::stringstream os; - flush(os); - bl.append(os.str()); + CachedStackStringStream css; + flush(*css); + bl.append(css->strv()); } void Formatter::dump_format(std::string_view name, const char *fmt, ...) @@ -290,10 +303,10 @@ void JSONFormatter::finish_pending_string() template void JSONFormatter::add_value(std::string_view name, T val) { - std::stringstream ss; - ss.precision(std::numeric_limits::max_digits10); - ss << val; - add_value(name, ss.str(), false); + CachedStackStringStream css; + css->precision(std::numeric_limits::max_digits10); + *css << val; + add_value(name, css->strv(), false); } void JSONFormatter::add_value(std::string_view name, std::string_view val, bool quoted) @@ -564,15 +577,15 @@ void XMLFormatter::write_bin_data(const char* buff, int buf_len) void XMLFormatter::get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str) { - std::stringstream attrs_ss; + CachedStackStringStream css; for (std::list >::const_iterator iter = attrs->attrs.begin(); iter != attrs->attrs.end(); ++iter) { std::pair p = *iter; - attrs_ss << " " << p.first << "=" << "\"" << p.second << "\""; + *css << " " << p.first << "=" << "\"" << p.second << "\""; } - attrs_str = attrs_ss.str(); + attrs_str = css->strv(); } void XMLFormatter::open_section_in_ns(std::string_view name, const char *ns, const FormatterAttrs *attrs) @@ -941,15 +954,15 @@ void TableFormatter::write_raw_data(const char *data) { void TableFormatter::get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str) { - std::stringstream attrs_ss; + CachedStackStringStream css; for (std::list >::const_iterator iter = attrs->attrs.begin(); iter != attrs->attrs.end(); ++iter) { std::pair p = *iter; - attrs_ss << " " << p.first << "=" << "\"" << p.second << "\""; + *css << " " << p.first << "=" << "\"" << p.second << "\""; } - attrs_str = attrs_ss.str(); + attrs_str = css->strv(); } void TableFormatter::finish_pending_string() -- 2.39.5