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

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
2 participants
@maksimr
Copy link

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)

    #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 label Dec 23, 2017

@ljharb
Copy link
Collaborator

left a comment

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

This comment has been minimized.

Copy link
@ljharb

ljharb Dec 23, 2017

Collaborator

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.

This comment has been minimized.

Copy link
@maksimr

maksimr Dec 23, 2017

Author

@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

This comment has been minimized.

Copy link
@ljharb

ljharb Dec 23, 2017

Collaborator

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.

This comment has been minimized.

Copy link
@maksimr

maksimr Dec 23, 2017

Author

🤔 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

This comment has been minimized.

Copy link
@ljharb

ljharb Dec 23, 2017

Collaborator

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

@maksimr maksimr force-pushed the maksimr:patch-perf-1 branch from ee73456 to 791010a Dec 23, 2017

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

This comment has been minimized.

Copy link
@ljharb

ljharb Dec 23, 2017

Collaborator

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

@maksimr maksimr force-pushed the maksimr:patch-perf-1 branch from 791010a to f3ffea5 Dec 23, 2017

@ljharb

This comment has been minimized.

Copy link
Collaborator

commented Dec 27, 2017

Still seeing test failures :-/

@maksimr

This comment has been minimized.

Copy link
Author

commented Dec 27, 2017

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

@maksimr

This comment has been minimized.

Copy link
Author

commented Dec 27, 2017

I will try to figure out how fix tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.