From 3abe682147f74104f1f1225e8c142b80018e17e7 Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:48:18 -0600 Subject: 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). --- src/pattern.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/pattern.c') 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); } -- cgit From d60c99985c1c4e058dd2de7e6272ef4d48a14686 Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:50:57 -0600 Subject: perf(pattern,printf): skip the class table on ASCII, a bare %s through vsnprintf in_range() looked the two ends of `[a-z]` up in the alpha/digit tables to get alphabet order. In ASCII the code order *is* the alphabet order, so the compiler can take one comparison; the tables stay for EBCDIC. convert() formatted a bare "%s" through the spec builder and vsnprintf(), which pads, truncates and checks flags it was never given. Out it goes straight to out_puts(); anything with a flag, width or precision still takes the general path. make check unchanged (126 passed, 1 skipped, known linux/i-dscomplete); the whole suite under ASan+UBSan reports nothing. --- src/pattern.c | 5 +++++ src/printf.c | 4 ++++ 2 files changed, 9 insertions(+) (limited to 'src/pattern.c') diff --git a/src/pattern.c b/src/pattern.c index 46c667c..dded417 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -30,6 +30,10 @@ static int index_in(const char *set, unsigned char c) { * alphabet. Any other range is by character code. */ static bool in_range(unsigned char lo, unsigned char hi, unsigned char c) { +#if ZASH_ASCII + /* Code order is alphabet order here: one comparison, no table. */ + return lo <= c && c <= hi; +#else static const char *const sets[] = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", @@ -44,6 +48,7 @@ static bool in_range(unsigned char lo, unsigned char hi, unsigned char c) { } } return lo <= c && c <= hi; +#endif } /** diff --git a/src/printf.c b/src/printf.c index f366f2d..fabc0b3 100644 --- a/src/printf.c +++ b/src/printf.c @@ -240,6 +240,10 @@ static const char *convert(printf_state *pf, const char *f) { } case 's': { const char *s = next_arg(pf); + if (n == 1) { /* a bare "%s": there is nothing to pad or truncate */ + out_puts(&out1, s ? s : ""); + break; + } spec[n++] = 's'; spec[n] = '\0'; out_printf(&out1, spec, s ? s : ""); -- cgit