Perf: memoize _zsh_highlight_main_highlighter_check_path

This commit is contained in:
Roman Perepelitsa 2020-08-16 11:34:16 +02:00
parent 4cf464f843
commit 826aa7312d

View File

@ -1181,6 +1181,19 @@ _zsh_highlight_main_highlighter_highlight_path_separators()
#
# $2 should be non-zero iff we're in command position.
_zsh_highlight_main_highlighter_check_path()
{
if (( $+_zsh_highlight_main__path_cache )); then
local cache_key=$1$'\0'$2
if (( has_end && len == end_pos && !in_alias )) && [[ $WIDGET != zle-line-finish ]]; then
cache_key+=$'\0'
fi
local cache_val=$_zsh_highlight_main__path_cache[$cache_key]
if [[ -n $cache_val ]]; then
REPLY=${cache_val:1}
return $cache_val[1]
fi
fi
{
_zsh_highlight_main_highlighter_expand_path "$1"
local expanded_path="$REPLY" tmp_path
@ -1274,6 +1287,12 @@ _zsh_highlight_main_highlighter_check_path()
# It's not a path.
return 1
} always {
local -i ret=$((!!$?))
if (( $+_zsh_highlight_main__path_cache )); then
_zsh_highlight_main__path_cache[$cache_key]=$ret$REPLY
fi
}
}
# Highlight an argument and possibly special chars in quotes starting at $1 in $arg
@ -1809,15 +1828,16 @@ _zsh_highlight_main__precmd_hook() {
fi
_zsh_highlight_main__command_type_cache=()
_zsh_highlight_main__path_cache=()
}
autoload -Uz add-zsh-hook
if add-zsh-hook precmd _zsh_highlight_main__precmd_hook 2>/dev/null; then
# Initialize command type cache
typeset -gA _zsh_highlight_main__command_type_cache
# Initialize caches
typeset -gA _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache
else
print -r -- >&2 'zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.'
# Make sure the cache is unset
unset _zsh_highlight_main__command_type_cache
# Make sure the caches are unset
unset _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache
fi
typeset -ga ZSH_HIGHLIGHT_DIRS_BLACKLIST