Skip to content

Commit 9d6c580

Browse files
peffgitster
authored andcommitted
match_pathname(): reorder prefix-match check
As an optimization, we use fspathncmp() to match a prefix of the pattern that does not contain any wildcards, and then pass the remainder to fnmatch(). If it has matched the whole thing, we can return early. Let's shift this early-return check to before we tweak the pattern and name strings. That will gives us more flexibility with that tweaking. It might also save a few instructions, but I couldn't measure any improvement in doing so (and I wouldn't be surprised if an optimizing compiler could figure that out itself). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c44beea commit 9d6c580

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

dir.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,18 +1360,19 @@ int match_pathname(const char *pathname, int pathlen,
13601360

13611361
if (fspathncmp(pattern, name, prefix))
13621362
return 0;
1363-
pattern += prefix;
1364-
patternlen -= prefix;
1365-
name += prefix;
1366-
namelen -= prefix;
13671363

13681364
/*
13691365
* If the whole pattern did not have a wildcard,
13701366
* then our prefix match is all we need; we
13711367
* do not need to call fnmatch at all.
13721368
*/
1373-
if (!patternlen && !namelen)
1369+
if (patternlen == prefix && namelen == prefix)
13741370
return 1;
1371+
1372+
pattern += prefix;
1373+
patternlen -= prefix;
1374+
name += prefix;
1375+
namelen -= prefix;
13751376
}
13761377

13771378
return fnmatch_icase_mem(pattern, patternlen,

0 commit comments

Comments
 (0)