Skip to content

fmt::format<Char, T> inefficiency #92

@seanmiddleditch

Description

@seanmiddleditch

Around format.h:2066 is the function:

template <typename Char, typename T>
void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value) {
  std::basic_ostringstream<Char> os;
  os << value;

The use of basic_ostringstream is unnecessary here. The use of this template creates a new string stream - including a new string buffer - for each value without an explicit formatter.

basic_ostringstream is essentially just a specialized basic_ostream that binds to a basic_stringbuf. It's this string buffer that's the major unnecessary overhead. basic_stringbuf is an implementation of basic_streambuf that writes into a basic_string. The stream buffer is the object that does all of the actual writing and real work for IOStreams; the other classes that most users are familiar with are just a more usable facade.

cppformat could include its own basic_formatbuf that directly writes into the buffer space used by cppformat, directly applying the format parameters. This would remove any need for excess allocations or temporary string buffers. It would continue allowing the use of cppformat with any type that supports IOStreams output without (very much) overhead.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions