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: improve posixSplitPath performance #3034

Merged
merged 1 commit into from
Sep 25, 2015
Merged

Conversation

evanlucas
Copy link
Contributor

Instead of slicing the first element off of the matches, manually build
the array. This improves performance of the following path.posix functions:

  • basename
    • path/basename.js type=win32 n=1000000: ./node: 847760 /usr/local/bin/node: 829710 .. 2.18%
    • path/basename.js type=posix n=1000000: ./node: 989290 /usr/local/bin/node: 847820 . 21.55%
  • extname
    • path/extname.js type=win32 n=1000000: ./node: 2385400 /usr/local/bin/node: 2451800 . -2.71%
    • path/extname.js type=posix n=1000000: ./node: 3379700 /usr/local/bin/node: 2113500 . 70.64%
  • dirname
    • path/dirname.js type=win32 n=1000000: ./node: 1699600 /usr/local/bin/node: 1706700 . -0.42%
    • path/dirname.js type=posix n=1000000: ./node: 2038700 /usr/local/bin/node: 1704300 . 21.99%
  • parse
    • path/parse.js type=win32 n=1000000: ./node: 1907900 /usr/local/bin/node: 1886300 .. 1.15%
    • path/parse.js type=posix n=1000000: ./node: 1923400 /usr/local/bin/node: 1616800 . 22.79%

@evanlucas evanlucas added the path Issues and PRs related to the path subsystem. label Sep 23, 2015
@mscdex
Copy link
Contributor

mscdex commented Sep 23, 2015

LGTM

@evanlucas
Copy link
Contributor Author

@evanlucas
Copy link
Contributor Author

meh, test-stringbytes-external on arm failed...as well as test-child-process-emfile on freebsd. I haven't seen that one fail before. Is that one known to be flaky at all?

@thefourtheye
Copy link
Contributor

@evanlucas They both are flaky, so don't worry about it :-)

@@ -408,7 +408,13 @@ var posix = {};


function posixSplitPath(filename) {
return splitPathRe.exec(filename).slice(1);
const matches = splitPathRe.exec(filename);
var len = matches.length - 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

If there is no match, then matches will be null.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would have been the case before too, though. I'm not sure if it's possible to get no match with the regexp that's currently being used ...

@alexlamsl
Copy link

Just curious - why not Array.shift() and return?

@evanlucas
Copy link
Contributor Author

@alexlamsl Nice catch. I swear I had tried that before and it was a tiny bit slower than manually doing it, but that is not correct. It looks like shift is a little faster. Thanks!

@evanlucas
Copy link
Contributor Author

@evanlucas
Copy link
Contributor Author

CI looks happy (minus test-stringbytes-external.js). Will plan to land tomorrow if no one has any objections.

@thefourtheye
Copy link
Contributor

I still have my doubts about null, otherwise LGTM.

@alexlamsl
Copy link

@thefourtheye let me see if I can convince you...

     1       2                3.1           3.2          4
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/

Part 1 (\/?|) - can be empty
Part 2 ([\s\S]*?) - matches nothing or any character sequence
Part 3.1 (?:\.{1,2}|[^\/]+?|) - can be empty
Part 3.2 (\.[^.\/]*|) - can be empty
Part 4 (?:[\/]*) - matches nothing, or any number of /'s

So this pattern will always match.

Instead of slicing the first element off of the matches, shift and then
return. This improves performance of the following path functions:

- basename: 18-20%
- extname: 60-70%
- dirname: 18-20%
- parse: 20-25%

PR-URL: nodejs#3034
Reviewed-By: Brian White <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
@evanlucas
Copy link
Contributor Author

Thanks! Landed in b50e89e

evanlucas added a commit that referenced this pull request Sep 30, 2015
Instead of slicing the first element off of the matches, shift and then
return. This improves performance of the following path functions:

- basename: 18-20%
- extname: 60-70%
- dirname: 18-20%
- parse: 20-25%

PR-URL: #3034
Reviewed-By: Brian White <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
This was referenced Sep 30, 2015
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.

4 participants