Browse Source

tree-diff: avoid strncmp()

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>
maint
Junio C Hamano 18 years ago
parent
commit
ccc744abbb
  1. 20
      tree-diff.c

20
tree-diff.c

@ -93,7 +93,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int @@ -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,9 +111,15 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int @@ -111,9 +111,15 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
match += baselen;
matchlen -= baselen;

if (never_interesting) {
/*
* Does match sort strictly earlier than path with their
* common parts?
* 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);
@ -134,6 +140,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int @@ -134,6 +140,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
* 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 @@ -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

Loading…
Cancel
Save