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

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.

Signed-off-by: Manuel Stoeckl <code@mstoeckl.com>
Reviewed-by: Zoltán Böszörményi <zboszor@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13614>
This commit is contained in:
Manuel Stoeckl 2021-10-31 14:02:18 -04:00 committed by Marge Bot
parent c2b1555a57
commit 04f232ed99

View file

@ -879,6 +879,7 @@ disk_cache_write_item_to_disk(struct disk_cache_put_job *dc_job,
*
* $MESA_SHADER_CACHE_DIR
* $XDG_CACHE_HOME/mesa_shader_cache
* $HOME/.cache/mesa_shader_cache
* <pwd.pw_dir>/.cache/mesa_shader_cache
*/
char *
@ -924,6 +925,20 @@ disk_cache_generate_cache_dir(void *mem_ctx, const char *gpu_name,
}
}
if (!path) {
char *home = getenv("HOME");
if (home) {
path = concatenate_and_mkdir(mem_ctx, home, ".cache");
if (!path)
return NULL;
path = concatenate_and_mkdir(mem_ctx, path, cache_dir_name);
if (!path)
return NULL;
}
}
if (!path) {
char *buf;
size_t buf_size;