Open3

zsh起動高速化奮闘記

antyuntyunantyuntyun

2021/12/06 早朝

# zplugで設定中の現在のシェル。とても重い
$ time (zsh -i -c exit;)
( zsh -i -c exit; )  2.31s user 2.47s system 76% cpu 6.217 total

# zinitが早いという記事がググるとよく出るのでレポジトリみにいってみる
# これはなぜかnot found
# https://github.com/zdharma/zinit
# 削除されていていまは有志がforkしたものしかない模様
# https://github.com/zdharma-continuum/zinit

# zinitにしてみた
 $ time (zsh -i -c exit;)                                                                                        [5:39:24]
( zsh -i -c exit; )  1.54s user 2.04s system 67% cpu 5.294 total
# あまりかわらなかった

# ~/.zshrcの最初にこれ
zmodload zsh/zprof && zprof
# ~/.zshrの最後にこれ
if (which zprof > /dev/null 2>&1) ;then
  zprof
fi
# 上記追記して計測してgoenv筆頭にenv系が遅いことがわかった
$ time (zsh -i -c exit;)                                                                                                                    [5:42:13]
num  calls                time                       self            name
-----------------------------------------------------------------------------------
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1         142.14   142.14   30.11%    142.14   142.14   30.11%  goenv
 2)    2         138.99    69.50   29.45%    138.99    69.50   29.45%  compaudit
 3)    1         102.79   102.79   21.78%    102.79   102.79   21.78%  _jenv_export_hook
 4)    1         172.59   172.59   36.57%     33.60    33.60    7.12%  compinit
 5)    1          16.30    16.30    3.45%     15.13    15.13    3.21%  prompt_lime_setup
 6)    2           6.40     3.20    1.35%      6.40     3.20    1.35%  promptinit
 7)    6           4.04     0.67    0.86%      4.04     0.67    0.86%  .zinit-diff-parameter
 8)    3          35.14    11.71    7.45%      3.22     1.07    0.68%  .zinit-load-plugin
 9)    6           3.04     0.51    0.64%      3.04     0.51    0.64%  .zinit-diff-functions
10)    3          40.48    13.49    8.58%      2.16     0.72    0.46%  zinit
11)    9           2.33     0.26    0.49%      1.89     0.21    0.40%  :zinit-tmp-subst-zle
12)    1           1.88     1.88    0.40%      1.88     1.88    0.40%  colors
13)    3          37.68    12.56    7.98%      1.37     0.46    0.29%  .zinit-load
14)    6           1.36     0.23    0.29%      1.36     0.23    0.29%  .zinit-diff-env
15)    3           1.36     0.45    0.29%      1.36     0.45    0.29%  is-at-least
16)    1           1.21     1.21    0.26%      1.21     1.21    0.26%  bashcompinit
17)    3           1.49     0.50    0.32%      1.20     0.40    0.25%  add-zsh-hook
18)   22           1.13     0.05    0.24%      1.13     0.05    0.24%  @zinit-register-hook
19)    3           1.27     0.42    0.27%      0.96     0.32    0.20%  :zinit-tmp-subst-autoload
20)    3           0.95     0.32    0.20%      0.95     0.32    0.20%  .zinit-tmp-subst-off
21)    6           9.94     1.66    2.11%      0.88     0.15    0.19%  .zinit-diff
22)   29           0.78     0.03    0.16%      0.78     0.03    0.16%  .zinit-add-report
23)    3           0.67     0.22    0.14%      0.67     0.22    0.14%  .zinit-tmp-subst-on
24)    6           0.61     0.10    0.13%      0.61     0.10    0.13%  .zinit-diff-options
25)    7           2.74     0.39    0.58%      0.47     0.07    0.10%  (anon)
26)    4           0.56     0.14    0.12%      0.46     0.11    0.10%  :zinit-tmp-subst-zstyle
27)    6           0.46     0.08    0.10%      0.46     0.08    0.10%  .zinit-set-m-func
28)    4           0.40     0.10    0.08%      0.40     0.10    0.08%  .zinit-get-mtime-into
29)    3           0.39     0.13    0.08%      0.39     0.13    0.08%  .zinit-register-plugin
30)    6           0.39     0.06    0.08%      0.39     0.06    0.08%  .zinit-any-to-user-plugin
31)    3           0.29     0.10    0.06%      0.29     0.10    0.06%  .zinit-ice
32)    1           0.46     0.46    0.10%      0.24     0.24    0.05%  complete
33)    1           0.22     0.22    0.05%      0.22     0.22    0.05%  compdef
34)    3          37.88    12.63    8.03%      0.20     0.07    0.04%  .zinit-load-object
35)    9           2.50     0.28    0.53%      0.17     0.02    0.04%  zle
36)    1           0.17     0.17    0.04%      0.17     0.17    0.04%  .zinit-prepare-home
37)    3           0.13     0.04    0.03%      0.13     0.04    0.03%  .zinit-pack-ice
38)    4           0.66     0.17    0.14%      0.10     0.03    0.02%  zstyle
39)    3           1.34     0.45    0.28%      0.07     0.02    0.01%  autoload
40)    1           0.07     0.07    0.01%      0.07     0.07    0.01%  install_hook

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

 4)    1         172.59   172.59   36.57%     33.60    33.60    7.12%  compinit
       1/2       138.99   138.99   29.45%      1.56     1.56             compaudit [2]

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

 1)    1         142.14   142.14   30.11%    142.14   142.14   30.11%  goenv

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

       1/2       138.99   138.99   29.45%      1.56     1.56             compinit [4]
       1/2       137.43   137.43   29.12%    137.43   137.43             compaudit [2]
 2)    2         138.99    69.50   29.45%    138.99    69.50   29.45%  compaudit
       1/2       137.43   137.43   29.12%    137.43   137.43             compaudit [2]

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

 3)    1         102.79   102.79   21.78%    102.79   102.79   21.78%  _jenv_export_hook

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

