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

Add fmt::print() overload to support compiled format (FMT_COMPILE) #2304

Merged
merged 1 commit into from
May 25, 2021

Conversation

alexezeder
Copy link
Contributor

Resolves #2280.

By adding a new exported function - detail::print_buffer(std::FILE*, const memory_buffer&). This function is used in both vprint() from format-inl.h and a new print() from compile.h.

@vitaut
Copy link
Contributor

vitaut commented May 23, 2021

Thanks for the PR. In general looks good but I wonder if it's worth doing considering that format string processing time is likely small compared to I/O. Could you provide some benchmarks?

@alexezeder
Copy link
Contributor Author

alexezeder commented May 24, 2021

Okay, I have the results, and there are some improvements. It worth noting though that these results may be too specific for my PC.

Using static library:

Benchmark Time
JustWrite42 12.2 ns
PrintTrivial 25.2 ns
PrintTrivialCompile 20.3 ns
PrintComplex 216 ns
PrintComplexCompile 135 ns

Using header-only library:

Benchmark Time
JustWrite42 12.1 ns
PrintTrivial 25.2 ns
PrintTrivialCompile 16.6 ns
PrintComplex 216 ns
PrintComplexCompile 141 ns

Here a short notes about each benchmark:

  • JustWrite42 - just write "42" to file using std::fwrite() (just to measure base I/O speed)
  • PrintTrivial - use fmt::print("{}", 42) to write 42
  • PrintTrivialCompile - same as PrintTrivial, but using compile-time API
  • PrintComplex - same as trivial, but with "{:08x} {:0.2f} {:>7}" as format string
  • PrintComplexCompile - same as PrintComplex, but using compile-time API

memory_buffer buffer;
detail::vformat_to(buffer, format_str, args);
namespace detail {
FMT_FUNC void print_buffer(std::FILE* f, const memory_buffer& buffer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be slightly better to pass the text as a string_view instead of a buffer. This also means that the name would have to change. Maybe detail::print?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, the most challenging part of this change was to name the new argument correctly 😄, but since you mentioned that it's the text, I used text as a name.

@vitaut
Copy link
Contributor

vitaut commented May 24, 2021

Okay, I have the results, and there are some improvements.

Thanks for the benchmarks, they look convincing.

@alexezeder alexezeder force-pushed the fix/print_with_fmt_compile branch from effd732 to 16f9d54 Compare May 25, 2021 20:57
@vitaut vitaut merged commit ee52a6d into fmtlib:master May 25, 2021
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.

FMT_COMPILE(s) does not work with chrono example
2 participants