From: Christophe CURIS <[email protected]>

The first optimisation is to compute only once the path, and then
always re-use the value which did not change anyway.
The second optimisation is to avoid a lot of excessive function
calls, including alloc+free that are not necessary and participate
in memory fragmentation.
---
 WINGs/userdefaults.c |   37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/WINGs/userdefaults.c b/WINGs/userdefaults.c
index c1b4652..4cfd55a 100644
--- a/WINGs/userdefaults.c
+++ b/WINGs/userdefaults.c
@@ -48,35 +48,34 @@ extern char *WMGetApplicationName();
 
 const char *wusergnusteppath()
 {
+       static const char subdir[] = "/GNUstep";
        static char *path = NULL;
-       char *gspath;
+       char *gspath, *h;
        int pathlen;
 
+       if (path)
+               /* Value have been already computed, re-use it */
+               return path;
+
        gspath = getenv("GNUSTEP_USER_ROOT");
        if (gspath) {
                gspath = wexpandpath(gspath);
                if (gspath) {
-                       pathlen = strlen(gspath) + 4;
-                       path = wmalloc(pathlen);
-                       if (wstrlcpy(path, gspath, pathlen) >= pathlen) {
-                               wfree(gspath);
-                               return NULL;
-                       }
-                       wfree(gspath);
-               }
-       } else {
-               char *h = wgethomedir();
-               if (!h)
-                       return NULL;
-               pathlen = strlen(h) + 8 /* /GNUstep */ + 1;
-               path = wmalloc(pathlen);
-               if (wstrlcpy(path, h, pathlen) >= pathlen ||
-                   wstrlcat(path, "/GNUstep", pathlen) >= pathlen) {
-                       wfree(path);
-                       return NULL;
+                       path = gspath;
+                       return path;
                }
+               wwarning(_("variable GNUSTEP_USER_ROOT defined with invalid 
path, not used"));
        }
 
+       h = wgethomedir();
+       if (!h)
+               return NULL;
+
+       pathlen = strlen(h);
+       path = wmalloc(pathlen + sizeof(subdir));
+       strcpy(path, h);
+       strcpy(path + pathlen, subdir);
+
        return path;
 }
 
-- 
1.7.10.4


-- 
To unsubscribe, send mail to [email protected].

Reply via email to