Speedup scanning for excluded files.
Try to avoid a lot of work scanning for excluded files, by caching some more information when setting up the exclusion data structure. Speeds up 'git runstatus' on a repository containing the Qt sources by 30% and reduces the amount of instructions executed (as measured by valgrind) by a factor of 2. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Lars Knoll committed
Oct 28, 2007 at 21:27 UTC
68492fc73b7387fa72d09545f5743728c5fdb419
2 files changed
+50
-16
dir.c
+43
-16
@@ -118,14 +118,32 @@ int match_pathspec(const char **pathspec, const char *name, int namelen, int pre
118
return retval;
119
}
120
121
+static int no_wildcard(const char *string)
122
+{
123
+ return string[strcspn(string, "*?[{")] == '\0';
124
+}
125
+
126
void add_exclude(const char *string, const char *base,
127
int baselen, struct exclude_list *which)
128
{
129
struct exclude *x = xmalloc(sizeof (*x));
130
131
+ x->to_exclude = 1;
132
+ if (*string == '!') {
133
+ x->to_exclude = 0;
134
+ string++;
135
+ }
136
x->pattern = string;
137
+ x->patternlen = strlen(string);
138
x->base = base;
139
x->baselen = baselen;
140
+ x->flags = 0;
141
+ if (!strchr(string, '/'))
142
+ x->flags |= EXC_FLAG_NODIR;
143
+ if (no_wildcard(string))
144
+ x->flags |= EXC_FLAG_NOWILDCARD;
145
+ if (*string == '*' && no_wildcard(string+1))
146
+ x->flags |= EXC_FLAG_ENDSWITH;
147
if (which->nr == which->alloc) {
148
which->alloc = alloc_nr(which->alloc);
149
which->excludes = xrealloc(which->excludes,
@@ -209,7 +227,7 @@ void pop_exclude_per_directory(struct dir_struct *dir, int stk)
227
* Return 1 for exclude, 0 for include and -1 for undecided.
228
*/
229
static int excluded_1(const char *pathname,
212
- int pathlen,
230
+ int pathlen, const char *basename,
231
struct exclude_list *el)
232
{
233
int i;
@@ -218,19 +236,21 @@ static int excluded_1(const char *pathname,
236
for (i = el->nr - 1; 0 <= i; i--) {
237
struct exclude *x = el->excludes[i];
238
const char *exclude = x->pattern;
221
- int to_exclude = 1;
239
+ int to_exclude = x->to_exclude;
240
223
- if (*exclude == '!') {
224
- to_exclude = 0;
225
- exclude++;
226
- }
227
-
228
- if (!strchr(exclude, '/')) {
241
+ if (x->flags & EXC_FLAG_NODIR) {
242
/* match basename */
230
- const char *basename = strrchr(pathname, '/');
231
- basename = (basename) ? basename+1 : pathname;
232
- if (fnmatch(exclude, basename, 0) == 0)
233
- return to_exclude;
243
+ if (x->flags & EXC_FLAG_NOWILDCARD) {
244
+ if (!strcmp(exclude, basename))
245
+ return to_exclude;
246
+ } else if (x->flags & EXC_FLAG_ENDSWITH) {
247
+ if (x->patternlen - 1 <= pathlen &&
248
+ !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
249
+ return to_exclude;
250
+ } else {
251
+ if (fnmatch(exclude, basename, 0) == 0)
252
+ return to_exclude;
253
+ }
254
}
255
else {
256
/* match with FNM_PATHNAME:
@@ -246,9 +266,14 @@ static int excluded_1(const char *pathname,
266
strncmp(pathname, x->base, baselen))
267
continue;
268
249
- if (fnmatch(exclude, pathname+baselen,
250
- FNM_PATHNAME) == 0)
251
- return to_exclude;
269
+ if (x->flags & EXC_FLAG_NOWILDCARD) {
270
+ if (!strcmp(exclude, pathname + baselen))
271
+ return to_exclude;
272
+ } else {
273
+ if (fnmatch(exclude, pathname+baselen,
274
+ FNM_PATHNAME) == 0)
275
+ return to_exclude;
276
+ }
277
}
278
}
279
}
@@ -259,9 +284,11 @@ int excluded(struct dir_struct *dir, const char *pathname)
284
{
285
int pathlen = strlen(pathname);
286
int st;
287
+ const char *basename = strrchr(pathname, '/');
288
+ basename = (basename) ? basename+1 : pathname;
289
290
for (st = EXC_CMDL; st <= EXC_FILE; st++) {
264
- switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
291
+ switch (excluded_1(pathname, pathlen, basename, &dir->exclude_list[st])) {
292
case 0:
293
return 0;
294
case 1:
dir.h
+7
@@ -17,13 +17,20 @@ struct dir_entry {
17
char name[FLEX_ARRAY]; /* more */
18
};
19
20
+#define EXC_FLAG_NODIR 1
21
+#define EXC_FLAG_NOWILDCARD 2
22
+#define EXC_FLAG_ENDSWITH 4
23
+
24
struct exclude_list {
25
int nr;
26
int alloc;
27
struct exclude {
28
const char *pattern;
29
+ int patternlen;
30
const char *base;
31
int baselen;
32
+ int to_exclude;
33
+ int flags;
34
} **excludes;
35
};
36