Code

tree-diff: avoid strncmp()
authorJunio C Hamano <junkio@cox.net>
Wed, 21 Mar 2007 19:34:46 +0000 (12:34 -0700)
committerJunio C Hamano <junkio@cox.net>
Thu, 22 Mar 2007 07:34:39 +0000 (00:34 -0700)
If we already know that some of the pathspecs can match later
entries in the tree we are looking at, we do not have to do more
expensive strncmp() upfront before comparing the length of the
match pattern and the path, as a path longer than the match
pattern will not match it, and a path shorter than the match
pattern will match only if the path is a directory-component
wise prefix of the match pattern.

Signed-off-by: Junio C Hamano <junkio@cox.net>
tree-diff.c

index 35af569fb814c5305e816ed52dd03c702cf7970e..15fd665fb827bfa6ebc9254b5eab142e7d261718 100644 (file)
@@ -93,7 +93,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
        for (i = 0; i < opt->nr_paths; i++) {
                const char *match = opt->paths[i];
                int matchlen = opt->pathlens[i];
-               int m;
+               int m = -1; /* signals that we haven't called strncmp() */
 
                if (baselen >= matchlen) {
                        /* If it doesn't match, move along... */
@@ -111,29 +111,36 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
                match += baselen;
                matchlen -= baselen;
 
-               /*
-                * Does match sort strictly earlier than path with their
-                * common parts?
-                */
-               m = strncmp(match, path,
-                           (matchlen < pathlen) ? matchlen : pathlen);
-               if (m < 0)
-                       continue;
+               if (never_interesting) {
+                       /*
+                        * We have not seen any match that sorts later
+                        * than the current path.
+                        */
+
+                       /*
+                        * Does match sort strictly earlier than path
+                        * with their common parts?
+                        */
+                       m = strncmp(match, path,
+                                   (matchlen < pathlen) ? matchlen : pathlen);
+                       if (m < 0)
+                               continue;
 
-               /*
-                * If we come here even once, that means there is at
-                * least one pathspec that would sort equal to or
-                * later than the path we are currently looking at.
-                * In other words, if we have never reached this point
-                * after iterating all pathspecs, it means all
-                * pathspecs are either outside of base, or inside the
-                * base but sorts strictly earlier than the current
-                * one.  In either case, they will never match the
-                * subsequent entries.  In such a case, we initialized
-                * the variable to -1 and that is what will be
-                * returned, allowing the caller to terminate early.
-                */
-               never_interesting = 0;
+                       /*
+                        * If we come here even once, that means there is at
+                        * least one pathspec that would sort equal to or
+                        * later than the path we are currently looking at.
+                        * In other words, if we have never reached this point
+                        * after iterating all pathspecs, it means all
+                        * pathspecs are either outside of base, or inside the
+                        * base but sorts strictly earlier than the current
+                        * one.  In either case, they will never match the
+                        * subsequent entries.  In such a case, we initialized
+                        * the variable to -1 and that is what will be
+                        * returned, allowing the caller to terminate early.
+                        */
+                       never_interesting = 0;
+               }
 
                if (pathlen > matchlen)
                        continue;
@@ -145,6 +152,13 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
                                continue;
                }
 
+               if (m == -1)
+                       /*
+                        * we cheated and did not do strncmp(), so we do
+                        * that here.
+                        */
+                       m = strncmp(match, path, pathlen);
+
                /*
                 * If common part matched earlier then it is a hit,
                 * because we rejected the case where path is not a