ls: fix one stupid performance bug
authorAlan Cox <alan@linux.intel.com>
Mon, 12 Oct 2015 22:17:52 +0000 (23:17 +0100)
committerAlan Cox <alan@linux.intel.com>
Mon, 12 Oct 2015 22:17:52 +0000 (23:17 +0100)
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

index 7155407..7ec1ab6 100644 (file)
@@ -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 <stdio.h>
 #include <string.h>
@@ -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;
            }