From f146a4f9c9dd7efe891a96f3158fa4917583a46b Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Wed, 2 Aug 2023 17:04:53 +0900 Subject: [PATCH] Make zsh faster again ~ Yesterday Once More ~ (#237) --- home-manager/packages.nix | 4 +- home-manager/zsh.nix | 42 +++++++++++++++++-- .../share/homemade/bin/bench_shells.bash | 9 ++-- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/home-manager/packages.nix b/home-manager/packages.nix index 4f0f1fd8..9a0159d0 100644 --- a/home-manager/packages.nix +++ b/home-manager/packages.nix @@ -23,6 +23,9 @@ pkgs.zoxide pkgs.fzf + # Used in anywhere + pkgs.coreutils + # asdf/rtx # # Prefer rtx now @@ -42,7 +45,6 @@ pkgs.shfmt pkgs.nixpkgs-fmt - pkgs.coreutils pkgs.tree pkgs.exa pkgs.curl diff --git a/home-manager/zsh.nix b/home-manager/zsh.nix index 3688dc7a..87af459f 100644 --- a/home-manager/zsh.nix +++ b/home-manager/zsh.nix @@ -1,4 +1,4 @@ -{ config, ... }: +{ config, lib, pkgs, ... }: { programs.starship.enableZshIntegration = true; @@ -37,7 +37,42 @@ syntaxHighlighting.enable = true; enableAutosuggestions = true; + + # NOTE: enabling without tuning makes much slower zsh as +100~200ms execution time + # And the default path is not intended, so you SHOULD update `completionInit` enableCompletion = true; + # https://github.com/nix-community/home-manager/blob/8c731978f0916b9a904d67a0e53744ceff47882c/modules/programs/zsh.nix#L325C7-L329 + # https://github.com/nix-community/home-manager/blob/8c731978f0916b9a904d67a0e53744ceff47882c/modules/programs/zsh.nix#L368-L372 + # The default is "autoload -U compinit && compinit", I can not accept the path and speed + initExtraBeforeCompInit = '' + _elapsed_seconds_for() { + local -r target_path="$1" + echo "$(("$(${lib.getBin pkgs.coreutils}/bin/date +"%s")" - "$(${lib.getBin pkgs.coreutils}/bin/stat --format='%Y' "$target_path")"))" + } + + # path - https://stackoverflow.com/a/48057649/1212807 + # speed - https://gist.github.com/ctechols/ca1035271ad134841284 + # both - https://github.com/kachick/dotfiles/pull/155 + _compinit_with_interval() { + local -r dump_dir="${config.xdg.cacheHome}/zsh" + local -r dump_path="$dump_dir/zcompdump-$ZSH_VERSION" + # seconds * minutes * hours + local -r threshold="$((60 * 60 * 3))" + + if [ -e "$dump_path" ] && [ "$(_elapsed_seconds_for "$dump_path")" -le "$threshold" ]; then + # https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Use-of-compinit + # -C omit to check new functions + compinit -C -d "$dump_path" + else + mkdir -p "$dump_dir" + compinit -d "$dump_path" + fi + } + ''; + completionInit = '' + # `autoload` enable to use compinit + autoload -Uz compinit && _compinit_with_interval + ''; # Setting bindkey # https://github.com/nix-community/home-manager/blob/8c731978f0916b9a904d67a0e53744ceff47882c/modules/programs/zsh.nix#L28 @@ -46,9 +81,8 @@ # home-manager path will set in `programs.home-manager.enable = true`; envExtra = '' - # https://wiki.archlinux.jp/index.php/XDG_Base_Directory - # https://www.reddit.com/r/zsh/comments/tpwx9t/zcompcache_vs_zcompdump/ - zstyle ':completion:*' cache-path "$XDG_CACHE_HOME/zsh/zcompcache" + # https://gist.github.com/ctechols/ca1035271ad134841284?permalink_comment_id=3401477#gistcomment-3401477 + skip_global_compinit=1 if [ -e "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then . "$HOME/.nix-profile/etc/profile.d/nix.sh"; fi # added by Nix installer ''; diff --git a/home/.local/share/homemade/bin/bench_shells.bash b/home/.local/share/homemade/bin/bench_shells.bash index a50bba9d..fce7657b 100755 --- a/home/.local/share/homemade/bin/bench_shells.bash +++ b/home/.local/share/homemade/bin/bench_shells.bash @@ -4,10 +4,13 @@ set -euxo pipefail # ~ my feeling ~ # 50ms : blazing fast! -# 120ms : acceptable +# 110ms : acceptable +# 150ms : slow # 200ms : 1980s? -# 300ms : slow! +# 300ms : much slow! -hyperfine --warmup 1 'bash -i -c exit' +# zsh should be first, because it often makes much slower with the completion hyperfine --warmup 1 'zsh --interactive -c exit' + +hyperfine --warmup 1 'bash -i -c exit' hyperfine --warmup 1 'fish --interactive --command exit'