Skip to content

Commit ccec401

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 79f371f + 9972877 commit ccec401

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
@@ -817,12 +817,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
817817
size_t size = 0;
818818
char *buf;
819819

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

1253+
#ifndef is_fscache_enabled
1254+
#define is_fscache_enabled(path) (0)
1255+
#endif
1256+
12531257
extern int cmd_main(int, const char **);
12541258

12551259
/*

0 commit comments

Comments
 (0)