-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: replace duplicate conditions by functions #18693
Conversation
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.
Nice, LGTM. The change in performance doesn't look statistically significant.
32810c4
to
173c4f7
Compare
I resolved a conflict here |
173c4f7
to
6120bf5
Compare
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.
This is nice! Just some nits below.
lib/path.js
Outdated
|
||
function isWindowsDeviceRoot(code) { | ||
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z || | ||
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z; |
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.
Nit: this would be slightly nicer aligned with the code
part above, rather than just indented two spaces.
lib/path.js
Outdated
// `path` contains just a path separator, exit early to avoid unnecessary | ||
// work | ||
return '\\'; | ||
} | ||
|
||
code = path.charCodeAt(len - 1); | ||
var trailingSeparator = | ||
(code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH); | ||
var trailingSeparator = (isPathSeparator(code)); |
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.
Unnecessary parentheses here.
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.
I would also inline path.charCodeAt(len - 1)
.
lib/path.js
Outdated
@@ -935,15 +935,14 @@ const win32 = { | |||
if (path.length >= 2) { | |||
const code = path.charCodeAt(0); |
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.
This is unnecessary. This could all be just one if
statement. path.length >= 2 && path.charCodeAt(1) === CHAR_COLON && isWindowsDeviceRoot(path.charCodeAt(0))
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. I would just go ahead and inline lots of charCodeAt
calls.
lib/path.js
Outdated
// `path` contains just a path separator, exit early to avoid unnecessary | ||
// work | ||
return '\\'; | ||
} | ||
|
||
code = path.charCodeAt(len - 1); | ||
var trailingSeparator = | ||
(code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH); | ||
var trailingSeparator = (isPathSeparator(code)); |
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.
I would also inline path.charCodeAt(len - 1)
.
lib/path.js
Outdated
code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH; | ||
|
||
if (isPathSeparator) | ||
if (isPathSeparator(code)) |
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.
It should be fine to inline path.charCodeAt(j)
.
lib/path.js
Outdated
// Possible UNC root | ||
|
||
// If we started with a separator, we know we at least have an | ||
// absolute path of some kind (UNC or otherwise) | ||
isAbsolute = true; | ||
|
||
code = path.charCodeAt(1); | ||
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { | ||
if (isPathSeparator(code)) { |
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.
It should be fine to inline path.charCodeAt(1)
.
lib/path.js
Outdated
// Matched double path separator at beginning | ||
var j = 2; | ||
var last = j; | ||
// Match 1 or more non-path separators | ||
for (; j < len; ++j) { | ||
code = path.charCodeAt(j); | ||
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) | ||
if (isPathSeparator(code)) |
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.
It should be fine to inline path.charCodeAt(j)
lib/path.js
Outdated
@@ -259,7 +269,7 @@ const win32 = { | |||
// Match 1 or more path separators | |||
for (; j < len; ++j) { | |||
code = path.charCodeAt(j); | |||
if (code !== CHAR_FORWARD_SLASH && code !== CHAR_BACKWARD_SLASH) | |||
if (!isPathSeparator(code)) |
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.
It should be fine to inline pathCharCodeAt(j)
lib/path.js
Outdated
// Possible device root | ||
|
||
if (path.charCodeAt(1) === CHAR_COLON) { | ||
device = path.slice(0, 2); | ||
rootEnd = 2; | ||
if (len > 2) { | ||
code = path.charCodeAt(2); | ||
if (code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH) { | ||
if (isPathSeparator(code)) { |
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.
It should be fine to inline path.charCodeAt(2)
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.
Ok, this applies to a lot of places here ;-)
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.
@BridgeAR there are too many places like this. I think it will be better to fix it in a another pull request. What do you think?
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.
Hm, I personally think this would actually be the right place to do so. The reason is that it touches the same lines. @apapirovski what do you think?
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.
code
seems to mostly be there to avoid constantly calling path.charCodeAt(x) within the same conditional but now that these are a function, it seems like their purpose is gone. I would say that this could definitely be done in this PR even though it adds a bunch of work.
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.
@BridgeAR ok, I was just worried that it might affect some logic here. Nonetheless, I'll fix it
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.
@daynin it'll probably take some effort to validate all the places where it's safe to do so. Would suggest doing it as a separate commit. Should make reviews easier.
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.
@apaprocki ok, sure
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.
It should be relatively straight forward. Try to remove them from the most inner function and go to the outer one. I checked it for one big function and as far as I saw it was possible to remove all of them besides a single one that should switch to be const.
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.
@apapirovski @BridgeAR fixed
6120bf5
to
6853af6
Compare
lib/path.js
Outdated
return true; | ||
} else if ((code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) || | ||
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z)) { | ||
} else if (isWindowsDeviceRoot(path.charCodeAt(0))) { |
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.
Here in this case it would be better to keep the code
above as it will otherwise require two lookups for this branch.
lib/path.js
Outdated
@@ -958,7 +929,7 @@ const win32 = { | |||
matchedSlash = false; | |||
end = i + 1; | |||
} | |||
if (code === CHAR_DOT) { | |||
if (path.charCodeAt(i) === CHAR_DOT) { |
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.
Here it would be better to keep code
because this branch otherwise has to do a second lookup.
lib/path.js
Outdated
@@ -1571,7 +1529,7 @@ const posix = { | |||
matchedSlash = false; | |||
end = i + 1; | |||
} | |||
if (code === CHAR_DOT) { | |||
if (path.charCodeAt(i) === CHAR_DOT) { |
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.
Here it would be better to keep the code
because of a second lookup.
lib/path.js
Outdated
@@ -1492,7 +1452,7 @@ const posix = { | |||
matchedSlash = false; | |||
end = i + 1; | |||
} | |||
if (code === CHAR_DOT) { | |||
if (path.charCodeAt(i) === CHAR_DOT) { |
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 code
here to prevent a second lookup.
lib/path.js
Outdated
@@ -1421,7 +1382,7 @@ const posix = { | |||
} | |||
if (extIdx >= 0) { | |||
// Try to match the explicit extension | |||
if (code === ext.charCodeAt(extIdx)) { | |||
if (path.charCodeAt(i) === ext.charCodeAt(extIdx)) { |
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 keep code
here.
lib/path.js
Outdated
@@ -1115,7 +1079,7 @@ const win32 = { | |||
matchedSlash = false; | |||
end = i + 1; | |||
} | |||
if (code === CHAR_DOT) { | |||
if (path.charCodeAt(i) === CHAR_DOT) { |
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 keep code
here.
a33bf9c
to
b18a1a3
Compare
b18a1a3
to
1825066
Compare
@BridgeAR fixed |
Landed in b404aa5 🎉 |
It will also remove useless "code" variables by inlining path.charCodeAt. PR-URL: nodejs#18693 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com>
It will also remove useless "code" variables by inlining path.charCodeAt. PR-URL: #18693 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com>
It will also remove useless "code" variables by inlining path.charCodeAt. PR-URL: #18693 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com>
It will also remove useless "code" variables by inlining path.charCodeAt. PR-URL: #18693 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com>
It will also remove useless "code" variables by inlining path.charCodeAt. PR-URL: nodejs#18693 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
A benchmark results (there are no significant changes):