diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:48:18 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:48:18 -0600 |
| commit | 3abe682147f74104f1f1225e8c142b80018e17e7 (patch) | |
| tree | 1ef44f0ebbfd2bccd747c3557a46bc1842b97eb9 | |
| parent | 270ac6676c05470223c5fc09fb3230b97acfbe2b (diff) | |
| download | zash-3abe682147f74104f1f1225e8c142b80018e17e7.tar.gz | |
perf(glob): do not lstat() a component readdir() just named
The last component of a pattern was matched by readdir() and then stat'ed
again by the recursive call, only to learn that the name it had just read
exists. When nothing follows the component, add it straight away; a
literal tail and a trailing `/` still go through lstat(), the second
because `*/` must not match a file.
Measured on Linux (x86-64) with 3000 names in one directory and
`set -- d/*` run 200 times: 0.20 s before, 0.05 s after, three runs each,
same result count.
make check unchanged (126 passed, 1 skipped, the known
linux/i-dscomplete failure on this machine).
| -rw-r--r-- | src/pattern.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/pattern.c b/src/pattern.c index 0dabc15..46c667c 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -255,7 +255,12 @@ static void glob_from(globber *g, const char *pat) { continue; g->path.len = dirlen; sb_puts(&g->path, name); - glob_from(g, rest); + /* readdir() already named this entry: the last component needs + * no lstat(), only a literal tail or a trailing `/` does. */ + if (!*rest) + g->add(g->ctx, sb_cstr(&g->path)); + else + glob_from(g, rest); } closedir(dir); } |
