-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang-tidy] Adjust size-empty doc because C++11 size() is constant-time #117629
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
[clang-tidy] Adjust size-empty doc because C++11 size() is constant-time #117629
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Niels Dekker (N-Dekker) ChangesFrom C++11, a conforming Notes:
Full diff: https://github.com/llvm/llvm-project/pull/117629.diff 1 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
index 617dadce76bd3e..acd8a6bfc50f5e 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
@@ -18,12 +18,10 @@ namespace clang::tidy::readability {
/// a call to `empty()`.
///
/// The emptiness of a container should be checked using the `empty()` method
-/// instead of the `size()` method. It is not guaranteed that `size()` is a
-/// constant-time function, and it is generally more efficient and also shows
-/// clearer intent to use `empty()`. Furthermore some containers may implement
-/// the `empty()` method but not implement the `size()` method. Using `empty()`
-/// whenever possible makes it easier to switch to another container in the
-/// future.
+/// instead of the `size()` method. It shows clearer intent to use `empty()`.
+/// Furthermore some containers may implement the `empty()` method but not
+/// implement the `size()` method. Using `empty()` whenever possible makes it
+/// easier to switch to another container in the future.
class ContainerSizeEmptyCheck : public ClangTidyCheck {
public:
ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context);
|
A personal note: yes, I like clang-tidy's readibility-container-size-empty check, I think it's great! And I do very much prefer |
You understand that this check does not apply only to std:: containers but also to boost and other custom one that have size and empty methods. In such case claim is still valid. If you change check description, then check documentation also should be updated to be in sync. |
Thanks for your prompt reply, Piotr. I thought of custom containers too, but then again, I would rather not make any claim about the efficiency of In order for the documentation to make claims about But then, do you happen to know whether there are indeed still Boost containers that have a
Thanks! So this pull request should make some more adjustments before it can be merged (if it would be accepted at all)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update clang-tools-extra/docs/clang-tidy/checks/readability/container-size-empty.rst
also.
I think we should remove the discussion about efficient in claim since it is misleading after C++11. For self-defined container, because implement of empty and size can be totally different, this claim is still misleading. |
From C++11, a conforming `size()` method is guaranteed to be a constant-time function. `empty()` is not _generally_ more efficient than `size()`. It might even be implemented in terms of `size()`.
87688f9
to
ab3009e
Compare
Thanks for your encouragement, @HerrCai0907. I just force-pushed, as you suggested. To further clarify my motivation, I believe that C++ users actually got the impression from the documentation that this check will make their code faster. ITK pull request InsightSoftwareConsortium/ITK#4985 ("PERF: readability container size empty") was also labeled as a performance improvement, while I think it is very unlikely to have any effect on the run-time performance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
@PiotrZSL @HerrCai0907 Thank you both for your feedback and approval! Would one of you be able the merge the pull request? |
@N-Dekker Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/17/builds/4311 Here is the relevant piece of the build log for the reference
|
From C++11, a conforming
size()
method is guaranteed to be a constant-time function.empty()
is not generally more efficient thansize()
. It might even be implemented in terms ofsize()
.Notes:
empty()
asreturn size() == 0
, until May 2021: https://github.com/microsoft/STL/pull/1836/filessize()
(specifically forstd::set
) was discussed by the library working group in 2007-2009: https://cplusplus.github.io/LWG/issue632