Optimize fish_config theme choose

Just following basic shellscript optimization:

- Remove a useless use of cat (`status get-file` takes microseconds,
`status get-file | cat` is on the order of a millisecond - slower with
bigger $PATH)
- Pipe, don't run in a loop
- Filter early

This reduces the time taken from 12ms to 6ms on one of my systems, and
6.5ms to 4.5ms on another.

This is paid on every single shell startup, including
non-interactively, so it's worth it.

There's more to investigate, but this is a good first pass.
This commit is contained in:
Fabian Boehm
2025-12-29 17:26:55 +01:00
parent 941a6cb434
commit b31387416d
2 changed files with 8 additions and 7 deletions

View File

@@ -6,15 +6,16 @@ function __fish_theme_cat -a theme_name
echo >&2 Searched (__fish_theme_dir) "and `status list-files themes`"
return 1
end
set -l theme_data (__fish_config_with_file $theme_path cat)
set -l theme_data (if string match -q '/*' -- $theme_path; cat $theme_path; else status get-file $theme_path; end)
set -l allowed_lines \
'\s*' \
'\s*#.*' \
'\[(dark|light|unknown)\]' \
(__fish_theme_variable_filter)
set allowed_lines "^($(string join -- '|' $allowed_lines))\$"
for line in $theme_data
string match -rq -- $allowed_lines $line
printf '%s\n' $theme_data | string match -rvq -- $allowed_lines
and for line in $theme_data
string match -rq -- $allowed_lines $theme_data
or printf >&2 "error: unsupported line in theme '%s': %s\n" $theme_name $line
end
string join \n $theme_data

View File

@@ -287,7 +287,7 @@ function __fish_config_theme_choose
set -l color_theme
__fish_config_theme_canonicalize
set -l theme_data (type -q cat && __fish_theme_cat $theme_name)
set -l theme_data (__fish_theme_cat $theme_name)
or return
set -l color_themes dark light unknown
set -l theme_is_color_theme_aware false
@@ -340,7 +340,7 @@ function __fish_config_theme_choose
end
set -l color_theme
string join \n -- $theme_data |
string match -re -- (__fish_theme_variable_filter)'|^\[.*\]$' $theme_data |
while read -lat toks
if $theme_is_color_theme_aware
for ct in $color_themes
@@ -354,8 +354,8 @@ function __fish_config_theme_choose
end
end
set -l varname $toks[1]
string match -rq -- (__fish_theme_variable_filter) "$varname"
or continue
string match -q '[*' -- $varname
and continue
# If we're supposed to set universally, remove any shadowing globals
# so the change takes effect immediately (and there's no warning).
if test $scope = -U; and set -qg $varname