Skip to content

Commit

Permalink
common/Formatter: use CachedStackStringStream for efficiency
Browse files Browse the repository at this point in the history
A stringstream is incredibly expensive to construct, use a cached one.

Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
  • Loading branch information
batrick committed May 10, 2024
1 parent 61bf8cb commit 124a0df
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions src/common/Formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "HTMLFormatter.h"
#include "common/escape.h"
#include "common/StackStringStream.h"
#include "include/buffer.h"

#include <fmt/format.h>
Expand All @@ -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;
CachedStackStringStream css;

bool neg = num < 0;
if (neg) num = -num;

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);
css->fill('0');
css->width(scale + 1);
*css << num;
auto len = css->strv().size();

CachedStackStringStream css2;
*css2 << (neg ? "-" : "")
<< css->strv().substr(0, len - scale)
<< "."
<< css->strv().substr(len - scale);
return css2->str();
}

/*
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -290,10 +303,10 @@ void JSONFormatter::finish_pending_string()
template <class T>
void JSONFormatter::add_value(std::string_view name, T val)
{
std::stringstream ss;
ss.precision(std::numeric_limits<T>::max_digits10);
ss << val;
add_value(name, ss.str(), false);
CachedStackStringStream css;
css->precision(std::numeric_limits<T>::max_digits10);
*css << val;
add_value(name, css->strv(), false);
}

void JSONFormatter::add_value(std::string_view name, std::string_view val, bool quoted)
Expand Down Expand Up @@ -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<std::pair<std::string, std::string> >::const_iterator iter = attrs->attrs.begin();
iter != attrs->attrs.end(); ++iter) {
std::pair<std::string, std::string> 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)
Expand Down Expand Up @@ -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<std::pair<std::string, std::string> >::const_iterator iter = attrs->attrs.begin();
iter != attrs->attrs.end(); ++iter) {
std::pair<std::string, std::string> 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()
Expand Down

0 comments on commit 124a0df

Please sign in to comment.