10)    3          40.48    13.49    8.58%      2.16     0.72    0.46%  zinit
       6/7         0.14     0.02    0.03%      0.14     0.02             (anon) [25]
       3/3         0.29     0.10    0.06%      0.29     0.10             .zinit-ice [31]
       3/3        37.88    12.63    8.03%      0.20     0.07             .zinit-load-object [34]

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

       3/3        37.88    12.63    8.03%      0.20     0.07             zinit [10]
34)    3          37.88    12.63    8.03%      0.20     0.07    0.04%  .zinit-load-object
       3/3        37.68    12.56    7.98%      1.37     0.46             .zinit-load [13]

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

       3/3        37.68    12.56    7.98%      1.37     0.46             .zinit-load-object [34]
13)    3          37.68    12.56    7.98%      1.37     0.46    0.29%  .zinit-load
       3/3         0.13     0.04    0.03%      0.13     0.04             .zinit-pack-ice [37]
       3/6         0.20     0.07    0.04%      0.20     0.07             .zinit-any-to-user-plugin [30]
       3/3         0.39     0.13    0.08%      0.39     0.13             .zinit-register-plugin [29]
       6/6         0.46     0.08    0.10%      0.46     0.08             .zinit-set-m-func [27]
       3/3        35.14    11.71    7.45%      3.22     1.07             .zinit-load-plugin [8]

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

       3/3        35.14    11.71    7.45%      3.22     1.07             .zinit-load [13]
 8)    3          35.14    11.71    7.45%      3.22     1.07    0.68%  .zinit-load-plugin
       3/29        0.13     0.04    0.03%      0.13     0.04             .zinit-add-report [22]
       1/3         0.14     0.14    0.03%      0.14     0.14             is-at-least [15]
       1/9         0.23     0.23    0.05%      0.02     0.02             zle [35]
       1/3         0.42     0.42    0.09%      0.13     0.13             add-zsh-hook [17]
       1/3         0.55     0.55    0.12%      0.02     0.02             autoload [39]
       3/3         0.67     0.22    0.14%      0.67     0.22             .zinit-tmp-subst-on [23]
       3/3         0.95     0.32    0.20%      0.95     0.32             .zinit-tmp-subst-off [20]
       1/7         2.60     2.60    0.55%      0.33     0.33             (anon) [25]
       6/6         9.94     1.66    2.11%      0.88     0.15             .zinit-diff [21]
       1/1        16.30    16.30    3.45%     15.13    15.13             prompt_lime_setup [5]

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

       1/1        16.30    16.30    3.45%     15.13    15.13             .zinit-load-plugin [8]
 5)    1          16.30    16.30    3.45%     15.13    15.13    3.21%  prompt_lime_setup
       1/3         0.51     0.51    0.11%      0.03     0.03             autoload [39]
       4/4         0.66     0.17    0.14%      0.10     0.03             zstyle [38]

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

       6/6         9.94     1.66    2.11%      0.88     0.15             .zinit-load-plugin [8]
