Skip to content

Commit

Permalink
Make zsh faster again ~ Yesterday Once More ~ (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick authored Aug 2, 2023
1 parent f21c8bf commit f146a4f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
4 changes: 3 additions & 1 deletion home-manager/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
pkgs.zoxide
pkgs.fzf

# Used in anywhere
pkgs.coreutils

# asdf/rtx
#
# Prefer rtx now
Expand All @@ -42,7 +45,6 @@
pkgs.shfmt
pkgs.nixpkgs-fmt

pkgs.coreutils
pkgs.tree
pkgs.exa
pkgs.curl
Expand Down
42 changes: 38 additions & 4 deletions home-manager/zsh.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, ... }:
{ config, lib, pkgs, ... }:

{
programs.starship.enableZshIntegration = true;
Expand Down Expand Up @@ -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
Expand All @@ -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
'';
Expand Down
9 changes: 6 additions & 3 deletions home/.local/share/homemade/bin/bench_shells.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit f146a4f

Please sign in to comment.