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

chore: improve performance of nvm version command #1695

Closed
wants to merge 1 commit into from
Closed

chore: improve performance of nvm version command #1695

wants to merge 1 commit into from

Conversation

maksimr
Copy link

@maksimr maksimr commented Dec 23, 2017

fix #1694

This pull request reduces CPU time from 50ms to 15ms.

What was done:

  1. I have changed command printf on echo because printf is slower than echo
    (printf - 2ms, echo - 0.09ms)

    chore: improve performance of nvm version command #1695 (comment)
  2. Replace while cycle from nvm_tree_contains_path on string comparison
    (while - 14ms, str comparison - 0.06ms)
  3. Changed getting node version in nvm_ls_current from node --version to getting it from NVM_LS_CURRENT_NODE_PATH.
    (node --version - 13ms, from string - 0.09ms)

Before:

v6.8.0
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    2          28.13    14.07   51.58%     28.13    14.07   51.58%  nvm_tree_contains_path
 2)    1          45.82    45.82   84.01%     15.08    15.08   27.65%  nvm_ls_current
 3)    2          54.51    27.26   99.96%      8.61     4.31   15.79%  nvm
 4)    1           2.61     2.61    4.79%      2.61     2.61    4.79%  nvm_echo
 5)    1          45.90    45.90   84.17%      0.08     0.08    0.15%  nvm_version
 6)    1          54.54    54.54  100.00%      0.02     0.02    0.04%  run-test

-----------------------------------------------------------------------------------

 6)    1          54.54    54.54  100.00%      0.02     0.02    0.04%  run-test
       1/2        54.51    54.51   99.96%      4.50     4.50             nvm [3]

-----------------------------------------------------------------------------------

       1/2        54.51    54.51   99.96%      4.50     4.50             run-test [6]
       1/2        50.01    50.01   91.70%      4.11     4.11             nvm [3]
 3)    2          54.51    27.26   99.96%      8.61     4.31   15.79%  nvm
       1/1        45.90    45.90   84.17%      0.08     0.08             nvm_version [5]
       1/2        50.01    50.01   91.70%      4.11     4.11             nvm [3]

-----------------------------------------------------------------------------------

       1/1        45.90    45.90   84.17%      0.08     0.08             nvm [3]
 5)    1          45.90    45.90   84.17%      0.08     0.08    0.15%  nvm_version
       1/1        45.82    45.82   84.01%     15.08    15.08             nvm_ls_current [2]

-----------------------------------------------------------------------------------

       1/1        45.82    45.82   84.01%     15.08    15.08             nvm_version [5]
 2)    1          45.82    45.82   84.01%     15.08    15.08   27.65%  nvm_ls_current
       1/1         2.61     2.61    4.79%      2.61     2.61             nvm_echo [4]
       2/2        28.13    14.07   51.58%     28.13    14.07             nvm_tree_contains_path [1]

-----------------------------------------------------------------------------------

       2/2        28.13    14.07   51.58%     28.13    14.07             nvm_ls_current [2]
 1)    2          28.13    14.07   51.58%     28.13    14.07   51.58%  nvm_tree_contains_path

-----------------------------------------------------------------------------------

       1/1         2.61     2.61    4.79%      2.61     2.61             nvm_ls_current [2]
 4)    1           2.61     2.61    4.79%      2.61     2.61    4.79%  nvm_echo

After:

v6.8.0
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    2          11.88     5.94   99.83%      8.06     4.03   67.76%  nvm
 2)    1           3.75     3.75   31.51%      3.55     3.55   29.82%  nvm_ls_current
 3)    2           0.11     0.06    0.93%      0.11     0.06    0.93%  nvm_tree_contains_path
 4)    1           0.09     0.09    0.76%      0.09     0.09    0.76%  nvm_echo
 5)    1           3.82     3.82   32.06%      0.07     0.07    0.55%  nvm_version
 6)    1          11.90    11.90  100.00%      0.02     0.02    0.17%  run-test

-----------------------------------------------------------------------------------

 6)    1          11.90    11.90  100.00%      0.02     0.02    0.17%  run-test
       1/2        11.88    11.88   99.83%      4.06     4.06             nvm [1]

-----------------------------------------------------------------------------------

       1/2        11.88    11.88   99.83%      4.06     4.06             run-test [6]
       1/2         7.82     7.82   65.74%      4.01     4.01             nvm [1]
 1)    2          11.88     5.94   99.83%      8.06     4.03   67.76%  nvm
       1/1         3.82     3.82   32.06%      0.07     0.07             nvm_version [5]
       1/2         7.82     7.82   65.74%      4.01     4.01             nvm [1]

