Closed hjmjohnson closed 4 days ago
It is not guaranteed that size() is a constant-time function
Actually, it is. For all std
container types, size()
has a constant-time complexity. So I don't expect any PERF improvement from this pull request. It's just a STYLE improvement to me. But I like it anyway 👍
Originally, Microsoft implemented empty()
by calling size()
, so empty()
might then even be slower than size()
😱 Fortunately I was able to convince them not to do so anymore 😇: https://github.com/microsoft/STL/pull/1836
It is not guaranteed that size() is a constant-time function
Actually, it is. For all
std
container types,size()
has a constant-time complexity. So I don't expect any PERF improvement from this pull request. It's just a STYLE improvement to me. But I like it anyway 👍Originally, Microsoft implemented
empty()
by callingsize()
, soempty()
might then even be slower thansize()
😱 Fortunately I was able to convince them not to do so anymore 😇: microsoft/STL#1836
FYI: The message is copied from the clang-tidy documentation page. I took it on faith that the documentation was correct. i trust your experiences more than the documentation :).
The message is copied from the clang-tidy documentation page. I took it on faith that the documentation was correct. i trust your experiences more than the documentation :).
Ah, thanks Hans, I see:
Maybe I should ask them for a clarification 🤔
@dzenanz @hjmjohnson Please "like" (👍) my clang-tidy pull request on this topic https://github.com/llvm/llvm-project/pull/117629 It is inspired by your pull request, Hans 😃 !
Done. I am not sure how much weight my approving review will carry 😄
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.
cd ${BLDDIR} run-clang-tidy.py -extra-arg=-Dclang -checks=-,readability-container-size-empty -header-filter=. -fix