msys2 `which` is very slow on windows 10

0

The which command was never particularly fast, but since I switched to Windows 10, it is extremely slow. I have a generic .zshrc that I carry around, so it tests to see if some programs are available before configuring them. And the first few calls take over 10 seconds.

I am using zsh, which has which built-in. This might make a difference, though defining which() { /usr/bin/which "%@" } does not seem to improve anything.

Note: I don't have any network drives mounted.

Jan Hudec

Posted 2018-02-26T10:45:55.800

Reputation: 885

Answers

1

A workaround and all around nicer approach, given what you're doing in this case, is to avoid using which entirely, like so:

if (( $+commands[foobar] ))
  # configure foobar
fi

How does it work?

From zshexpn(1):

${+name}

If name is the name of a set parameter '1' is substituted, otherwise '0' is substituted.

$commands is an associative array that is managed by zsh (also the hash builtin) consisting of command names as keys, and their associated path as a value.

So, using the ${+name} expansion on the $commands hash table, indexed by the command you wish to test for the existence of gives you a cheap and fast way to make that check.

Lastly, the (( expr )) construct is an arithemetic evaluation, since the ${+name} expansion returns either 0 or 1.

ZeroKnight

Posted 2018-02-26T10:45:55.800

Reputation: 408