Skip to content

Commit ec63f9b

Browse files
committed
Merge pull request #1344 from jeffhostetler/perf_add_excludes_with_fscache
dir.c: make add_excludes aware of fscache during status
2 parents 87a5864 + ef96151 commit ec63f9b

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

compat/win32/fscache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static void fscache_clear(void)
266266
/*
267267
* Checks if the cache is enabled for the given path.
268268
*/
269-
static inline int fscache_enabled(const char *path)
269+
int fscache_enabled(const char *path)
270270
{
271271
return enabled > 0 && !is_absolute_path(path);
272272
}

compat/win32/fscache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
int fscache_enable(int enable);
55
#define enable_fscache(x) fscache_enable(x)
66

7+
int fscache_enabled(const char *path);
8+
#define is_fscache_enabled(path) fscache_enabled(path)
9+
710
DIR *fscache_opendir(const char *dir);
811
int fscache_lstat(const char *file_name, struct stat *buf);
912

dir.c

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,16 +1063,59 @@ static int add_patterns(const char *fname, const char *base, int baselen,
10631063
size_t size = 0;
10641064
char *buf;
10651065

1066-
if (flags & PATTERN_NOFOLLOW)
1067-
fd = open_nofollow(fname, O_RDONLY);
1068-
else
1069-
fd = open(fname, O_RDONLY);
1070-
1071-
if (fd < 0 || fstat(fd, &st) < 0) {
1072-
if (fd < 0)
1073-
warn_on_fopen_errors(fname);
1066+
/*
1067+
* A performance optimization for status.
1068+
*
1069+
* During a status scan, git looks in each directory for a .gitignore
1070+
* file before scanning the directory. Since .gitignore files are not
1071+
* that common, we can waste a lot of time looking for files that are
1072+
* not there. Fortunately, the fscache already knows if the directory
1073+
* contains a .gitignore file, since it has already read the directory
1074+
* and it already has the stat-data.
1075+
*
1076+
* If the fscache is enabled, use the fscache-lstat() interlude to see
1077+
* if the file exists (in the fscache hash maps) before trying to open()
1078+
* it.
1079+
*
1080+
* This causes problem when the .gitignore file is a symlink, because
1081+
* we call lstat() rather than stat() on the symlnk and the resulting
1082+
* stat-data is for the symlink itself rather than the target file.
1083+
* We CANNOT use stat() here because the fscache DOES NOT install an
1084+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
1085+
* on the file and defeats the purpose of the optimization here. Since
1086+
* symlinks are even more rare than .gitignore files, we force a fstat()
1087+
* after our open() to get stat-data for the target file.
1088+
*/
1089+
if (is_fscache_enabled(fname)) {
1090+
if (lstat(fname, &st) < 0) {
1091+
fd = -1;
1092+
} else {
1093+
fd = open(fname, O_RDONLY);
1094+
if (fd < 0)
1095+
warn_on_fopen_errors(fname);
1096+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
1097+
warn_on_fopen_errors(fname);
1098+
close(fd);
1099+
fd = -1;
1100+
}
1101+
}
1102+
} else {
1103+
if (flags & PATTERN_NOFOLLOW)
1104+
fd = open_nofollow(fname, O_RDONLY);
10741105
else
1075-
close(fd);
1106+
fd = open(fname, O_RDONLY);
1107+
1108+
if (fd < 0 || fstat(fd, &st) < 0) {
1109+
if (fd < 0)
1110+
warn_on_fopen_errors(fname);
1111+
else {
1112+
close(fd);
1113+
fd = -1;
1114+
}
1115+
}
1116+
}
1117+
1118+
if (fd < 0) {
10761119
if (!istate)
10771120
return -1;
10781121
r = read_skip_worktree_file_from_index(istate, fname,

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,10 @@ static inline int is_missing_file_error(int errno_)
14211421
#define enable_fscache(x) /* noop */
14221422
#endif
14231423

1424+
#ifndef is_fscache_enabled
1425+
#define is_fscache_enabled(path) (0)
1426+
#endif
1427+
14241428
int cmd_main(int, const char **);
14251429

14261430
/*

0 commit comments

Comments
 (0)