Skip to content

Commit e5acc73

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 35e20e1 + 16b9939 commit e5acc73

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

compat/win32/fscache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static void fscache_clear(void)
252252
/*
253253
* Checks if the cache is enabled for the given path.
254254
*/
255-
static inline int fscache_enabled(const char *path)
255+
int fscache_enabled(const char *path)
256256
{
257257
return enabled > 0 && !is_absolute_path(path);
258258
}

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: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -824,12 +824,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
824824
size_t size = 0;
825825
char *buf;
826826

827-
fd = open(fname, O_RDONLY);
828-
if (fd < 0 || fstat(fd, &st) < 0) {
829-
if (fd < 0)
830-
warn_on_fopen_errors(fname);
831-
else
832-
close(fd);
827+
/*
828+
* A performance optimization for status.
829+
*
830+
* During a status scan, git looks in each directory for a .gitignore
831+
* file before scanning the directory. Since .gitignore files are not
832+
* that common, we can waste a lot of time looking for files that are
833+
* not there. Fortunately, the fscache already knows if the directory
834+
* contains a .gitignore file, since it has already read the directory
835+
* and it already has the stat-data.
836+
*
837+
* If the fscache is enabled, use the fscache-lstat() interlude to see
838+
* if the file exists (in the fscache hash maps) before trying to open()
839+
* it.
840+
*
841+
* This causes problem when the .gitignore file is a symlink, because
842+
* we call lstat() rather than stat() on the symlnk and the resulting
843+
* stat-data is for the symlink itself rather than the target file.
844+
* We CANNOT use stat() here because the fscache DOES NOT install an
845+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
846+
* on the file and defeats the purpose of the optimization here. Since
847+
* symlinks are even more rare than .gitignore files, we force a fstat()
848+
* after our open() to get stat-data for the target file.
849+
*/
850+
if (is_fscache_enabled(fname)) {
851+
if (lstat(fname, &st) < 0) {
852+
fd = -1;
853+
} else {
854+
fd = open(fname, O_RDONLY);
855+
if (fd < 0)
856+
warn_on_fopen_errors(fname);
857+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
858+
warn_on_fopen_errors(fname);
859+
close(fd);
860+
fd = -1;
861+
}
862+
}
863+
} else {
864+
fd = open(fname, O_RDONLY);
865+
if (fd < 0 || fstat(fd, &st) < 0) {
866+
if (fd < 0)
867+
warn_on_fopen_errors(fname);
868+
else {
869+
close(fd);
870+
fd = -1;
871+
}
872+
}
873+
}
874+
875+
if (fd < 0) {
833876
if (!istate)
834877
return -1;
835878
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
@@ -1265,6 +1265,10 @@ static inline int is_missing_file_error(int errno_)
12651265
#define enable_fscache(x) /* noop */
12661266
#endif
12671267

1268+
#ifndef is_fscache_enabled
1269+
#define is_fscache_enabled(path) (0)
1270+
#endif
1271+
12681272
extern int cmd_main(int, const char **);
12691273

12701274
/*

0 commit comments

Comments
 (0)