From 1a11cee55940d655ea0a7d353b4b0b9410979fd0 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Tue, 11 Jul 2023 17:44:49 +0200 Subject: [PATCH] functions/cd: Optimize check for too many args This ran two `test`s a `count` and one `echo`, which is a bit wasteful. So instead, for the common case where you pass one argument, this will run one `set -q`. This can save off ~160 microseconds for each ordinary `cd`, which speeds it up by a factor of ~2 (so 1000 runs of cd might take 260ms instead of 550ms). Ideally the cd function would just be incorporated into the builtin, but that's a bigger change. --- share/functions/cd.fish | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/share/functions/cd.fish b/share/functions/cd.fish index 7ebe37a7e..aa9114967 100644 --- a/share/functions/cd.fish +++ b/share/functions/cd.fish @@ -4,7 +4,10 @@ function cd --description "Change directory" set -l MAX_DIR_HIST 25 - if test (count $argv) -gt (test "$argv[1]" = "--" && echo 2 || echo 1) + if set -q argv[2]; and begin + set -q argv[3] + or not test "$argv[1]" = -- + end printf "%s\n" (_ "Too many args for cd command") >&2 return 1 end