From b31387416dc72e9b91f7efdab715326a8fd71749 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Mon, 29 Dec 2025 17:26:55 +0100 Subject: [PATCH] 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. --- share/functions/__fish_theme_cat.fish | 7 ++++--- share/functions/fish_config.fish | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/share/functions/__fish_theme_cat.fish b/share/functions/__fish_theme_cat.fish index 21f7176df..79dc363c1 100644 --- a/share/functions/__fish_theme_cat.fish +++ b/share/functions/__fish_theme_cat.fish @@ -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 diff --git a/share/functions/fish_config.fish b/share/functions/fish_config.fish index 801db489e..b0b0c0221 100644 --- a/share/functions/fish_config.fish +++ b/share/functions/fish_config.fish @@ -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