make glob faster
authorAnas Nashif <anas.nashif@intel.com>
Mon, 5 Nov 2012 19:21:37 +0000 (11:21 -0800)
committerAnas Nashif <anas.nashif@intel.com>
Mon, 5 Nov 2012 19:21:37 +0000 (11:21 -0800)
read.c
tests/scripts/functions/wildcard

diff --git a/read.c b/read.c
index 9dfd4ea04c4805067c34b95feb41b51f9ebd03ff..8587e858598986ba18cdba5aa4c5e75cebb32050 100644 (file)
--- a/read.c
+++ b/read.c
@@ -2904,6 +2904,7 @@ parse_file_seq (char **stringp, unsigned int size, int stopchar,
       const char *name;
       const char **nlist = 0;
       char *tildep = 0;
+      int globme = 1;
 #ifndef NO_ARCHIVES
       char *arname = 0;
       char *memname = 0;
@@ -3112,32 +3113,40 @@ parse_file_seq (char **stringp, unsigned int size, int stopchar,
        }
 #endif /* !NO_ARCHIVES */
 
-      switch (glob (name, GLOB_NOSORT|GLOB_ALTDIRFUNC, NULL, &gl))
-       {
-       case GLOB_NOSPACE:
-         fatal (NILF, _("virtual memory exhausted"));
+      /* glob() is expensive: don't call it unless we need to.  */
+      if (!(flags & PARSEFS_EXISTS) && strpbrk (name, "?*[") == NULL)
+        {
+          globme = 0;
+          i = 1;
+          nlist = &name;
+        }
+      else
+        switch (glob (name, GLOB_NOSORT|GLOB_ALTDIRFUNC, NULL, &gl))
+          {
+          case GLOB_NOSPACE:
+            fatal (NILF, _("virtual memory exhausted"));
 
-       case 0:
-          /* Success.  */
-          i = gl.gl_pathc;
-          nlist = (const char **)gl.gl_pathv;
-          break;
+          case 0:
+            /* Success.  */
+            i = gl.gl_pathc;
+            nlist = (const char **)gl.gl_pathv;
+            break;
 
-        case GLOB_NOMATCH:
-          /* If we want only existing items, skip this one.  */
-          if (flags & PARSEFS_EXISTS)
-            {
-              i = 0;
-              break;
-            }
-          /* FALLTHROUGH */
+          case GLOB_NOMATCH:
+            /* If we want only existing items, skip this one.  */
+            if (flags & PARSEFS_EXISTS)
+              {
+                i = 0;
+                break;
+              }
+            /* FALLTHROUGH */
 
-       default:
-          /* By default keep this name.  */
-          i = 1;
-          nlist = &name;
-          break;
-       }
+          default:
+            /* By default keep this name.  */
+            i = 1;
+            nlist = &name;
+            break;
+          }
 
       /* For each matched element, add it to the list.  */
       while (i-- > 0)
@@ -3177,7 +3186,8 @@ parse_file_seq (char **stringp, unsigned int size, int stopchar,
 #endif /* !NO_ARCHIVES */
           NEWELT (concat (2, prefix, nlist[i]));
 
-      globfree (&gl);
+      if (globme)
+        globfree (&gl);
 
 #ifndef NO_ARCHIVES
       if (arname)
index 2841f5d56d65e4a191eb2d2a52226230dddb01b8..bcd84ad793a3ff40c7924422a455b95bea94a8b0 100644 (file)
@@ -88,4 +88,16 @@ all: ; @echo $(wildcard xz--y*.7)
 !,
               '', "\n");
 
+# TEST #5: wildcard used to verify file existence
+
+touch('xxx.yyy');
+
+run_make_test(q!exists: ; @echo file=$(wildcard xxx.yyy)!,
+              '', "file=xxx.yyy\n");
+
+unlink('xxx.yyy');
+
+run_make_test(q!exists: ; @echo file=$(wildcard xxx.yyy)!,
+              '', "file=\n");
+
 1;