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

PERF: readability container size empty #414

Conversation

hjmjohnson
Copy link
Member

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.

SRCDIR=/Users/johnsonhj/Dashboard/src/ITK #My local SRC
BLDDIR=/Users/johnsonhj/Dashboard/src/ITK-clang #My local BLD

cd /Users/johnsonhj/Dashboard/src/ITK-clang
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-,readability-container-size-empty -header-filter=. -fix

@hjmjohnson hjmjohnson self-assigned this Jan 15, 2019
@hjmjohnson hjmjohnson added the type:Performance Improvement in terms of compilation or execution time label Jan 15, 2019
Copy link
Contributor

@N-Dekker N-Dekker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; correctly follows Scott Meyers, Effective STL (2001) Item 4: "Call empty instead of checking size() against zero", as far as I can see

Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

@@ -720,7 +720,7 @@ bool GDCMImageIO::CanWriteFile(const char *name)
{
std::string filename = name;

if ( filename == "" )
if ( filename.empty() )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to contribute 3rd party (GDCM etc) changes upstream?

@dzenanz
Copy link
Member

dzenanz commented Jan 15, 2019

@thewtex I don't see build errors from this PR on the dashboard. Was there some recent change, or is something wrong?

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.

SRCDIR=/Users/johnsonhj/Dashboard/src/ITK #My local SRC
BLDDIR=/Users/johnsonhj/Dashboard/src/ITK-clang #My local BLD

cd /Users/johnsonhj/Dashboard/src/ITK-clang
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,readability-container-size-empty  -header-filter=.* -fix
@hjmjohnson hjmjohnson force-pushed the readability-container-size-empty branch from 8bc31c8 to f11cb40 Compare January 15, 2019 17:03
@hjmjohnson hjmjohnson merged commit c82a5f2 into InsightSoftwareConsortium:master Jan 15, 2019
@thewtex
Copy link
Member

thewtex commented Jan 16, 2019

@thewtex I don't see build errors from this PR on the dashboard. Was there some recent change, or is something wrong?

I am not following - are errors expected?

@dzenanz
Copy link
Member

dzenanz commented Jan 16, 2019

I expected 3 builds with PR414 in name to appear on the dashboard. I could not find them. Now I can see the Linux one (Linux-Build787-PR414-readability-container-size-empty).

@hjmjohnson
Copy link
Member Author

@thewtex @dzenanz There was some CI build anomaly where all builds failed. I did a simple rebase on upstream/master to force used which triggered successful CI builds.

@hjmjohnson hjmjohnson deleted the readability-container-size-empty branch March 2, 2019 23:33
N-Dekker added a commit to SuperElastix/elastix that referenced this pull request May 23, 2021
Ran Clang-Tidy (LLVM 11.1.0) `readability-container-size-empty`, as motivated at https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html :

"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."

Follows ITK pull request InsightSoftwareConsortium/ITK#414 commit InsightSoftwareConsortium/ITK@c82a5f2 "PERF: readability container size empty", Hans Johnson, January 2019.
N-Dekker added a commit to SuperElastix/elastix that referenced this pull request May 23, 2021
Ran Clang-Tidy (LLVM 11.1.0) `readability-container-size-empty`, as motivated at https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html :

"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."

Got extra motivated by having my very first microsoft/STL commit on this topic: microsoft/STL@b95ba0e "Avoid calling size() in empty() member functions" (merged from pull request microsoft/STL#1836)

Follows ITK pull request InsightSoftwareConsortium/ITK#414 commit InsightSoftwareConsortium/ITK@c82a5f2 "PERF: readability container size empty", Hans Johnson, January 2019.
N-Dekker added a commit to SuperElastix/elastix that referenced this pull request May 23, 2021
Ran Clang-Tidy (LLVM 11.1.0) `readability-container-size-empty`, as motivated at https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html :

"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."

Got extra motivated by having my very first microsoft/STL commit on this topic: microsoft/STL@b95ba0e "Avoid calling size() in empty() member functions" (merged from pull request microsoft/STL#1836)

Follows ITK pull request InsightSoftwareConsortium/ITK#414 commit InsightSoftwareConsortium/ITK@c82a5f2 "PERF: readability container size empty", Hans Johnson, January 2019.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:Performance Improvement in terms of compilation or execution time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants