[PATCH] D148318: [clang-tidy] Add `performance-avoid-endl` check
Carlos Galvez via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 14 07:29:02 PDT 2023
carlosgalvezp added a comment.
Thank you for the contribution! Looks good in general, have minor comments.
================
Comment at: clang-tools-extra/docs/clang-tidy/checks/performance/avoid-endl.rst:1
+.. title:: clang-tidy - performance-avoid-endl
+
----------------
Please wrap file to 80 chars
================
Comment at: clang-tools-extra/docs/clang-tidy/checks/performance/avoid-endl.rst:9
+Rationale:
+Using ``std::endl`` on iostreams can be less efficient than using the newline character ``'\n'`` because ``std::endl`` performs two operations: it writes a newline character to the output stream and then flushes the stream buffer. Writing a single newline character using ``'\n'`` does not trigger a flush, which can improve performance. In addition, flushing the stream buffer can cause additional overhead when working with streams that are buffered.
+
----------------
Nit: maybe document the rationale for using `'\n'` instead of `"\n"`? Readability-wise it's more consistent to always use double-quotes.
================
Comment at: clang-tools-extra/docs/clang-tidy/checks/performance/avoid-endl.rst:23
+
+The ``std::endl`` on line 4 performs two operations: it writes a newline character to the ``std::cout`` stream and then flushes the stream buffer. This can be less efficient than using the newline character ``'\n'`` instead:
+
----------------
The rendering does not display lines numbers so this can be confusing. I believe we can just omit it - it's clear what it's referring too.
================
Comment at: clang-tools-extra/docs/clang-tidy/checks/performance/avoid-endl.rst:23
+
+The ``std::endl`` on line 4 performs two operations: it writes a newline character to the ``std::cout`` stream and then flushes the stream buffer. This can be less efficient than using the newline character ``'\n'`` instead:
+
----------------
carlosgalvezp wrote:
> The rendering does not display lines numbers so this can be confusing. I believe we can just omit it - it's clear what it's referring too.
This is repetition of the Rationale on line 8, I would just remove it and say "this gets transformed into:"
================
Comment at: clang-tools-extra/test/clang-tidy/checkers/performance/avoid-endl.cpp:47
+ std::cout << "World" << std::endl;
+ // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use std::endl with iostreams; use '\n' instead
+ std::cerr << "World" << std::endl;
----------------
Use CHECK-FIXES to also test the fixes.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D148318/new/
https://reviews.llvm.org/D148318
More information about the cfe-commits
mailing list