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.
This commit is contained in:
Fabian Boehm 2023-07-11 17:44:49 +02:00
parent 3fbff14e9b
commit 1a11cee559

View File

@ -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