21)    6           9.94     1.66    2.11%      0.88     0.15    0.19%  .zinit-diff
       6/6         0.61     0.10    0.13%      0.61     0.10             .zinit-diff-options [24]
       6/6         1.36     0.23    0.29%      1.36     0.23             .zinit-diff-env [14]
       6/6         3.04     0.51    0.64%      3.04     0.51             .zinit-diff-functions [9]
       6/6         4.04     0.67    0.86%      4.04     0.67             .zinit-diff-parameter [7]

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

       1/2         5.02     5.02    1.06%      5.02     5.02             promptinit [6]
 6)    2           6.40     3.20    1.35%      6.40     3.20    1.35%  promptinit
       1/2         5.02     5.02    1.06%      5.02     5.02             promptinit [6]

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

       6/6         4.04     0.67    0.86%      4.04     0.67             .zinit-diff [21]
 7)    6           4.04     0.67    0.86%      4.04     0.67    0.86%  .zinit-diff-parameter

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

       6/6         3.04     0.51    0.64%      3.04     0.51             .zinit-diff [21]
 9)    6           3.04     0.51    0.64%      3.04     0.51    0.64%  .zinit-diff-functions

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

       1/7         2.60     2.60    0.55%      0.33     0.33             .zinit-load-plugin [8]
       6/7         0.14     0.02    0.03%      0.14     0.02             zinit [10]
25)    7           2.74     0.39    0.58%      0.47     0.07    0.10%  (anon)
       8/9         2.27     0.28    0.48%      0.15     0.02             zle [35]

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

       8/9         2.27     0.28    0.48%      0.15     0.02             (anon) [25]
       1/9         0.23     0.23    0.05%      0.02     0.02             .zinit-load-plugin [8]
35)    9           2.50     0.28    0.53%      0.17     0.02    0.04%  zle
       9/9         2.33     0.26    0.49%      1.89     0.21             :zinit-tmp-subst-zle [11]

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

       9/9         2.33     0.26    0.49%      1.89     0.21             zle [35]
11)    9           2.33     0.26    0.49%      1.89     0.21    0.40%  :zinit-tmp-subst-zle
      18/29        0.43     0.02    0.09%      0.43     0.02             .zinit-add-report [22]

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

12)    1           1.88     1.88    0.40%      1.88     1.88    0.40%  colors

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

       1/3         0.42     0.42    0.09%      0.13     0.13             .zinit-load-plugin [8]
17)    3           1.49     0.50    0.32%      1.20     0.40    0.25%  add-zsh-hook
       1/3         0.29     0.29    0.06%      0.02     0.02             autoload [39]

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

       6/6         1.36     0.23    0.29%      1.36     0.23             .zinit-diff [21]
14)    6           1.36     0.23    0.29%      1.36     0.23    0.29%  .zinit-diff-env

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

       1/3         0.14     0.14    0.03%      0.14     0.14             .zinit-load-plugin [8]
15)    3           1.36     0.45    0.29%      1.36     0.45    0.29%  is-at-least

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

       1/3         0.55     0.55    0.12%      0.02     0.02             .zinit-load-plugin [8]
       1/3         0.51     0.51    0.11%      0.03     0.03             prompt_lime_setup [5]
       1/3         0.29     0.29    0.06%      0.02     0.02             add-zsh-hook [17]
39)    3           1.34     0.45    0.28%      0.07     0.02    0.01%  autoload
       3/3         1.27     0.42    0.27%      0.96     0.32             :zinit-tmp-subst-autoload [19]

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

       3/3         1.27     0.42    0.27%      0.96     0.32             autoload [39]
