From 6823f5e3374f00f43e9d20a4db12d63e0bc5da84 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 14 Jul 2023 20:17:19 +0200 Subject: [PATCH] wildcard: Remove useless access() call for trailing slash This confirmed that a file existed via access(file, F_OK). But we already *know* that it does because this is the expansion for the "trailing slash" - by definition all wildcard components up to here have already been checked. And it's not checking for directoryness either because it does F_OK. This will remove one `access()` per result, which will cut the number of syscalls needed for a glob that ends in a "/" in half. This brings us on-par with e.g. `ls` (which uses statx while we use newfstatat, but that should have about the same results) Fixes #9891. --- src/wildcard.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 6f6258379..21c8a8810 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -679,11 +679,8 @@ void wildcard_expander_t::expand_trailing_slash(const wcstring &base_dir, const } if (!(flags & expand_flag::for_completions)) { - // Trailing slash and not accepting incomplete, e.g. `echo /xyz/`. Insert this file if it - // exists. - if (waccess(base_dir, F_OK) == 0) { - this->add_expansion_result(wcstring{base_dir}); - } + // Trailing slash and not accepting incomplete, e.g. `echo /xyz/`. Insert this file, we already know it exists! + this->add_expansion_result(wcstring{base_dir}); } else { // Trailing slashes and accepting incomplete, e.g. `echo /xyz/`. Everything is added. dir_iter_t dir = open_dir(base_dir);