-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
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.