tarjoilija / zgen

A lightweight and simple plugin manager for ZSH
BSD 2-Clause "Simplified" License
1.49k stars 98 forks source link

slow init time #92

Open neumachen opened 7 years ago

neumachen commented 7 years ago

It's really slow loading a new shell with this.

Here's a zprof:

-----------------------------------------------------------------------------------
 1)    1         850.23   850.23   99.98%    595.87   595.87   70.07%  zgen-init
 2)    2         127.97    63.99   15.05%    105.57    52.78   12.41%  compinit
 3)    2          57.16    28.58    6.72%     57.16    28.58    6.72%  _zsh_highlight_bind_widgets
 4)    1          49.32    49.32    5.80%     48.85    48.85    5.74%  _zsh_highlight_load_highlighters
 5)    2          22.40    11.20    2.63%     22.40    11.20    2.63%  compaudit
 6)    2           9.10     4.55    1.07%      9.10     4.55    1.07%  env_default
 7)    2           6.47     3.24    0.76%      6.47     3.24    0.76%  grep-flag-available
 8)    1           2.17     2.17    0.26%      2.17     2.17    0.26%  colors
 9)    2           1.05     0.52    0.12%      1.05     0.52    0.12%  is-at-least
10)    3           0.90     0.30    0.11%      0.90     0.30    0.11%  compdef
11)    2           0.70     0.35    0.08%      0.70     0.35    0.08%  add-zsh-hook
12)    1           0.15     0.15    0.02%      0.11     0.11    0.01%  zgen
13)    1           0.04     0.04    0.00%      0.04     0.04    0.00%  zgen-saved

And here's my zgen setup

source ${HOME}/.zgen/zgen.zsh

# check if there's no init script
if ! zgen saved; then
  echo "Creating a zgen save"

  # Load robbyrussell's oh-my-zsh's library
  zgen oh-my-zsh

  # Plugins from robbyrussell's oh-my-zsh
  zgen oh-my-zsh plugins/tmux
  zgen oh-my-zsh plugins/tmuxinator
  zgen oh-my-zsh plugins/vagrant
  zgen oh-my-zsh plugins/bundler
  zgen oh-my-zsh plugins/pip
  zgen oh-my-zsh plugins/python
  zgen oh-my-zsh plugins/virtualenv
  zgen oh-my-zsh plugins/command-not-found
  zgen oh-my-zsh plugins/history-substring-search

  # Github plugins
  zgen load rupa/z
  zgen load zsh-users/zsh-syntax-highlighting
  zgen load zsh-users/zsh-completions src
  zgen load kennethreitz/autoenv

  # Load theme
  zgen oh-my-zsh themes/ys

  # Tell zgen that you're done
  zgen save
fi
Tuurlijk commented 7 years ago

I've been shaving microseconds the last couple of days. And I have found that replacing zgen with a shim function saves time:

# Load zgen only if a user types a zgen command
zgen () {
    if [[ ! -s ${ZDOTDIR:-${HOME}}/.zgen/zgen.zsh ]]; then
        git clone --recursive https://github.com/tarjoilija/zgen.git ${ZDOTDIR:-${HOME}}/.zgen
    fi
    source ${ZDOTDIR:-${HOME}}/.zgen/zgen.zsh
    zgen "$@"
}

Then in your init check script, you can use:

# check if there's no init script
if [[ ! -s ${ZDOTDIR:-${HOME}}/.zgen/init.zsh ]]; then
  echo "Creating a zgen save"
else
  source ${ZDOTDIR:-${HOME}}/.zgen/init.zsh
fi

And maybe you want to do:

zcompile ${ZDOTDIR:-${HOME}}/.zgen/init.zsh

After generating the init script. Right after zgen save.

This gives me:

Using zgen

