WSL: startup bash too slow with nvm loading in .bashrc config

Describe

It is too slow to startup a new bash shell, 10sec or so, with following 2 lines at the bottom of .bashrc:

export NVM_DIR=“/home/mzvast/.nvm” [ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh” # This loads nvm

In TMUX, it is extremely terrible to split a window and wait for a new bash to init.

It seems that every new bash needs to be inited with same amount of time,no matter whether there has bash instances been running in background.

Reproduce steps

  1. run curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.4/install.sh | bash
  2. reopen bash, it gets slow on init immediately

OS version,etc.

system:14393 win10 pro x64 hardware: Intel i5 2.5GHz, 6GB RAM, 256GB SSD

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 29
  • Comments: 49 (3 by maintainers)

Most upvoted comments

This is still an issue.

Problem solved fix don’t use nvm =) Thanks

We tracked down the slow nvm start-up time using xperf, and it’s a known perf bottle neck in clone\fork when copying address space information. We were already planning on the fix so it should be out to insider builds soon.

I’m using 16241 and it’s still unacceptably slow. 😕

Confirming. It’s adding ~30s to shell startup on my machine. (Core i7 6500k w/ M.2 SSD)

me@computer:~$ time (source "$NVM_DIR/nvm.sh")

real    0m29.657s
user    0m1.484s
sys     0m32.141s

Edit: Workaround. Use n instead. https://github.com/tj/n

me@computer:~$ time (source ~/.bashrc)

real    0m1.806s
user    0m0.156s
sys     0m1.594s

A fix for this should be out for insider builds soon that reduces nvm start-up by ~60%. On my test vm it took the launch from 12 seconds to 5 seconds. @benhillis reminded me that percentages are hard, and it sounds much better if we say it’s ~2.5x!!! faster 😃.

We understand where the remaining time is being spent and are tracking additional work to bring this down closer to native Linux speeds.

I’m also experiencing this. Quick fix for people who don’t use nvm too often - in your bashrc put the nvm related lines into a function and call the function when you need to use nvm. This way it doesn’t slow bash down while starting, you only need to wait right before you use it for the first time.

Gist: https://gist.github.com/KenG98/2d084a9859637cdd1614ba27485e2ef9

I spent some time trying to work around this problem on an RPi today and thought I ought to share some data in case it is helpful: strace_nvm_on_rpi4.zip

  • Since running as root I was able to slightly improve performance by increasing priority: time ionice -c 1 -n 0 nice -n -20 sh $NVM_DIR/nvm.sh # outputs ~0.8s instead of ~1.2s
  • Running from tmpfs (i.e. copying $HOME/.nvm to /tmp/.nvm_home at start-up and setting $NVM_HOME to match) didn’t seem to improve anything
  • strace -T -ttt -o strace.out sh $NVM_DIR/nvm.sh
  • perl strace_analyzer_ng_0.09-jrq.pl strace.out > analyzer_output.txt
  • Notable results:
    ----------------
    -- Time Stats --
    ----------------
    Elapsed Time for run: 0.971589 (secs) 
    Total IO Time: 0.899753 (secs) 
    Total IO Time Counter: 147 
       Percentage of Total Time = 92.606330% 
    ...
    Time for slowest read syscall (secs) = 0.551428 
       Line location in file: 288 
    ...
    
`head -n 300 strace.out |tail -n 40` # to see context around line 288
1568741598.613372 pipe([3, 4])          = 0 <0.000031>
1568741598.613458 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb6f97d18) = 5729 <0.000181>
1568741598.613723 close(4)              = 0 <0.000025>
1568741598.613811 read(3, "", 128)      = 0 <0.004820>
1568741598.618725 close(3)              = 0 <0.000029>
1568741598.618811 wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 5729 <0.000031>
1568741598.618902 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5729, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
1568741598.618946 sigreturn({mask=[]})  = 5729 <0.000019>
1568741598.619039 wait4(-1, 0xbefd2c5c, WNOHANG, NULL) = -1 ECHILD (No child processes) <0.000019>
1568741598.619154 openat(AT_FDCWD, "/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3 <0.000038>
1568741598.619255 fcntl64(1, F_DUPFD, 10) = 12 <0.000019>
1568741598.619323 close(1)              = 0 <0.000018>
1568741598.619386 fcntl64(12, F_SETFD, FD_CLOEXEC) = 0 <0.000018>
1568741598.619451 dup2(3, 1)            = 1 <0.000018>
1568741598.619522 close(3)              = 0 <0.000018>
1568741598.619585 fcntl64(2, F_DUPFD, 10) = 13 <0.000018>
1568741598.619649 close(2)              = 0 <0.000018>
1568741598.619711 fcntl64(13, F_SETFD, FD_CLOEXEC) = 0 <0.000018>
1568741598.619775 dup2(1, 2)            = 2 <0.000018>
1568741598.619846 stat64("/root/.nvm/versions/node/v12.10.0/bin/npm", {st_mode=S_IFREG|0755, st_size=4615, ...}) = 0 <0.000032>
1568741598.619949 write(1, "npm is /root/.nvm/versions/node/"..., 49) = 49 <0.000019>
1568741598.620018 dup2(12, 1)           = 1 <0.000018>
1568741598.620083 close(12)             = 0 <0.000017>
1568741598.620145 dup2(13, 2)           = 2 <0.000021>
1568741598.620213 close(13)             = 0 <0.000018>
1568741598.620285 pipe([3, 4])          = 0 <0.000028>
1568741598.620364 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb6f97d18) = 5735 <0.000169>
1568741598.620612 close(4)              = 0 <0.000019>
1568741598.620701 read(3, "/root/.nvm/versions/node/v12.10."..., 128) = 34 <0.551428>
1568741599.172269 read(3, "", 128)      = 0 <0.009788>
1568741599.182188 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5735, si_uid=0, si_status=0, si_utime=54, si_stime=4} ---
1568741599.182249 sigreturn({mask=[]})  = 0 <0.000020>
1568741599.182331 close(3)              = 0 <0.000032>
1568741599.182414 wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 5735 <0.000058>
1568741599.182556 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb6f97d18) = 5746 <0.000207>
1568741599.182877 wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 5746 <0.006748>
1568741599.189800 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5746, si_uid=0, si_status=0, si_utime=0, si_stime=1} ---
1568741599.189847 sigreturn({mask=[]})  = 5746 <0.000019>
1568741599.189929 wait4(-1, 0xbefd2f0c, WNOHANG, NULL) = -1 ECHILD (No child processes) <0.000019>
1568741599.190052 dup2(11, 1)           = 1 <0.000022>

