path_get_path: Narrow string before

This cuts down on the wcs2string here by ~25%.

The better solution would be to cache narrow versions of $PATH, since
we compute that over and over and over and over again, while it rarely changes.

Or we could add a full path-cache (where which command is), but that's
much harder to invalidate.

See #5905.
This commit is contained in:
Fabian Homborg 2019-05-31 10:09:10 +02:00
parent 5ca14904d0
commit 7525befadb

View file

@ -39,12 +39,13 @@ static bool path_get_path_core(const wcstring &cmd, wcstring *out_path,
// If the command has a slash, it must be an absolute or relative path and thus we don't bother
// looking for a matching command.
if (cmd.find(L'/') != wcstring::npos) {
if (waccess(cmd, X_OK) != 0) {
std::string narrow = wcs2string(cmd);
if (access(narrow.c_str(), X_OK) != 0) {
return false;
}
struct stat buff;
if (wstat(cmd, &buff)) {
if (stat(narrow.c_str(), &buff)) {
return false;
}
if (S_ISREG(buff.st_mode)) {
@ -66,9 +67,10 @@ static bool path_get_path_core(const wcstring &cmd, wcstring *out_path,
for (auto next_path : *pathsv) {
if (next_path.empty()) continue;
append_path_component(next_path, cmd);
if (waccess(next_path, X_OK) == 0) {
std::string narrow = wcs2string(next_path);
if (access(narrow.c_str(), X_OK) == 0) {
struct stat buff;
if (wstat(next_path, &buff) == -1) {
if (stat(narrow.c_str(), &buff) == -1) {
if (errno != EACCES) {
wperror(L"stat");
}