19)    3           1.27     0.42    0.27%      0.96     0.32    0.20%  :zinit-tmp-subst-autoload
       4/29        0.12     0.03    0.02%      0.12     0.03             .zinit-add-report [22]
       3/6         0.19     0.06    0.04%      0.19     0.06             .zinit-any-to-user-plugin [30]

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

16)    1           1.21     1.21    0.26%      1.21     1.21    0.26%  bashcompinit

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

18)   22           1.13     0.05    0.24%      1.13     0.05    0.24%  @zinit-register-hook

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

       3/3         0.95     0.32    0.20%      0.95     0.32             .zinit-load-plugin [8]
20)    3           0.95     0.32    0.20%      0.95     0.32    0.20%  .zinit-tmp-subst-off

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

      18/29        0.43     0.02    0.09%      0.43     0.02             :zinit-tmp-subst-zle [11]
       3/29        0.13     0.04    0.03%      0.13     0.04             .zinit-load-plugin [8]
       4/29        0.12     0.03    0.02%      0.12     0.03             :zinit-tmp-subst-autoload [19]
       4/29        0.10     0.02    0.02%      0.10     0.02             :zinit-tmp-subst-zstyle [26]
22)   29           0.78     0.03    0.16%      0.78     0.03    0.16%  .zinit-add-report

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

       3/3         0.67     0.22    0.14%      0.67     0.22             .zinit-load-plugin [8]
23)    3           0.67     0.22    0.14%      0.67     0.22    0.14%  .zinit-tmp-subst-on

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

       4/4         0.66     0.17    0.14%      0.10     0.03             prompt_lime_setup [5]
38)    4           0.66     0.17    0.14%      0.10     0.03    0.02%  zstyle
       4/4         0.56     0.14    0.12%      0.46     0.11             :zinit-tmp-subst-zstyle [26]

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

       6/6         0.61     0.10    0.13%      0.61     0.10             .zinit-diff [21]
24)    6           0.61     0.10    0.13%      0.61     0.10    0.13%  .zinit-diff-options

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

       4/4         0.56     0.14    0.12%      0.46     0.11             zstyle [38]
26)    4           0.56     0.14    0.12%      0.46     0.11    0.10%  :zinit-tmp-subst-zstyle
       4/29        0.10     0.02    0.02%      0.10     0.02             .zinit-add-report [22]

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

32)    1           0.46     0.46    0.10%      0.24     0.24    0.05%  complete
       1/1         0.22     0.22    0.05%      0.22     0.22             compdef [33]

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

       6/6         0.46     0.08    0.10%      0.46     0.08             .zinit-load [13]
27)    6           0.46     0.08    0.10%      0.46     0.08    0.10%  .zinit-set-m-func

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

28)    4           0.40     0.10    0.08%      0.40     0.10    0.08%  .zinit-get-mtime-into

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

       3/3         0.39     0.13    0.08%      0.39     0.13             .zinit-load [13]
29)    3           0.39     0.13    0.08%      0.39     0.13    0.08%  .zinit-register-plugin

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

       3/6         0.20     0.07    0.04%      0.20     0.07             .zinit-load [13]
       3/6         0.19     0.06    0.04%      0.19     0.06             :zinit-tmp-subst-autoload [19]
30)    6           0.39     0.06    0.08%      0.39     0.06    0.08%  .zinit-any-to-user-plugin

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

       3/3         0.29     0.10    0.06%      0.29     0.10             zinit [10]
31)    3           0.29     0.10    0.06%      0.29     0.10    0.06%  .zinit-ice

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

       1/1         0.22     0.22    0.05%      0.22     0.22             complete [32]
33)    1           0.22     0.22    0.05%      0.22     0.22    0.05%  compdef

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

36)    1           0.17     0.17    0.04%      0.17     0.17    0.04%  .zinit-prepare-home

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

       3/3         0.13     0.04    0.03%      0.13     0.04             .zinit-load [13]
37)    3           0.13     0.04    0.03%      0.13     0.04    0.03%  .zinit-pack-ice

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

40)    1           0.07     0.07    0.01%      0.07     0.07    0.01%  install_hook
( zsh -i -c exit; )  1.34s user 1.71s system 84% cpu 3.619 total
antyuntyunantyuntyun
# anyenvの初期化処理が戦犯
$ time (eval "$(anyenv init - )")                                                                                                          [5:50:17]
( eval "$(anyenv init - )"; )  1.18s user 1.76s system 64% cpu 4.550 total

