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

path: remove unnecessary string copies #14438

Closed
wants to merge 5 commits into from

Conversation

tniessen
Copy link
Member

The length of path is known, so path.slice(0, len) === path. Unless I am missing something, these calls are unnecessary.

Partial CI: https://ci.nodejs.org/job/node-test-commit-linuxone/7511/

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

path

At this line, `path` has a length of 2, so `path.slice(0, 2) === path`.
@tniessen tniessen added the path Issues and PRs related to the path subsystem. label Jul 23, 2017
@tniessen tniessen self-assigned this Jul 23, 2017
@tniessen tniessen requested a review from refack July 23, 2017 20:11
@nodejs-github-bot nodejs-github-bot added the path Issues and PRs related to the path subsystem. label Jul 23, 2017
@mscdex
Copy link
Contributor

mscdex commented Jul 23, 2017

Could you add a test for the second change? It seems that line is not currently covered and it would be good to check that the same result is returned before and after this PR.

@refack
Copy link
Contributor

refack commented Jul 23, 2017

@tniessen If you're adding new tests, see if you can cover the if (code === 47/*/*/ || code === 92/*\*/) from L1057
image

@@ -1049,7 +1049,7 @@ const win32 = {
} else {
// `path` contains just a drive root, exit early to avoid
// unnecessary work
ret.root = ret.dir = path.slice(0, 2);
ret.root = ret.dir = path;
return ret;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

The

       ret.root = ret.dir = path[0];

below can be simplified to

       ret.root = ret.dir = path;

as well, AFAICT.

Copy link
Contributor

Choose a reason for hiding this comment

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

Anyway could you add a comment // `(len <= 1)`

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

LGTM with a test.

@tniessen
Copy link
Member Author

coverage

@refack

@tniessen
Copy link
Member Author

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

💯

Copy link
Contributor

@XadillaX XadillaX left a comment

Choose a reason for hiding this comment

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

LGTM.

tniessen added a commit that referenced this pull request Jul 26, 2017
As the length of `path` is known at this point, there is no point in
making an exact copy using `slice`.

PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
tniessen added a commit that referenced this pull request Jul 26, 2017
PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
@tniessen
Copy link
Member Author

@tniessen tniessen closed this Jul 26, 2017
addaleax pushed a commit that referenced this pull request Jul 27, 2017
As the length of `path` is known at this point, there is no point in
making an exact copy using `slice`.

PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
@addaleax
Copy link
Member

The second commit (f7f590c) doesn’t land cleanly on 8.x; if you can, please follow the guide and raise a backport PR, if you don’t think it’s worth it let me know/add the dont-land-on label.

tniessen added a commit to tniessen/node that referenced this pull request Jul 27, 2017
PR-URL: nodejs#14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
addaleax pushed a commit that referenced this pull request Jul 28, 2017
Backport-PR-URL: #14521
Backport-Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
@addaleax addaleax mentioned this pull request Aug 2, 2017
@TimothyGu
Copy link
Member

TimothyGu commented Aug 12, 2017

I'll handle backporting this to v6.x together with #14440.

Edit: done in #14787

tniessen added a commit to TimothyGu/node that referenced this pull request Aug 14, 2017
As the length of `path` is known at this point, there is no point in
making an exact copy using `slice`.

PR-URL: nodejs#14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
MylesBorins pushed a commit that referenced this pull request Aug 14, 2017
Backport-PR-URL: #14787
PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
MylesBorins pushed a commit that referenced this pull request Aug 14, 2017
As the length of `path` is known at this point, there is no point in
making an exact copy using `slice`.

Backport-PR-URL: #14787
PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
MylesBorins pushed a commit that referenced this pull request Aug 16, 2017
Backport-PR-URL: #14787
PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
MylesBorins pushed a commit that referenced this pull request Aug 16, 2017
As the length of `path` is known at this point, there is no point in
making an exact copy using `slice`.

Backport-PR-URL: #14787
PR-URL: #14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
@MylesBorins MylesBorins mentioned this pull request Aug 16, 2017
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this pull request May 5, 2024
PR-URL: nodejs/node#14438
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
path Issues and PRs related to the path subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.