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

src: prefer C++ empty() in boolean expressions #34432

Closed
wants to merge 1 commit into from

Conversation

tniessen
Copy link
Member

This changes occurrences of length() and size() to empty() where it makes the code simpler.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@tniessen tniessen added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jul 19, 2020
@tniessen tniessen requested a review from a team July 19, 2020 21:29
@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Jul 19, 2020
@gengjiawen
Copy link
Member

Maybe also add readability-container-size-empty to https://github.com/nodejs/node/blob/a42dcbeb43ab22c0db07c4e0a9eab5e0f44578f4/src/.clang-tidy.

Comment on lines +2039 to 2042
while (url->path.size() > 1 && url->path[0].empty()) {
url->path.erase(url->path.begin());
}
Copy link
Member

Choose a reason for hiding this comment

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

Wildly inefficient way of removing empty prefixes... (n reallocations + n*m copies when 1 + m suffices)

Suggested change
while (url->path.size() > 1 && url->path[0].empty()) {
url->path.erase(url->path.begin());
}
auto begin = url->path.begin();
auto end = url->path.end();
auto from = begin;
auto to = begin;
while (to != end && to->empty()) ++to;
if (to == end && from == begin && from != to) ++from;
url->path.erase(from, to); // 1 + m

Copy link
Member

Choose a reason for hiding this comment

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

@bnoordhuis In your suggestion, the from == begin part is always true, unless i’m missing something?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, yeah. It was intended as a kind of self-documentation about the preconditions but I guess it's confusing more than anything else. if (to == end && from != end) ++from; is probably a clearer way of writing it.

Copy link
Member

Choose a reason for hiding this comment

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

As this is not directly related to the other changes in this PR, this could be done in a separate commit/PR.

Copy link
Member

@devsnek devsnek left a comment

Choose a reason for hiding this comment

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

the future is now

@gengjiawen
Copy link
Member

gengjiawen commented Jul 20, 2020

Failed test case in macOS. is this a flaky test ? cc @Trott

Path: parallel/test-macos-app-sandbox
--- stderr ---
assert.js:103
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

1 !== 0

    at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-macos-app-sandbox.js:41:8)
    at Module._compile (internal/modules/cjs/loader.js:1252:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1273:10)
    at Module.load (internal/modules/cjs/loader.js:1101:32)
    at Function.Module._load (internal/modules/cjs/loader.js:966:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: 1,
  expected: 0,
  operator: 'strictEqual'
}

@tniessen tniessen force-pushed the src-prefer-cpp-empty branch from 36b41e6 to 11d860c Compare August 9, 2020 16:47
@gengjiawen gengjiawen added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 10, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 10, 2020
@nodejs-github-bot
Copy link
Collaborator

@tniessen tniessen added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Aug 10, 2020
@tniessen tniessen requested review from a team as code owners August 10, 2020 16:06
@jasnell
Copy link
Member

jasnell commented Aug 11, 2020

Landed in 1be7bbd

@jasnell jasnell closed this Aug 11, 2020
jasnell pushed a commit that referenced this pull request Aug 11, 2020
PR-URL: #34432
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
MylesBorins pushed a commit that referenced this pull request Aug 17, 2020
PR-URL: #34432
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
@danielleadams danielleadams mentioned this pull request Aug 20, 2020
BethGriggs pushed a commit that referenced this pull request Aug 20, 2020
PR-URL: #34432
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
addaleax pushed a commit that referenced this pull request Sep 22, 2020
PR-URL: #34432
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
addaleax pushed a commit that referenced this pull request Sep 22, 2020
PR-URL: #34432
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
@codebytere codebytere mentioned this pull request Sep 28, 2020
@tniessen tniessen removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.