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

load-nvmrc is slow #1694

Closed
maksimr opened this issue Dec 22, 2017 · 4 comments · Fixed by #2317
Closed

load-nvmrc is slow #1694

maksimr opened this issue Dec 22, 2017 · 4 comments · Fixed by #2317
Labels
performance This relates to anything regarding the speed of using nvm. pull request wanted This is a great way to contribute! Help us out :-D shell: zsh

Comments

@maksimr
Copy link

maksimr commented Dec 22, 2017

load-nvmrc[1] function is very slow.
It spends around 220ms on check and it runs on every change directory(cd)

OS: macOs High Sierra
Processor: 2,2 GHz Intel Core i7
Termina: iTerm2(3.1.5)
Shell: zsh 5.3 (x86_64-apple-darwin17.0)

Test:

#!/usr/bin/env zsh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

load-nvmrc() {
    local node_version="$(nvm version)"
    local nvmrc_path="$(nvm_find_nvmrc)"

    if [ -n "$nvmrc_path" ]; then
      local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
      if [ "$nvmrc_node_version" = "N/A" ]; then
        nvm install
      elif [ "$nvmrc_node_version" != "$node_version" ]; then
        nvm use
      fi
    elif [ "$node_version" != "$(nvm version default)" ]; then
      nvm use default
    fi
}

zmodload zsh/zprof
load-nvmrc
zprof

load-nvmrc-profile

We could reduce time to 60ms if remove elif clause but 60ms is still a measurable delay.

load-nvmrc() {
    local node_version="$(nvm version)"
    local nvmrc_path="$(nvm_find_nvmrc)"

    if [ -n "$nvmrc_path" ]; then
      local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
      if [ "$nvmrc_node_version" = "N/A" ]; then
        nvm install
      elif [ "$nvmrc_node_version" != "$node_version" ]; then
        nvm use
      fi
    fi
}

nvm version takes 53ms

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    2          31.10    15.55   53.93%     31.10    15.55   53.93%  nvm_tree_contains_path
 2)    1          49.65    49.65   86.09%     15.99    15.99   27.72%  nvm_ls_current
 3)    2          57.65    28.82   99.96%      7.91     3.96   13.72%  nvm
 4)    1           2.56     2.56    4.44%      2.56     2.56    4.44%  nvm_echo
 5)    1          49.74    49.74   86.24%      0.09     0.09    0.15%  nvm_version
 6)    1          57.67    57.67  100.00%      0.02     0.02    0.04%  load-nvmrc

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

 6)    1          57.67    57.67  100.00%      0.02     0.02    0.04%  load-nvmrc
       1/2        57.65    57.65   99.96%      4.05     4.05             nvm [3]

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

       1/2        57.65    57.65   99.96%      4.05     4.05             load-nvmrc [6]
       1/2        53.60    53.60   92.95%      3.86     3.86             nvm [3]
 3)    2          57.65    28.82   99.96%      7.91     3.96   13.72%  nvm
       1/1        49.74    49.74   86.24%      0.09     0.09             nvm_version [5]
       1/2        53.60    53.60   92.95%      3.86     3.86             nvm [3]

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

       1/1        49.74    49.74   86.24%      0.09     0.09             nvm [3]
 5)    1          49.74    49.74   86.24%      0.09     0.09    0.15%  nvm_version
       1/1        49.65    49.65   86.09%     15.99    15.99             nvm_ls_current [2]

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

       1/1        49.65    49.65   86.09%     15.99    15.99             nvm_version [5]
 2)    1          49.65    49.65   86.09%     15.99    15.99   27.72%  nvm_ls_current
       1/1         2.56     2.56    4.44%      2.56     2.56             nvm_echo [4]
       2/2        31.10    15.55   53.93%     31.10    15.55             nvm_tree_contains_path [1]

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

       2/2        31.10    15.55   53.93%     31.10    15.55             nvm_ls_current [2]
 1)    2          31.10    15.55   53.93%     31.10    15.55   53.93%  nvm_tree_contains_path

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

       1/1         2.56     2.56    4.44%      2.56     2.56             nvm_ls_current [2]
 4)    1           2.56     2.56    4.44%      2.56     2.56    4.44%  nvm_echo

