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 #230

Closed

Conversation

NotImplemented
Copy link

Should we write some tests for this item? What is a proper way to run tests locally?

internal::Arg arg = internal::MakeValue<Char>(str);
arg.type = static_cast<internal::Arg::Type>(
internal::MakeValue<Char>::type(str));
format_str = f.format(format_str, arg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I haven't looked in depth at all the errors that are occurring, based on some of the error messages, it seems likely that it's because of the removal of this line. It seems like fixing this may require using a temporary internal::MemoryBuffer that's used by basic_formatbuf, and then format a BasicStringRef that's extracted from the MemoryBuffer. Since MemoryBuffer can avoid memory allocations for short strings, it seems like this would meet part of the goals for this issue, even if there would still be some unnecessary copies in the "empty format string" case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be a bit more helpful on my suggestion, keeping the majority of your change the same, except for changing basic_format's constructor to accept a Buffer directly:

// Formats a value.
template <typename Char, typename T>
void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value) {
  internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;

  basic_formatbuf<Char> format_buf(buffer);
  std::basic_ostream<Char> output(&format_buf);
  output << value;
  format_buf.flush();

  BasicStringRef<Char> str(buffer.size() > 0 ? &buffer[0] : NULL, buffer.size());
  internal::Arg arg = internal::MakeValue<Char>(str);
  arg.type = static_cast<internal::Arg::Type>(
        internal::MakeValue<Char>::type(str));
  format_str = f.format(format_str, arg);
}

This passes all of the tests, based on an an older commit, though.

@NotImplemented
Copy link
Author

Yes, that will fix. Failing tests were connected with format check.

@vitaut
Copy link
Contributor

vitaut commented Nov 18, 2015

@NotImplemented Thanks for the PR, Mike! Just two minor comments in addition to what @mwinterb wrote. The format function should be covered by existing tests, but it might be useful to test basic_formatbuf. You can run the tests locally via the test target (RUN_TESTS in MSVC), e.g.

> make test

@vitaut
Copy link
Contributor

vitaut commented Nov 21, 2015

@NotImplemented LGTM and happy to merge. But could you squash the commits first to get rid of the ones that were failing on CI?


using std::basic_streambuf<Elem, Traits>::setp;
using std::basic_streambuf<Elem, Traits>::pptr;
using std::basic_streambuf<Elem, Traits>::pbase;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space?

@vitaut vitaut force-pushed the master branch 3 times, most recently from 61d67dd to 6cff6d8 Compare November 24, 2015 16:18
@vitaut
Copy link
Contributor

vitaut commented Nov 24, 2015

Merged, thanks!

@vitaut vitaut closed this Nov 24, 2015
@NotImplemented NotImplemented deleted the slow-custom-type-format branch August 9, 2016 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants