From 6c4be6cdb4d147c377b5e6faf670fb1c5a137126 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Mon, 12 Oct 2015 23:17:52 +0100 Subject: [PATCH] ls: fix one stupid performance bug No point doing a stat on each node when we don't actually need it ! We should chdir into the directory before doing the stats on the short path. That avoids all the name walking overhead and thrash. Likewise it might be sensible to stat in inode order and batched, but that may cost more than the saving ! --- Applications/util/ls.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Applications/util/ls.c b/Applications/util/ls.c index 71554078..7ec1ab61 100644 --- a/Applications/util/ls.c +++ b/Applications/util/ls.c @@ -1,4 +1,8 @@ /* The "ls" standard command. + * + * FIXME: we should chdir into the directory we are listing if doing stats + * and then stat the shortname (this saves lots of filename walks and the + * resulting touches of atime). */ #include #include @@ -324,7 +328,8 @@ void main(int argc, char *argv[]) /* Now finally list the filenames. */ for (i = 0; i < listused; i++, free(name)) { name = list[i]; - if (LSTAT(name, &statbuf) < 0) { + /* Only do expensive stat calls if we actually need the data ! */ + if ((flags & (LSF_DIR|LSF_FILE|LSF_INODE|LSF_LONG)) && LSTAT(name, &statbuf) < 0) { perror(name); continue; } -- 2.34.1