Skip to content

Commit

Permalink
Merge pull request #1344 from jeffhostetler/perf_add_excludes_with_fs…
Browse files Browse the repository at this point in the history
…cache

dir.c: make add_excludes aware of fscache during status
  • Loading branch information
dscho committed Oct 12, 2018
2 parents 79f371f + 9972877 commit ccec401
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compat/win32/fscache.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ static void fscache_clear(void)
/*
* Checks if the cache is enabled for the given path.
*/
static inline int fscache_enabled(const char *path)
int fscache_enabled(const char *path)
{
return enabled > 0 && !is_absolute_path(path);
}
Expand Down
3 changes: 3 additions & 0 deletions compat/win32/fscache.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
int fscache_enable(int enable);
#define enable_fscache(x) fscache_enable(x)

int fscache_enabled(const char *path);
#define is_fscache_enabled(path) fscache_enabled(path)

DIR *fscache_opendir(const char *dir);
int fscache_lstat(const char *file_name, struct stat *buf);

Expand Down
55 changes: 49 additions & 6 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
size_t size = 0;
char *buf;

fd = open(fname, O_RDONLY);
if (fd < 0 || fstat(fd, &st) < 0) {
if (fd < 0)
warn_on_fopen_errors(fname);
else
close(fd);
/*
* A performance optimization for status.
*
* During a status scan, git looks in each directory for a .gitignore
* file before scanning the directory. Since .gitignore files are not
* that common, we can waste a lot of time looking for files that are
* not there. Fortunately, the fscache already knows if the directory
* contains a .gitignore file, since it has already read the directory
* and it already has the stat-data.
*
* If the fscache is enabled, use the fscache-lstat() interlude to see
* if the file exists (in the fscache hash maps) before trying to open()
* it.
*
* This causes problem when the .gitignore file is a symlink, because
* we call lstat() rather than stat() on the symlnk and the resulting
* stat-data is for the symlink itself rather than the target file.
* We CANNOT use stat() here because the fscache DOES NOT install an
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
* on the file and defeats the purpose of the optimization here. Since
* symlinks are even more rare than .gitignore files, we force a fstat()
* after our open() to get stat-data for the target file.
*/
if (is_fscache_enabled(fname)) {
if (lstat(fname, &st) < 0) {
fd = -1;
} else {
fd = open(fname, O_RDONLY);
if (fd < 0)
warn_on_fopen_errors(fname);
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
warn_on_fopen_errors(fname);
close(fd);
fd = -1;
}
}
} else {
fd = open(fname, O_RDONLY);
if (fd < 0 || fstat(fd, &st) < 0) {
if (fd < 0)
warn_on_fopen_errors(fname);
else {
close(fd);
fd = -1;
}
}
}

if (fd < 0) {
if (!istate)
return -1;
r = read_skip_worktree_file_from_index(istate, fname,
Expand Down
4 changes: 4 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,10 @@ static inline int is_missing_file_error(int errno_)
#define enable_fscache(x) /* noop */
#endif

#ifndef is_fscache_enabled
#define is_fscache_enabled(path) (0)
#endif

extern int cmd_main(int, const char **);

/*
Expand Down

0 comments on commit ccec401

Please sign in to comment.