Skip to content

Commit bf8f86e

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 40590db + 3719da2 commit bf8f86e

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
@@ -785,12 +785,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
785785
size_t size = 0;
786786
char *buf;
787787

788-
fd = open(fname, O_RDONLY);
789-
if (fd < 0 || fstat(fd, &st) < 0) {
790-
if (fd < 0)
791-
warn_on_fopen_errors(fname);
792-
else
793-
close(fd);
788+
/*
789+
* A performance optimization for status.
790+
*
791+
* During a status scan, git looks in each directory for a .gitignore
792+
* file before scanning the directory. Since .gitignore files are not
793+
* that common, we can waste a lot of time looking for files that are
794+
* not there. Fortunately, the fscache already knows if the directory
795+
* contains a .gitignore file, since it has already read the directory
796+
* and it already has the stat-data.
797+
*
798+
* If the fscache is enabled, use the fscache-lstat() interlude to see
799+
* if the file exists (in the fscache hash maps) before trying to open()
800+
* it.
801+
*
802+
* This causes problem when the .gitignore file is a symlink, because
803+
* we call lstat() rather than stat() on the symlnk and the resulting
804+
* stat-data is for the symlink itself rather than the target file.
805+
* We CANNOT use stat() here because the fscache DOES NOT install an
806+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
807+
* on the file and defeats the purpose of the optimization here. Since
808+
* symlinks are even more rare than .gitignore files, we force a fstat()
809+
* after our open() to get stat-data for the target file.
810+
*/
811+
if (is_fscache_enabled(fname)) {
812+
if (lstat(fname, &st) < 0) {
813+
fd = -1;
814+
} else {
815+
fd = open(fname, O_RDONLY);
816+
if (fd < 0)
817+
warn_on_fopen_errors(fname);
818+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
819+
warn_on_fopen_errors(fname);
820+
close(fd);
821+
fd = -1;
822+
}
823+
}
824+
} else {
825+
fd = open(fname, O_RDONLY);
826+
if (fd < 0 || fstat(fd, &st) < 0) {
827+
if (fd < 0)
828+
warn_on_fopen_errors(fname);
829+
else {
830+
close(fd);
831+
fd = -1;
832+
}
833+
}
834+
}
835+
836+
if (fd < 0) {
794837
if (!istate)
795838
return -1;
796839
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
@@ -1307,6 +1307,10 @@ static inline int is_missing_file_error(int errno_)
13071307
#define enable_fscache(x) /* noop */
13081308
#endif
13091309

1310+
#ifndef is_fscache_enabled
1311+
#define is_fscache_enabled(path) (0)
1312+
#endif
1313+
13101314
int cmd_main(int, const char **);
13111315

13121316
/*

0 commit comments

Comments
 (0)