Also, though you probably all figured this out already:

Upon running the strace command the way @arcanis suggested, several trace_files (215) were created. Is this an expected result? If so, what exactly is @sunilmut expecting from this?

The -ff option causes a separate trace file to be written for each PID, so yes (man strace.1).


TL;DR Here’s what I ultimately came up with: set path, etc. to a default node version then lazy load nvm with an alias as others have done (only tested on bash but seems to do just what I wanted):

# (updated .bashrc)

# Utility for removing an entry from $PATH -- copied from SO post:
# https://stackoverflow.com/questions/11650840/remove-redundant-paths-from-path-variable#answer-47159781
pathremove() {
    local IFS=':'
    local NEWPATH
    local DIR
    local PATHVARIABLE=${2:-PATH}
    for DIR in ${!PATHVARIABLE} ; do
        if [ "$DIR" != "$1" ] ; then
            NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
        fi
    done
    export $PATHVARIABLE="$NEWPATH"
}

export NVM_DIR="$HOME/.nvm"
DEFAULT_NODE_VERSION="v12.10.0"
load_nvm() {
    # TODO: ionice -c 1 -n 0 nice -n -20 cmd... ?
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
}
#load_nvm

# This gets node & friends into the path but doesn't initialize nvm proper until needed
lasy_load_nvm() {
    export NVM_BIN="$NVM_DIR/versions/node/$DEFAULT_NODE_VERSION/bin"
    export PATH="$NVM_BIN:$PATH"
    export NVM_CD_FLAGS=""
    alias nvm="echo 'Please wait while nvm loads' && unset NVM_CD_FLAGS && pathremove $NVM_BIN && unset NVM_BIN && unalias nvm && load_nvm && nvm $@"
}
lasy_load_nvm

So now, just after logging in, we can do this (assuming nvm install 10 was run previously):

$ node -v; echo $PATH; nvm use 10; node -v; echo $PATH
v12.10.0
/root/.nvm/versions/node/v12.10.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Please wait while nvm loads
# ... time passes here ...
Now using node v10.16.3 (npm v6.9.0)
v10.16.3
/root/.nvm/versions/node/v10.16.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I’ve wrapped nvm setup in a function, so it’s lazy-loaded but seamless to use. Give it a shot!

https://github.com/ravron/dotfiles/blob/2093bb4b257db221f31fa900cfc8cd394476a7cd/.bashrc#L233-L243

Related Thread: https://github.com/creationix/nvm/issues/782

If you installed node without nvm (the system installation), and that is what you usually use, and you use nvm for only switching to other versions of node, then this comment on that thread is a nice workaround

Great to hear. I’m going to close this out as fixed for insiders and we can take a look if anything new comes up.

Thanks for the snippet @well1791 it didn’t speed stuff up much but it did stop stuff from constantly telling me that nvm wasn’t installed when it was.

@mrmckeb thanks for the feedback. Pushing out changes to the Creator’s update is currently gated through servicing criteria which has a pretty high bar and requires data on the impact. We would like to get to the point were mid-cycle updates can go out, but unfortunately aren’t there yet. This change will likely fall into the other category where it is flighted to insider builds and available in the next official release of Windows.

after commenting the command below in .bashrc, i have removed a 4 second+ delay to the loading of the terminal, any idea how to make this faster at least?thanks, using ubuntu 16 :

export NVM_DIR=“/home/username/.nvm” [ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh” # This loads nvm

probably you should exam your .bashrc For initializing fasd would cause slow startup