Skip to content

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

Merged
vitaut merged 1 commit into
fmtlib:masterfrom
alexezeder:fix/print_with_fmt_compile
May 25, 2021
Merged

vitaut merged 1 commit into
fmtlib:masterfrom
alexezeder:fix/print_with_fmt_compile

Conversation

@alexezeder

Copy link
Copy Markdown
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

vitaut commented May 23, 2021

Copy link
Copy Markdown
Contributor

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

alexezeder commented May 24, 2021

Copy link
Copy Markdown
Contributor Author

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

Comment thread include/fmt/format-inl.h Outdated
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
Copy Markdown
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
Copy Markdown
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

vitaut commented May 24, 2021

Copy link
Copy Markdown
Contributor

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