[vlc-commits] linux/dirs: cache the result of config_GetLibDir()

Thomas Guillem git at videolan.org
Thu May 18 14:06:00 CEST 2017


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Thu May 18 14:03:07 2017 +0200| [b474d7de790e9cfb98d7fa4dd4ae611926c86014] | committer: Thomas Guillem

linux/dirs: cache the result of config_GetLibDir()

On release builds, this slow function is called from config_GetDataDir() (if
VLC_DATA_PATH is not defined) each time we probe a lua file.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b474d7de790e9cfb98d7fa4dd4ae611926c86014
---

 src/linux/dirs.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/linux/dirs.c b/src/linux/dirs.c
index 59af705f84..701f1027fc 100644
--- a/src/linux/dirs.c
+++ b/src/linux/dirs.c
@@ -24,6 +24,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <linux/limits.h>
 
 #include <vlc_common.h>
 #include "libvlc.h"
@@ -31,6 +32,23 @@
 
 char *config_GetLibDir (void)
 {
+    static struct
+    {
+        vlc_mutex_t lock;
+        char path[PATH_MAX];
+    } cache = {
+        VLC_STATIC_MUTEX, "",
+    };
+
+    /* Reading and parsing /proc/self/maps is slow, so cache the value since
+     * it's guaranteed not to change during the life-time of the process. */
+    vlc_mutex_lock(&cache.lock);
+    if (cache.path[0] != '\0')
+    {
+        char *ret = strdup(cache.path);
+        vlc_mutex_unlock(&cache.lock);
+        return ret;
+    }
     char *path = NULL;
 
     /* Find the path to libvlc (i.e. ourselves) */
@@ -72,7 +90,12 @@ char *config_GetLibDir (void)
     free (line);
     fclose (maps);
 error:
-    return (path != NULL) ? path : strdup (PKGLIBDIR);
+    if (path == NULL)
+        path = strdup(PKGLIBDIR);
+    if (likely(path != NULL && sizeof(cache.path) > strlen(path)))
+        strcpy(cache.path, path);
+    vlc_mutex_unlock(&cache.lock);
+    return path;
 }
 
 char *config_GetDataDir (void)



More information about the vlc-commits mailing list