-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ssh: optimize detection #2774
base: master
Are you sure you want to change the base?
ssh: optimize detection #2774
Conversation
How did you benchmark that the new comparison is faster? And how big is |
I had a hard time believing it and needed to give this a try myself. Note I am no Linux, but this shouldn't be that different on an M1 #!/usr/bin/env zsh
COUNT=${COUNT:-10000}
function old() {
(( $+commands[who] )) || return
}
function new() {
[[ -z "$(command -v who)" ]] && return
}
t0=$(date +%s)
for i in {0..$COUNT}; do
old
done
t1=$(date +%s)
for i in {0..$COUNT}; do
new
done
t2=$(date +%s)
for i in {0..$COUNT}; do
(( $+commands[who] ))
done
t3=$(date +%s)
for i in {0..$COUNT}; do
[[ -z "$(command -v who)" ]]
done
t4=$(date +%s)
{
echo "command total average"
echo "+commandsF: $((t1-t0))s $(bc -l <<< "scale=5; 1000*($t1-$t0)/$COUNT")ms"
echo "command-vF: $((t2-t1))s $(bc -l <<< "scale=5; 1000*($t2-$t1)/$COUNT")ms"
echo "+commands: $((t3-t2))s $(bc -l <<< "scale=5; 1000*($t3-$t2)/$COUNT")ms"
echo "command-v: $((t4-t3))s $(bc -l <<< "scale=5; 1000*($t4-$t3)/$COUNT")ms"
} |column -t
Note that this measuring is very inaccurate since I call |
@Syphdias instead of |
My favorite recipe for quick-n-dirty benchmarks: time ( repeat 100000 (( $+commands[who] )) ) Adjust the repeat count to get the total run time of at least 100ms. |
I used that first with Any how, as expected, searching through a variable is faster than calling a program/function… |
i used hyperfine to run the command inside a terminal. i don't have the command handy in my terminal history at the moment, though. for reference |
p10k's ssh detection is pretty thorough, but also pretty heavy, especially when the primary use case is local. on my m1 macbook,
(( $+commands[who] )) && return
costs 7 milliseconds.this PR changes
$+commands[who]
tocommand -v who
, which runs almost instantaneously in comparison.the next biggest cost is in the regex match at the end of the function. it feels like ssh detection should be an optional thing. the best way i've found to bypass it is to set
P9K_SSH
and faking out_P9K_TTY
, and forcing detection onSSH_CLIENT
orSSH_CONNECTION
alone, which i'm fine with.