Conda init 设置拖慢 shell 启动速度的解决方法

最近发现 zsh 的启动速度变慢了,经常打了几个字母,终端才缓过神来,导致我输入命令被打断,按理说不应该,毕竟我用的 Alaritty + zsh + zshfw 的组合,Alacritty使用 GPU 加速渲染,zimfw号称第三快的 zsh 配置框架,性能方面肯定是没问题的,那就只能是我.zshrc 的问题了。

而恰巧我的.zshrc 又比较精简,除了一点点必备的插件其他啥也没有,所以很快啊,我的目光聚焦在了 conda init 往我的配置文件里写的那几行上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/miniconda/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/miniconda/etc/profile.d/conda.sh" ]; then
. "/opt/miniconda/etc/profile.d/conda.sh"
else
export PATH="/opt/miniconda/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

果不其然,删掉这几行后,终端启动速度又飞快了起来

但是肯定是不能直接删的,不然 conda 指令又没法用了(虽然我基本上就不用 conda),看下这几行的大意,大概就是做了个添加环境变量的事,那么占时间比较多的肯定就是 $('/opt/miniconda/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)这里了,又是执行脚本又是重定向输出的,一看就很花时间,验证一下:

zsh 加载整个.zshrc 只要 0.386s,而单独执行这条命令都要 0.328s,果断注释掉它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# __conda_setup="$('/opt/miniconda/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
# if [ $? -eq 0 ]; then
# eval "$__conda_setup"
# else
if [ -f "/opt/miniconda/etc/profile.d/conda.sh" ]; then
. "/opt/miniconda/etc/profile.d/conda.sh"
else
export PATH="/opt/miniconda/bin:$PATH"
fi
# fi
# unset __conda_setup
# <<< conda initialize <<<

zsh 启动速度又和飞一样了,不用每次等 zsh 启动完再重打命令了,实测 conda 指令也没有受到影响,依然能正常切换环境,在 github 上也找到了一样的方法:

也有为 conda 加载设置别名,用命令来手动加载 conda 的方法:

Conda init 设置拖慢 shell 启动速度的解决方法

https://fly.meow-2.com/post/records/conda-faster.html

作者

Meow-2

发布于

2022-02-18

更新于

2023-02-22


评论