[1] - https://github.com/creationix/nvm#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file

@ljharb
Copy link
Member

ljharb commented Dec 23, 2017

This is just a suggestion in the readme; its code isn't actually part of nvm. If you'd like to PR in improvements to it, that'd be appreciated!

@ljharb ljharb added informational shell: zsh pull request wanted This is a great way to contribute! Help us out :-D labels Dec 23, 2017
@maksimr
Copy link
Author

maksimr commented Dec 23, 2017

@ljharb that is true, but this function use nvm's functions

for example take nvm_tree_contains_path

2          31.10    15.55   53.93%     31.10    15.55                  nvm_ls_current [2]
2          31.10    15.55   53.93%     31.10    15.55   53.93%  nvm_tree_contains_path

it takes 31ms

and if change this code

  local pathdir
  pathdir=$(dirname "${node_path}")
  while [ "${pathdir}" != "" ] && [ "${pathdir}" != "." ] && [ "${pathdir}" != "/" ] && [ "${pathdir}" != "${tree}" ]; do
    pathdir=$(dirname "${pathdir}")
  done
  [ "${pathdir}" = "${tree}" ]

on something like this

  [ "$tree${node_path#$tree}" = "${node_path}" ]

you could reduce the time from 31.10 to 0.11ms

Thanks

@ljharb
Copy link
Member

ljharb commented Dec 23, 2017

@maksimr now that seems much more compelling a change to me. Would you be willing to make a PR for that? :-D

@ljharb ljharb added performance This relates to anything regarding the speed of using nvm. and removed informational labels Dec 23, 2017
@baaart
Copy link

baaart commented Nov 13, 2018

I have similar problem nvm takes to much time to start, stats below. Any sollution for that?

-----------------------------------------------------------------------------------
1)    1         651.20   651.20   32.86%    651.05   651.05   32.85%  nvm_die_on_prefix
2)    2        1504.45   752.22   75.91%    553.93   276.96   27.95%  nvm
3)    1        1800.55  1800.55   90.85%    296.10   296.10   14.94%  nvm_auto
4)    1         299.13   299.13   15.09%    268.28   268.28   13.54%  nvm_ensure_version_installed
5)    2         140.77    70.39    7.10%    110.81    55.40    5.59%  compinit
6)    1          30.85    30.85    1.56%     30.85    30.85    1.56%  nvm_is_version_installed
7)    4          29.96     7.49    1.51%     29.96     7.49    1.51%  compaudit
8)    1          12.31    12.31    0.62%     12.31    12.31    0.62%  nvm_supports_source_options
9)    2           8.19     4.09    0.41%      8.19     4.09    0.41%  grep-flag-available
10)    2           7.88     3.94    0.40%      7.88     3.94    0.40%  env_default
11)   56           5.49     0.10    0.28%      5.49     0.10    0.28%  __git_alias
12)   18           1.72     0.10    0.09%      1.72     0.10    0.09%  compdef
13)    1           1.67     1.67    0.08%      1.67     1.67    0.08%  _git_wrap_commands
14)    1           0.91     0.91    0.05%      0.91     0.91    0.05%  colors
15)   18           1.14     0.06    0.06%      0.65     0.04    0.03%  _alias
16)   18           0.49     0.03    0.02%      0.49     0.03    0.02%  _safe_alias
17)    3           0.42     0.14    0.02%      0.42     0.14    0.02%  nvm_has
18)    1           0.35     0.35    0.02%      0.35     0.35    0.02%  is-at-least
19)    1           0.34     0.34    0.02%      0.34     0.34    0.02%  add-zsh-hook
20)    4           0.17     0.04    0.01%      0.17     0.04    0.01%  _bind
21)    1           0.30     0.30    0.02%      0.15     0.15    0.01%  complete
22)    3           0.09     0.03    0.00%      0.09     0.03    0.00%  is_plugin
23)    1        1812.94  1812.94   91.47%      0.07     0.07    0.00%  nvm_process_parameters
24)    1           0.02     0.02    0.00%      0.02     0.02    0.00%  bashcompinit```

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. pull request wanted This is a great way to contribute! Help us out :-D shell: zsh
Projects
None yet
3 participants