Skip to content

Commit 9d0d998

Browse files
dschomjcheetham
authored andcommitted
Merge pull request #1344 from jeffhostetler/perf_add_excludes_with_fscache
dir.c: make add_excludes aware of fscache during status
2 parents c290abf + 052cecc commit 9d0d998

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
@@ -1113,16 +1113,59 @@ static int add_patterns(const char *fname, const char *base, int baselen,
11131113
size_t size = 0;
11141114
char *buf;
11151115

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

1540+
#ifndef is_fscache_enabled
1541+
#define is_fscache_enabled(path) (0)
1542+
#endif
1543+
15401544
int cmd_main(int, const char **);
15411545

15421546
/*

0 commit comments

Comments
 (0)