repeat 10 {time zsh -i -c exit}
zsh -i -c exit  0.09s user 0.03s system 98% cpu 0.124 total
zsh -i -c exit  0.09s user 0.03s system 97% cpu 0.118 total
zsh -i -c exit  0.09s user 0.03s system 97% cpu 0.126 total
zsh -i -c exit  0.08s user 0.03s system 98% cpu 0.110 total
zsh -i -c exit  0.08s user 0.03s system 98% cpu 0.117 total
zsh -i -c exit  0.08s user 0.03s system 98% cpu 0.117 total
zsh -i -c exit  0.10s user 0.03s system 98% cpu 0.130 total
zsh -i -c exit  0.09s user 0.03s system 98% cpu 0.122 total
zsh -i -c exit  0.08s user 0.03s system 98% cpu 0.111 total
zsh -i -c exit  0.08s user 0.03s system 98% cpu 0.111 total

Using shim and direct test against and sourcing of init.zsh

repeat 10 {time zsh -i -c exit}
zsh -i -c exit  0.05s user 0.02s system 96% cpu 0.068 total
zsh -i -c exit  0.05s user 0.02s system 97% cpu 0.073 total
zsh -i -c exit  0.05s user 0.02s system 95% cpu 0.072 total
zsh -i -c exit  0.05s user 0.02s system 96% cpu 0.068 total
zsh -i -c exit  0.05s user 0.02s system 96% cpu 0.072 total
zsh -i -c exit  0.05s user 0.02s system 96% cpu 0.071 total
zsh -i -c exit  0.05s user 0.02s system 97% cpu 0.070 total
zsh -i -c exit  0.05s user 0.02s system 96% cpu 0.076 total
zsh -i -c exit  0.05s user 0.02s system 97% cpu 0.068 total
zsh -i -c exit  0.05s user 0.02s system 97% cpu 0.073 total

Does something like that improve loading times for you?

smac89 commented 6 years ago

Ironically, not saving the configuration is faster than saving it.

With saving

➜ for i in {1..5}; do /usr/bin/time zsh -i -c exit; done
1.62user 0.28system 0:01.86elapsed 102%CPU (0avgtext+0avgdata 36988maxresident)k
0inputs+48outputs (0major+111549minor)pagefaults 0swaps
1.59user 0.33system 0:01.86elapsed 103%CPU (0avgtext+0avgdata 36936maxresident)k
0inputs+48outputs (0major+111176minor)pagefaults 0swaps
1.59user 0.33system 0:01.87elapsed 102%CPU (0avgtext+0avgdata 37436maxresident)k
0inputs+48outputs (0major+111984minor)pagefaults 0swaps
1.59user 0.32system 0:01.84elapsed 103%CPU (0avgtext+0avgdata 37096maxresident)k
0inputs+48outputs (0major+111236minor)pagefaults 0swaps
1.62user 0.30system 0:01.87elapsed 103%CPU (0avgtext+0avgdata 36984maxresident)k
0inputs+48outputs (0major+111390minor)pagefaults 0swaps

Without saving:

➜ for i in {1..5}; do /usr/bin/time zsh -i -c exit; done
1.45user 0.29system 0:01.75elapsed 99%CPU (0avgtext+0avgdata 33328maxresident)k
0inputs+64outputs (0major+81763minor)pagefaults 0swaps
1.35user 0.25system 0:01.60elapsed 100%CPU (0avgtext+0avgdata 33368maxresident)k
0inputs+64outputs (0major+81667minor)pagefaults 0swaps
1.33user 0.27system 0:01.60elapsed 100%CPU (0avgtext+0avgdata 33280maxresident)k
0inputs+64outputs (0major+81729minor)pagefaults 0swaps
1.39user 0.30system 0:01.69elapsed 100%CPU (0avgtext+0avgdata 33340maxresident)k
0inputs+64outputs (0major+81740minor)pagefaults 0swaps
1.36user 0.27system 0:01.62elapsed 100%CPU (0avgtext+0avgdata 33408maxresident)k
0inputs+64outputs (0major+81695minor)pagefaults 0swaps

:face_with_head_bandage:

I could do with more time saving, but this is a start

jandamm commented 3 years ago

I've created a maintained fork of zgen - dubbed zgenom - which fixes some bugs as well as introducing new features like lazy loading and compiling the source files which should improve this issue.