ioutil.ReadDir returns []os.FileInfo, which contains information on
each entry in the directory that is only available by calling
os.Lstat on the entry. Finder only the name and type (regular,
directory or symlink) of the files, which on Linux kernels >= 2.6.4
is available in the return values of syscall.Getdents.
Replace ioutil.ReadDir with a call that uses syscall.Getdents
directly and collects the type information from the result.
Testing with:
rm -f /tmp/db && strace -fc finder -names Android.mk,Android.bp,Blueprints,CleanSpec.mk,TEST_MAPPING -exclude-dirs .git,.repo -prune-files .out-dir,.find-ignore -db /tmp/db .
Before:
7.01 52.688304 63 833398 1 lstat
1.90 14.246644 68 210523 getdents64
1.25 9.370471 90 104286 1 openat
After:
3.48 12.201385 117 104286 1 openat
3.06 10.729138 51 210523 getdents64
1.70 5.951892 57 104283 1 lstat
Pros:
Avoids 729115 calls to lstat.
Cons:
Requires copying ~200 lines of finicky buffer parsing code.
Puts all getdents calls (and possibly fallback lstat calls) onto
a non-blocking file descriptor, which will cause it to block a
thread and not just a goroutine.
Only works on Linux and Darwin.
Bug: 70897635
Test: m checkbuild
Change-Id: Iab9f82c38c8675d0b73b4e90540bb9e4d2ee52c1