Skip to content

Commit 8544ec3

Browse files
committed
fscache: remember not-found directories
Teach FSCACHE to remember "not found" directories. This is a performance optimization. FSCACHE is a performance optimization available for Windows. It intercepts Posix-style lstat() calls into an in-memory directory using FindFirst/FindNext. It improves performance on Windows by catching the first lstat() call in a directory, using FindFirst/ FindNext to read the list of files (and attribute data) for the entire directory into the cache, and short-cut subsequent lstat() calls in the same directory. This gives a major performance boost on Windows. However, it does not remember "not found" directories. When STATUS runs and there are missing directories, the lstat() interception fails to find the parent directory and simply return ENOENT for the file -- it does not remember that the FindFirst on the directory failed. Thus subsequent lstat() calls in the same directory, each re-attempt the FindFirst. This completely defeats any performance gains. This can be seen by doing a sparse-checkout on a large repo and then doing a read-tree to reset the skip-worktree bits and then running status. This change reduced status times for my very large repo by 60%. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 0217bed commit 8544ec3

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

compat/win32/fscache.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
164164
* Dir should not contain trailing '/'. Use an empty string for the current
165165
* directory (not "."!).
166166
*/
167-
static struct fsentry *fsentry_create_list(const struct fsentry *dir)
167+
static struct fsentry *fsentry_create_list(const struct fsentry *dir,
168+
int *dir_not_found)
168169
{
169170
wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */
170171
WIN32_FIND_DATAW fdata;
@@ -173,6 +174,8 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
173174
struct fsentry *list, **phead;
174175
DWORD err;
175176

177+
*dir_not_found = 0;
178+
176179
/* convert name to UTF-16 and check length */
177180
if ((wlen = xutftowcs_path_ex(pattern, dir->name, MAX_LONG_PATH,
178181
dir->len, MAX_PATH - 2, core_long_paths)) < 0)
@@ -191,6 +194,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
191194
h = FindFirstFileW(pattern, &fdata);
192195
if (h == INVALID_HANDLE_VALUE) {
193196
err = GetLastError();
197+
*dir_not_found = 1; /* or empty directory */
194198
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
195199
trace_printf_key(&trace_fscache, "fscache: error(%d) '%.*s'\n",
196200
errno, dir->len, dir->name);
@@ -199,6 +203,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
199203

200204
/* allocate object to hold directory listing */
201205
list = fsentry_alloc(NULL, dir->name, dir->len);
206+
list->st_mode = S_IFDIR;
202207

203208
/* walk directory and build linked list of fsentry structures */
204209
phead = &list->next;
@@ -299,12 +304,16 @@ static struct fsentry *fscache_get_wait(struct fsentry *key)
299304
static struct fsentry *fscache_get(struct fsentry *key)
300305
{
301306
struct fsentry *fse, *future, *waiter;
307+
int dir_not_found;
302308

303309
EnterCriticalSection(&mutex);
304310
/* check if entry is in cache */
305311
fse = fscache_get_wait(key);
306312
if (fse) {
307-
fsentry_addref(fse);
313+
if (fse->st_mode)
314+
fsentry_addref(fse);
315+
else
316+
fse = NULL; /* non-existing directory */
308317
LeaveCriticalSection(&mutex);
309318
return fse;
310319
}
@@ -313,7 +322,10 @@ static struct fsentry *fscache_get(struct fsentry *key)
313322
fse = fscache_get_wait(key->list);
314323
if (fse) {
315324
LeaveCriticalSection(&mutex);
316-
/* dir entry without file entry -> file doesn't exist */
325+
/*
326+
* dir entry without file entry, or dir does not
327+
* exist -> file doesn't exist
328+
*/
317329
errno = ENOENT;
318330
return NULL;
319331
}
@@ -327,7 +339,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
327339

328340
/* create the directory listing (outside mutex!) */
329341
LeaveCriticalSection(&mutex);
330-
fse = fsentry_create_list(future);
342+
fse = fsentry_create_list(future, &dir_not_found);
331343
EnterCriticalSection(&mutex);
332344

333345
/* remove future entry and signal waiting threads */
@@ -341,6 +353,17 @@ static struct fsentry *fscache_get(struct fsentry *key)
341353

342354
/* leave on error (errno set by fsentry_create_list) */
343355
if (!fse) {
356+
if (dir_not_found && key->list) {
357+
/*
358+
* Record that the directory does not exist (or is
359+
* empty, which for all practical matters is the same
360+
* thing as far as fscache is concerned).
361+
*/
362+
fse = fsentry_alloc(key->list->list,
363+
key->list->name, key->list->len);
364+
fse->st_mode = 0;
365+
hashmap_add(&map, fse);
366+
}
344367
LeaveCriticalSection(&mutex);
345368
return NULL;
346369
}
@@ -352,6 +375,9 @@ static struct fsentry *fscache_get(struct fsentry *key)
352375
if (key->list)
353376
fse = hashmap_get(&map, key, NULL);
354377

378+
if (fse && !fse->st_mode)
379+
fse = NULL; /* non-existing directory */
380+
355381
/* return entry or ENOENT */
356382
if (fse)
357383
fsentry_addref(fse);

0 commit comments

Comments
 (0)