-----------------------------------------------------------------------------------

       1/1         3.82     3.82   32.06%      0.07     0.07             nvm [1]
 5)    1           3.82     3.82   32.06%      0.07     0.07    0.55%  nvm_version
       1/1         3.75     3.75   31.51%      3.55     3.55             nvm_ls_current [2]

-----------------------------------------------------------------------------------

       1/1         3.75     3.75   31.51%      3.55     3.55             nvm_version [5]
 2)    1           3.75     3.75   31.51%      3.55     3.55   29.82%  nvm_ls_current
       1/1         0.09     0.09    0.76%      0.09     0.09             nvm_echo [4]
       2/2         0.11     0.06    0.93%      0.11     0.06             nvm_tree_contains_path [3]

-----------------------------------------------------------------------------------

       2/2         0.11     0.06    0.93%      0.11     0.06             nvm_ls_current [2]
 3)    2           0.11     0.06    0.93%      0.11     0.06    0.93%  nvm_tree_contains_path

-----------------------------------------------------------------------------------

       1/1         0.09     0.09    0.76%      0.09     0.09             nvm_ls_current [2]
 4)    1           0.09     0.09    0.76%      0.09     0.09    0.76%  nvm_echo

@ljharb ljharb added the performance This relates to anything regarding the speed of using nvm. label Dec 23, 2017
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

There's a few legit tests failing.

In particular, some of the substitutions you're using seem to not be working.

nvm.sh Outdated
@@ -13,7 +13,7 @@
NVM_SCRIPT_SOURCE="$_"

nvm_echo() {
command printf %s\\n "$*" 2>/dev/null
echo "$*" 2>/dev/null
Copy link
Member

Choose a reason for hiding this comment

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

Why this change? This is problematic; the command is necessary to bypass any user aliases or functions, and the printf is more reliable than echo across shells.

Copy link
Author

Choose a reason for hiding this comment

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

@ljharb

I have described it in the description.

I have changed command printf on echo because printf is slower than echo
(printf - 2ms, echo - 0.09ms)

What do you think about command echo?

Thanks

Copy link
Member

Choose a reason for hiding this comment

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

We can certainly try it; I think i added automated tests around this one. Specifically the issue was about multiline input on specific shells, I think dash and sh.

Copy link
Author

@maksimr maksimr Dec 23, 2017

Choose a reason for hiding this comment

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

🤔 I have checked and I was wrong.

command printf majorized by command so if we could not remove command I just revert this change

Thanks

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good; some slowdowns are inevitable for correctness, sadly, but we can still try to shave off time wherever we can :-)

nvm.sh Outdated
else
nvm_echo "${VERSION}"
fi
VERSION=${${NVM_LS_CURRENT_NODE_PATH#$NVM_DIR/versions/node/}%/bin/node}
Copy link
Member

Choose a reason for hiding this comment

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

This is reporting "bad substitution" - https://travis-ci.org/creationix/nvm/jobs/320732790#L1029

@ljharb
Copy link
Member

ljharb commented Dec 27, 2017

Still seeing test failures :-/

@maksimr
Copy link
Author

maksimr commented Dec 27, 2017

@ljharb yup, tests fail because they rely on some externals commands for example that code will call node --version

@maksimr
Copy link
Author

maksimr commented Dec 27, 2017

I will try to figure out how fix tests

@ljharb
Copy link
Member

ljharb commented Sep 29, 2021

@maksimr hi! it's been awhile, but i've rebased this and it's failing some tests. any chance you're still interested in finishing this PR?

@maksimr
Copy link
Author

maksimr commented Sep 30, 2021

@ljharb I have checked changes again with node@14.17.6 on M1 and don't see too much profit from it. So let's close it.

Thanks!

@maksimr maksimr closed this Sep 30, 2021
@ljharb
Copy link
Member

ljharb commented Sep 30, 2021

Ok, that’s unfortunate - any speed improvement helps. If you ever change your mind, please reopen this PR in the future.

@maksimr
Copy link
Author

maksimr commented Sep 30, 2021

@ljharb sure, we use nvm heavily but I guess that main pitfall from the issue was fixed by node itself or by Moore law :)
If we face a problem or find a way to improve nvm somehow I definitely make a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance This relates to anything regarding the speed of using nvm.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

load-nvmrc is slow
2 participants