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

Proposal: Add formatter for std::error_code. #2269

Closed
phprus opened this issue May 7, 2021 · 4 comments
Closed

Proposal: Add formatter for std::error_code. #2269

phprus opened this issue May 7, 2021 · 4 comments

Comments

@phprus
Copy link
Contributor

phprus commented May 7, 2021

The std::error_code has are operator<< (https://en.cppreference.com/w/cpp/error/error_code/operator_ltlt), which is equivalent os << ec.category().name() << ':' << ec.value() .

Formatting with ostream is 4-5 (C++11) or 11-24 (C++17) times slower than the custom formatter class.

Simpe and possibly incorrect implementation: https://github.com/phprus/fmt-bench/blob/format-error_code/test_formatter.cpp (I agree that this code are licensed under the {fmt} license, and agree to future changes to the licensing.)
Benchmark (https://github.com/phprus/fmt-bench/tree/format-error_code):

AppleClang 11.0.0.11000033 C++11

Running ./fmt_test
Run on (8 X 2700 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x4)
  L1 Instruction 32 KiB (x4)
  L2 Unified 256 KiB (x4)
  L3 Unified 6144 KiB (x1)
Load Average: 1.79, 1.73, 1.69
-----------------------------------------------------------------
Benchmark                       Time             CPU   Iterations
-----------------------------------------------------------------
FMTIostream/22                369 ns          368 ns      1953098
FMTIostreamCompile/22         385 ns          384 ns      1863938
FMTFormatter/22              75.9 ns         75.8 ns      9392947
FMTFormatterCompile/22       79.6 ns         79.5 ns      8838830

AppleClang 11.0.0.11000033 C++17

Running ./fmt_test
Run on (8 X 2700 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x4)
  L1 Instruction 32 KiB (x4)
  L2 Unified 256 KiB (x4)
  L3 Unified 6144 KiB (x1)
Load Average: 1.68, 1.71, 1.69
-----------------------------------------------------------------
Benchmark                       Time             CPU   Iterations
-----------------------------------------------------------------
FMTIostream/22                389 ns          388 ns      1854806
FMTIostreamCompile/22         380 ns          378 ns      1794761
FMTFormatter/22              33.7 ns         33.7 ns     20944226
FMTFormatterCompile/22       16.4 ns         16.4 ns     42452029
@vitaut
Copy link
Contributor

vitaut commented May 7, 2021

Seems like a good idea, could you submit a PR?

@phprus
Copy link
Contributor Author

phprus commented May 7, 2021

Yes, but I'm not sure if my implementation is correct.

I don't know how to write the parse method correctly to follow the logic of the library.

@vitaut
Copy link
Contributor

vitaut commented May 7, 2021

I'm not sure if my implementation is correct.

Looks OK to me. You could simplify parse by just returning ctx.begin().

@phprus
Copy link
Contributor Author

phprus commented May 8, 2021

PR: #2270

@vitaut vitaut closed this as completed May 9, 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

No branches or pull requests

2 participants