Skip to content

Commit 7b8af6e

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 cc0d2da + b648f3d commit 7b8af6e

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
@@ -268,7 +268,7 @@ static void fscache_clear(void)
268268
/*
269269
* Checks if the cache is enabled for the given path.
270270
*/
271-
static inline int fscache_enabled(const char *path)
271+
int fscache_enabled(const char *path)
272272
{
273273
return enabled > 0 && !is_absolute_path(path);
274274
}

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
@@ -1110,16 +1110,59 @@ static int add_patterns(const char *fname, const char *base, int baselen,
11101110
size_t size = 0;
11111111
char *buf;
11121112

1113-
if (flags & PATTERN_NOFOLLOW)
1114-
fd = open_nofollow(fname, O_RDONLY);
1115-
else
1116-
fd = open(fname, O_RDONLY);
1117-
1118-
if (fd < 0 || fstat(fd, &st) < 0) {
1119-
if (fd < 0)
1120-
warn_on_fopen_errors(fname);
1113+
/*
1114+
* A performance optimization for status.
1115+
*
1116+
* During a status scan, git looks in each directory for a .gitignore
1117+
* file before scanning the directory. Since .gitignore files are not
1118+
* that common, we can waste a lot of time looking for files that are
1119+
* not there. Fortunately, the fscache already knows if the directory
1120+
* contains a .gitignore file, since it has already read the directory
1121+
* and it already has the stat-data.
1122+
*
1123+
* If the fscache is enabled, use the fscache-lstat() interlude to see
1124+
* if the file exists (in the fscache hash maps) before trying to open()
1125+
* it.
1126+
*
1127+
* This causes problem when the .gitignore file is a symlink, because
1128+
* we call lstat() rather than stat() on the symlnk and the resulting
1129+
* stat-data is for the symlink itself rather than the target file.
1130+
* We CANNOT use stat() here because the fscache DOES NOT install an
1131+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
1132+
* on the file and defeats the purpose of the optimization here. Since
1133+
* symlinks are even more rare than .gitignore files, we force a fstat()
1134+
* after our open() to get stat-data for the target file.
1135+
*/
1136+
if (is_fscache_enabled(fname)) {
1137+
if (lstat(fname, &st) < 0) {
1138+
fd = -1;
1139+
} else {
1140+
fd = open(fname, O_RDONLY);
1141+
if (fd < 0)
1142+
warn_on_fopen_errors(fname);
1143+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
1144+
warn_on_fopen_errors(fname);
1145+
close(fd);
1146+
fd = -1;
1147+
}
1148+
}
1149+
} else {
1150+
if (flags & PATTERN_NOFOLLOW)
1151+
fd = open_nofollow(fname, O_RDONLY);
11211152
else
1122-
close(fd);
1153+
fd = open(fname, O_RDONLY);
1154+
1155+
if (fd < 0 || fstat(fd, &st) < 0) {
1156+
if (fd < 0)
1157+
warn_on_fopen_errors(fname);
1158+
else {
1159+
close(fd);
1160+
fd = -1;
1161+
}
1162+
}
1163+
}
1164+
1165+
if (fd < 0) {
11231166
if (!istate)
11241167
return -1;
11251168
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
@@ -1562,6 +1562,10 @@ static inline int is_missing_file_error(int errno_)
15621562
#define enable_fscache(x) /* noop */
15631563
#endif
15641564

1565+
#ifndef is_fscache_enabled
1566+
#define is_fscache_enabled(path) (0)
1567+
#endif
1568+
15651569
int cmd_main(int, const char **);
15661570

15671571
/*

0 commit comments

Comments
 (0)