Skip to content

Commit 9fc42d6

Browse files
torvaldsJunio C Hamano
authored andcommitted
Optimize directory listing with pathspec limiter.
The way things are set up, you can now pass a "pathspec" to the "read_directory()" function. If you pass NULL, it acts exactly like it used to do (read everything). If you pass a non-NULL pointer, it will simplify it into a "these are the prefixes without any special characters", and stop any readdir() early if the path in question doesn't match any of the prefixes. NOTE! This does *not* obviate the need for the caller to do the *exact* pathspec match later. It's a first-level filter on "read_directory()", but it does not do the full pathspec thing. Maybe it should. But in the meantime, builtin-add.c really does need to do first read_directory(dir, .., pathspec); if (pathspec) prune_directory(dir, pathspec, baselen); ie the "prune_directory()" part will do the *exact* pathspec pruning, while the "read_directory()" will use the pathspec just to do some quick high-level pruning of the directories it will recurse into. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent d8b6a1a commit 9fc42d6

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

builtin-add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec)
8787
}
8888

8989
/* Read the directory and prune it */
90-
read_directory(dir, path, base, baselen);
90+
read_directory(dir, path, base, baselen, pathspec);
9191
if (pathspec)
9292
prune_directory(dir, pathspec, baselen);
9393
}

builtin-ls-files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static void show_files(struct dir_struct *dir, const char *prefix)
216216

217217
if (baselen)
218218
path = base = prefix;
219-
read_directory(dir, path, base, baselen);
219+
read_directory(dir, path, base, baselen, pathspec);
220220
if (show_others)
221221
show_other_files(dir);
222222
if (show_killed)

dir.c

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#include "cache.h"
99
#include "dir.h"
1010

11+
struct path_simplify {
12+
int len;
13+
const char *path;
14+
};
15+
1116
int common_prefix(const char **pathspec)
1217
{
1318
const char *path, *slash, *next;
@@ -292,6 +297,31 @@ static int dir_exists(const char *dirname, int len)
292297
return !strncmp(active_cache[pos]->name, dirname, len);
293298
}
294299

300+
/*
301+
* This is an inexact early pruning of any recursive directory
302+
* reading - if the path cannot possibly be in the pathspec,
303+
* return true, and we'll skip it early.
304+
*/
305+
static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
306+
{
307+
if (simplify) {
308+
for (;;) {
309+
const char *match = simplify->path;
310+
int len = simplify->len;
311+
312+
if (!match)
313+
break;
314+
if (len > pathlen)
315+
len = pathlen;
316+
if (!memcmp(path, match, len))
317+
return 0;
318+
simplify++;
319+
}
320+
return 1;
321+
}
322+
return 0;
323+
}
324+
295325
/*
296326
* Read a directory tree. We currently ignore anything but
297327
* directories, regular files and symlinks. That's because git
@@ -301,7 +331,7 @@ static int dir_exists(const char *dirname, int len)
301331
* Also, we ignore the name ".git" (even if it is not a directory).
302332
* That likely will not change.
303333
*/
304-
static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only)
334+
static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
305335
{
306336
DIR *fdir = opendir(path);
307337
int contents = 0;
@@ -324,6 +354,8 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
324354
continue;
325355
len = strlen(de->d_name);
326356
memcpy(fullname + baselen, de->d_name, len+1);
357+
if (simplify_away(fullname, baselen + len, simplify))
358+
continue;
327359
if (excluded(dir, fullname) != dir->show_ignored) {
328360
if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
329361
continue;
@@ -350,13 +382,13 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
350382
if (dir->hide_empty_directories &&
351383
!read_directory_recursive(dir,
352384
fullname, fullname,
353-
baselen + len, 1))
385+
baselen + len, 1, simplify))
354386
continue;
355387
break;
356388
}
357389

358390
contents += read_directory_recursive(dir,
359-
fullname, fullname, baselen + len, 0);
391+
fullname, fullname, baselen + len, 0, simplify);
360392
continue;
361393
case DT_REG:
362394
case DT_LNK:
@@ -386,8 +418,61 @@ static int cmp_name(const void *p1, const void *p2)
386418
e2->name, e2->len);
387419
}
388420

389-
int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
421+
/*
422+
* Return the length of the "simple" part of a path match limiter.
423+
*/
424+
static int simple_length(const char *match)
390425
{
426+
const char special[256] = {
427+
[0] = 1, ['?'] = 1,
428+
['\\'] = 1, ['*'] = 1,
429+
['['] = 1
430+
};
431+
int len = -1;
432+
433+
for (;;) {
434+
unsigned char c = *match++;
435+
len++;
436+
if (special[c])
437+
return len;
438+
}
439+
}
440+
441+
static struct path_simplify *create_simplify(const char **pathspec)
442+
{
443+
int nr, alloc = 0;
444+
struct path_simplify *simplify = NULL;
445+
446+
if (!pathspec)
447+
return NULL;
448+
449+
for (nr = 0 ; ; nr++) {
450+
const char *match;
451+
if (nr >= alloc) {
452+
alloc = alloc_nr(alloc);
453+
simplify = xrealloc(simplify, alloc * sizeof(*simplify));
454+
}
455+
match = *pathspec++;
456+
if (!match)
457+
break;
458+
simplify[nr].path = match;
459+
simplify[nr].len = simple_length(match);
460+
}
461+
simplify[nr].path = NULL;
462+
simplify[nr].len = 0;
463+
return simplify;
464+
}
465+
466+
static void free_simplify(struct path_simplify *simplify)
467+
{
468+
if (simplify)
469+
free(simplify);
470+
}
471+
472+
int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
473+
{
474+
struct path_simplify *simplify = create_simplify(pathspec);
475+
391476
/*
392477
* Make sure to do the per-directory exclude for all the
393478
* directories leading up to our base.
@@ -414,7 +499,8 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
414499
}
415500
}
416501

417-
read_directory_recursive(dir, path, base, baselen, 0);
502+
read_directory_recursive(dir, path, base, baselen, 0, simplify);
503+
free_simplify(simplify);
418504
qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
419505
return dir->nr;
420506
}

dir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern int common_prefix(const char **pathspec);
4848
#define MATCHED_EXACTLY 3
4949
extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
5050

51-
extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
51+
extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec);
5252
extern int push_exclude_per_directory(struct dir_struct *, const char *, int);
5353
extern void pop_exclude_per_directory(struct dir_struct *, int);
5454

wt-status.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static void wt_status_print_untracked(struct wt_status *s)
260260
if (file_exists(x))
261261
add_excludes_from_file(&dir, x);
262262

263-
read_directory(&dir, ".", "", 0);
263+
read_directory(&dir, ".", "", 0, NULL);
264264
for(i = 0; i < dir.nr; i++) {
265265
/* check for matching entry, which is unmerged; lifted from
266266
* builtin-ls-files:show_other_files */

0 commit comments

Comments
 (0)