Skip to content

util/disk_cache: try getenv(HOME) before getpwuid->pw_dir

M. Stoeckl requested to merge mstoeckl/mesa:home-first into main

getenv("HOME") is significantly faster than getpwuid_r(...)->pw_dir, because the latter may require loading NSS libraries, reading /etc/passwd, etc.

Furthermore, the Linux man pages for getpwuid_r recommend using getenv("HOME") instead of getpwuid_r, because the user may wish to change the value of HOME after logging in.

This MR is a small step towards #5155, but does not resolve it. It does not remove the getpwuid_r call (since for all I know there are situations where HOME is unavailable), and there is still other code using getpwuid_r (like radv_builtin_cache_path in radv_meta.c), that I am unable to test.

On one computer (Linux, i5-8250U, SSD, using libnss_files.so for password database) calling getenv("HOME") for the first time in a process almost always takes <0.005 msec to scan the list of environment variables that is already in memory, while getpwuid_r(getuid(), &pwd, buf, sizeof(buf), &result); needs >0.15 milliseconds to call _dl_open, read and parse /etc/passwd, and fill a struct passwd. This is a small enough change that it is dwarfed by the random variation in OpenGL program startup time. _

Merge request reports