From 04f232ed99bb68ef7c5169173253d4cdfa840bce Mon Sep 17 00:00:00 2001
From: Manuel Stoeckl <code@mstoeckl.com>
Date: Sun, 31 Oct 2021 14:02:18 -0400
Subject: [PATCH] util/disk_cache: try getenv(HOME) before getpwuid->pw_dir
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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>
---
 src/util/disk_cache_os.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/util/disk_cache_os.c b/src/util/disk_cache_os.c
index cb3f466083b97..d9a75cd7fc6d4 100644
--- a/src/util/disk_cache_os.c
+++ b/src/util/disk_cache_os.c
@@ -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;
-- 
GitLab