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

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

Closed
seanmiddleditch opened this issue Jan 28, 2015 · 5 comments
Closed

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

seanmiddleditch opened this issue Jan 28, 2015 · 5 comments

Comments

@seanmiddleditch
Copy link

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.

@vitaut
Copy link
Contributor

vitaut commented Jan 28, 2015

Thanks for the suggestion. I did plan to implement a custom stream buffer, but didn't get around to it yet.

@patlecat
Copy link

Yeah stringstreams with a custom allocator class would be awesome and really make sense, because they are kinda slowish in STL.

@NotImplemented
Copy link

I have experimented with custom basic_formatbuf which is derived from basic_streambuf and I made it possible to write to Formatter's writer().buffer_. This solution is done to preserve ability to work with types, which support output to IO streams without any changes. However, Writer is unaware of what is written by basic_formatbuf.

@vitaut
Copy link
Contributor

vitaut commented Nov 15, 2015

Awesome. Could you submit a pull request? BTW your nick looks familiar, Mike is it you? =)

NotImplemented pushed a commit to NotImplemented/cppformat that referenced this issue Nov 17, 2015
vitaut pushed a commit that referenced this issue Nov 24, 2015
@vitaut
Copy link
Contributor

vitaut commented Nov 24, 2015

Fixed in d266adf.

@vitaut vitaut closed this as completed Nov 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants