LoginSignup
5
4

More than 5 years have passed since last update.

zplugちょっとだけ高速化

Posted at

TL;DR

パッケージの追加を毎回するのではなく、必要な時だけするようにすると少し早くなる。

source $ZPLUG_HOME/init.zsh

cache_dir=$HOME/.zplug/cache

if [ -d $cache_dir ]; then
  plugins=$HOME/.zsh.d/zplug_plugin.zsh
  cache=$cache_dir/interface
  plugin_date=$(date -r $plugins +%s)
  cache_date=$(date -r $cache +%s)
  # echo $package_date $cache_date
  if [ ! -d $cache_dir -o $plugin_date -gt $cache_date ]; then
    . $HOME/.zsh.d/zplug_plugin.zsh
  fi
else
  . $HOME/.zsh.d/zplug_plugin.zsh
fi

zplug load

背景

以前脱zplugをしたが新しいパソコンに移行した時マニュアル管理が面倒になったのでzplugに復帰した(笑)

新しいパソコンでロード時間が伸びても体感ではそこまで遅く感じないというのもある。(zshrcのロード以前のラグが多少あるみたい)

でもやっぱり少し遅いのでなんとかならないかと調べてみたところ、zplugは一度実行するとcacheを作るので毎回プラグインを追加する必要がないということに気づいた。
なので、pluginを追加する処理を別のファイルにしてcacheと比べて新しい場合だけロードするようにした。

具体的にはつぎのようにしてプラグインの追加処理をzplug_plugin.zshに分離して、cacheの更新日時と比べて新しい場合はロードしている。

source $ZPLUG_HOME/init.zsh

cache_dir=$HOME/.zplug/cache

if [ -d $cache_dir ]; then
  plugins=$HOME/.zsh.d/zplug_plugin.zsh
  cache=$cache_dir/interface
  plugin_date=$(date -r $plugins +%s)
  cache_date=$(date -r $cache +%s)
  # echo $package_date $cache_date
  if [ ! -d $cache_dir -o $plugin_date -gt $cache_date ]; then
    . $HOME/.zsh.d/zplug_plugin.zsh
  fi
else
  . $HOME/.zsh.d/zplug_plugin.zsh
fi

zplug load

zplug_plugin.zshは以下のような感じ

zplug "zsh-users/zsh-completions"
zplug "zsh-users/zsh-autosuggestions"
zplug "zsh-users/zsh-syntax-highlighting", defer:2

zplug "mafredri/zsh-async"
zplug "sindresorhus/pure"

zplug "b4b4r07/enhancd", use:"init.sh"
# zplug "mollifier/anyframe"
zplug "vintersnow/anyframe"
loadlib $DOTFILES/.zsh.d/any_frame.zsh

zplug "marzocchi/zsh-notify", if:"has 'terminal-notifier' || has 'notify-send'"
zstyle ':notify:*' command-complete-timeout 10

zplug "Tarrasch/zsh-colors"

zplug "lukechilds/zsh-nvm"

結果

毎回zplug_plugin.zshを読んだ場合

❯  for i in $(seq 1 10); do time zsh -i -c exit; done
zsh -i -c exit  0.13s user 0.06s system 71% cpu 0.265 total
zsh -i -c exit  0.12s user 0.08s system 72% cpu 0.267 total
zsh -i -c exit  0.12s user 0.07s system 71% cpu 0.266 total
zsh -i -c exit  0.12s user 0.07s system 71% cpu 0.267 total
zsh -i -c exit  0.11s user 0.08s system 71% cpu 0.266 total
zsh -i -c exit  0.11s user 0.08s system 71% cpu 0.270 total
zsh -i -c exit  0.13s user 0.06s system 72% cpu 0.268 total
zsh -i -c exit  0.10s user 0.10s system 72% cpu 0.268 total
zsh -i -c exit  0.13s user 0.06s system 72% cpu 0.263 total
zsh -i -c exit  0.13s user 0.06s system 71% cpu 0.270 total

改良版

❯  for i in $(seq 1 10); do time zsh -i -c exit; done
zsh -i -c exit  0.43s user 0.22s system 87% cpu 0.743 total
zsh -i -c exit  0.11s user 0.05s system 77% cpu 0.212 total
zsh -i -c exit  0.12s user 0.05s system 78% cpu 0.213 total
zsh -i -c exit  0.12s user 0.04s system 78% cpu 0.208 total
zsh -i -c exit  0.12s user 0.04s system 78% cpu 0.209 total
zsh -i -c exit  0.12s user 0.05s system 78% cpu 0.216 total
zsh -i -c exit  0.11s user 0.05s system 79% cpu 0.210 total
zsh -i -c exit  0.11s user 0.05s system 77% cpu 0.211 total
zsh -i -c exit  0.12s user 0.05s system 78% cpu 0.213 total
zsh -i -c exit  0.13s user 0.03s system 78% cpu 0.209 total

初回だけやたら時間がかかっているが、それ以外では50msほど早くなった。
複雑な処理は行っていないし、特に副作用はないと思うのでまあしばらくこれで使ってみたいと思う。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4