realpath(3) is a major source of fstatat calls e.g., during buildkernel (used heavily by clang).
Moving realpath into the kernel removes most stats and getcwd calls (see below for example test result).
This works by performing a regular lookup while saving the name and found parent directory. If the terminal vnode is a directory we can resolve it using usual means. Otherwise we can use the name saved by lookup and resolve the parent.
The patch is almost complete (may use some comments). truss correctly dumps data, but ktrace only dumps one buffer and not the other one. I have not checked yet how to sort that out, but I don't think it should be a blocker.
before:
__getcwd 66065 mprotect 72233 open 94032 sigaction 111967 write 133762 pread 195676 openat 281137 fstat 318231 close 325406 sigprocmask 718695 fstatat 1423441 mmap 3259492
after:
mprotect 72233 open 94032 sigaction 111967 write 133761 fstatat 148846 pread 195676 __realpath 225900 openat 281137 fstat 318231 close 325404 sigprocmask 718699 mmap 3260705