Skip to content

Commit 17597c0

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 805181e + a23a7e0 commit 17597c0

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
@@ -1068,16 +1068,59 @@ static int add_patterns(const char *fname, const char *base, int baselen,
10681068
size_t size = 0;
10691069
char *buf;
10701070

1071-
if (flags & PATTERN_NOFOLLOW)
1072-
fd = open_nofollow(fname, O_RDONLY);
1073-
else
1074-
fd = open(fname, O_RDONLY);
1075-
1076-
if (fd < 0 || fstat(fd, &st) < 0) {
1077-
if (fd < 0)
1078-
warn_on_fopen_errors(fname);
1071+
/*
1072+
* A performance optimization for status.
1073+
*
1074+
* During a status scan, git looks in each directory for a .gitignore
1075+
* file before scanning the directory. Since .gitignore files are not
1076+
* that common, we can waste a lot of time looking for files that are
1077+
* not there. Fortunately, the fscache already knows if the directory
1078+
* contains a .gitignore file, since it has already read the directory
1079+
* and it already has the stat-data.
1080+
*
1081+
* If the fscache is enabled, use the fscache-lstat() interlude to see
1082+
* if the file exists (in the fscache hash maps) before trying to open()
1083+
* it.
1084+
*
1085+
* This causes problem when the .gitignore file is a symlink, because
1086+
* we call lstat() rather than stat() on the symlnk and the resulting
1087+
* stat-data is for the symlink itself rather than the target file.
1088+
* We CANNOT use stat() here because the fscache DOES NOT install an
1089+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
1090+
* on the file and defeats the purpose of the optimization here. Since
1091+
* symlinks are even more rare than .gitignore files, we force a fstat()
1092+
* after our open() to get stat-data for the target file.
1093+
*/
1094+
if (is_fscache_enabled(fname)) {
1095+
if (lstat(fname, &st) < 0) {
1096+
fd = -1;
1097+
} else {
1098+
fd = open(fname, O_RDONLY);
1099+
if (fd < 0)
1100+
warn_on_fopen_errors(fname);
1101+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
1102+
warn_on_fopen_errors(fname);
1103+
close(fd);
1104+
fd = -1;
1105+
}
1106+
}
1107+
} else {
1108+
if (flags & PATTERN_NOFOLLOW)
1109+
fd = open_nofollow(fname, O_RDONLY);
10791110
else
1080-
close(fd);
1111+
fd = open(fname, O_RDONLY);
1112+
1113+
if (fd < 0 || fstat(fd, &st) < 0) {
1114+
if (fd < 0)
1115+
warn_on_fopen_errors(fname);
1116+
else {
1117+
close(fd);
1118+
fd = -1;
1119+
}
1120+
}
1121+
}
1122+
1123+
if (fd < 0) {
10811124
if (!istate)
10821125
return -1;
10831126
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
@@ -1583,6 +1583,10 @@ static inline int is_missing_file_error(int errno_)
15831583
#define enable_fscache(x) /* noop */
15841584
#endif
15851585

1586+
#ifndef is_fscache_enabled
1587+
#define is_fscache_enabled(path) (0)
1588+
#endif
1589+
15861590
int cmd_main(int, const char **);
15871591

15881592
/*

0 commit comments

Comments
 (0)