# asdfに乗り換えはせずにanyenvでやっていきたい
# --no-rehash つけることにする。必要な時にanyenv init 打てばなんとかなる気がするので
if [ -d $HOME/.anyenv ]
then
    export PATH="$HOME/.anyenv/bin:$PATH"
    eval "$(anyenv init --no-rehash - zsh)"
fi
# それでも1秒を切れない
$ time (zsh -i -c exit;)                                                       [6:00:11]
( zsh -i -c exit; )  1.00s user 1.39s system 75% cpu 3.156 total

# こちら参考にしてみる
https://zenn.dev/ktakayama/articles/27b9d6218ed2f0ee9992
# こうしてみた。$HOMEとチルダがあるのは一旦気にしない
if [ -d $HOME/.anyenv ]
then
    export PATH="$HOME/.anyenv/bin:$PATH"
    # eval "$(anyenv init --no-rehash - zsh)"
   if ! [ -f ~/.cache/anyenv.cache ]
   then
      anyenv init - --no-rehash > ~/.cache/anyenv.cache
      zcompile ~/.cache/anyenv.cache
   fi
   source ~/.cache/anyenv.cache
fi

# それでも1秒を切れない
$ time (zsh -i -c exit;)                                                       [6:07:22]
( zsh -i -c exit; )  0.47s user 0.53s system 92% cpu 1.083 total

# anyenvで設定したらenv系全部に継承してくれてないのだろうか
# 使っている全env書いてみる
if [ -d $HOME/.anyenv ]
then
    export PATH="$HOME/.anyenv/bin:$PATH"
    # eval "$(anyenv init --no-rehash - zsh)"
   if ! [ -f ~/.cache/anyenv.cache ]
   then
      anyenv init - --no-rehash > ~/.cache/anyenv.cache
      zcompile ~/.cache/anyenv.cache
   fi
   source ~/.cache/anyenv.cache

   if ! [ -f ~/.cache/pyenv.cache ]
   then
      pyenv init - --no-rehash > ~/.cache/pyenv.cache
      zcompile ~/.cache/pyenv.cache
   fi
   source ~/.cache/pyenv.cache

   if ! [ -f ~/.cache/goenv.cache ]
   then
      goenv init - --no-rehash > ~/.cache/goenv.cache
      zcompile ~/.cache/goenv.cache
   fi
   source ~/.cache/goenv.cache

   if ! [ -f ~/.cache/nodenv.cache ]
   then
      nodenv init - --no-rehash > ~/.cache/nodenv.cache
      zcompile ~/.cache/nodenv.cache
   fi
   source ~/.cache/nodenv.cache

   if ! [ -f ~/.cache/jenv.cache ]
   then
      jenv init - --no-rehash > ~/.cache/jenv.cache
      zcompile ~/.cache/jenv.cache
   fi
   source ~/.cache/jenv.cache

   if ! [ -f ~/.cache/rbenv.cache ]
   then
      rbenv init - --no-rehash > ~/.cache/rbenv.cache
      zcompile ~/.cache/rbenv.cache
   fi
   source ~/.cache/rbenv.cache

   if ! [ -f ~/.cache/Renv.cache ]
   then
      Renv init - --no-rehash > ~/.cache/Renv.cache
      zcompile ~/.cache/Renv.cache
   fi
   source ~/.cache/Renv.cache

fi

# なんだかむしろ遅くなった。anyenvだけキャッシュする状態に戻す

# もう一度みてみる
# goenvがやはり遅い
$ time (zsh -i -c exit;)                                                       [6:14:09]
num  calls                time                       self            name
-----------------------------------------------------------------------------------
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1         131.95   131.95   32.92%    131.95   131.95   32.92%  goenv
 2)    1          98.33    98.33   24.53%     98.33    98.33   24.53%  _jenv_export_hook
 3)    2          91.20    45.60   22.75%     91.20    45.60   22.75%  compaudit

# ghqでgo(env)にはお世話になっているからなんとかしたい
# なんとか1秒切りたいのでまたいつか頑張る