From 984d07a53ba3cefd4a4d010044cdecb68b4d7bf8 Mon Sep 17 00:00:00 2001 From: Robby Russell Date: Sun, 6 Sep 2026 08:06:51 -0700 Subject: [PATCH] perf(git): cache the git version used for alias selection `git version` was forked on every startup so that four aliases could be picked according to the installed git. Cache the version in $ZSH_CACHE_DIR for a day, keyed on the git binary's path and mtime, so a different or upgraded git is picked up right away and the fork otherwise happens once a day. Measured on macOS arm64, zsh 5.9: git.plugin.zsh 9.2 ms -> 2.0 ms per interactive start. Co-Authored-By: Claude Fable 5.1 --- plugins/git/git.plugin.zsh | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index 0b6b3ee1f..4273613b4 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -1,6 +1,24 @@ # Git version checking autoload -Uz is-at-least -git_version="${${(As: :)$(git version 2>/dev/null)}[3]}" +# Running `git version` forks on every startup, so cache the result for a day +# in $ZSH_CACHE_DIR, keyed on the git binary's path and mtime. +() { + local cache="$ZSH_CACHE_DIR/git-version" key + local -a stat lines + zmodload -F zsh/stat b:zstat + zstat -A stat +mtime -- "$commands[git]" 2>/dev/null + key="$commands[git] $stat[1]" + + lines=("$cache"(Nm-1)) + (( $#lines )) && lines=("${(@f)$(<"$cache")}") + if [[ "$lines[1]" = "$key" ]]; then + git_version="$lines[2]" + return + fi + + git_version="${${(As: :)$(git version 2>/dev/null)}[3]}" + [[ ! -w "$ZSH_CACHE_DIR" ]] || print -rl -- "$key" "$git_version" >| "$cache" +} # # Functions Current