Skip to content

Commit 47db01a

Browse files
jeffhostetlerdscho
authored andcommitted
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]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 482228f commit 47db01a

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
@@ -163,7 +163,8 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
163163
* Dir should not contain trailing '/'. Use an empty string for the current
164164
* directory (not "."!).
165165
*/
166-
static struct fsentry *fsentry_create_list(const struct fsentry *dir)
166+
static struct fsentry *fsentry_create_list(const struct fsentry *dir,
167+
int *dir_not_found)
167168
{
168169
wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */
169170
WIN32_FIND_DATAW fdata;
@@ -172,6 +173,8 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
172173
struct fsentry *list, **phead;
173174
DWORD err;
174175

176+
*dir_not_found = 0;
177+
175178
/* convert name to UTF-16 and check length */
176179
if ((wlen = xutftowcs_path_ex(pattern, dir->name, MAX_LONG_PATH,
177180
dir->len, MAX_PATH - 2, core_long_paths)) < 0)
@@ -190,6 +193,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
190193
h = FindFirstFileW(pattern, &fdata);
191194
if (h == INVALID_HANDLE_VALUE) {
192195
err = GetLastError();
196+
*dir_not_found = 1; /* or empty directory */
193197
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
194198
trace_printf_key(&trace_fscache, "fscache: error(%d) '%.*s'\n",
195199
errno, dir->len, dir->name);
@@ -198,6 +202,7 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
198202

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

202207
/* walk directory and build linked list of fsentry structures */
203208
phead = &list->next;
@@ -301,12 +306,16 @@ static struct fsentry *fscache_get_wait(struct fsentry *key)
301306
static struct fsentry *fscache_get(struct fsentry *key)
302307
{
303308
struct fsentry *fse, *future, *waiter;
309+
int dir_not_found;
304310

305311
EnterCriticalSection(&mutex);
306312
/* check if entry is in cache */
307313
fse = fscache_get_wait(key);
308314
if (fse) {
309-
fsentry_addref(fse);
315+
if (fse->st_mode)
316+
fsentry_addref(fse);
317+
else
318+
fse = NULL; /* non-existing directory */
310319
LeaveCriticalSection(&mutex);
311320
return fse;
312321
}
@@ -315,7 +324,10 @@ static struct fsentry *fscache_get(struct fsentry *key)
315324
fse = fscache_get_wait(key->list);
316325
if (fse) {
317326
LeaveCriticalSection(&mutex);
318-
/* dir entry without file entry -> file doesn't exist */
327+
/*
328+
* dir entry without file entry, or dir does not
329+
* exist -> file doesn't exist
330+
*/
319331
errno = ENOENT;
320332
return NULL;
321333
}
@@ -329,7 +341,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
329341

330342
/* create the directory listing (outside mutex!) */
331343
LeaveCriticalSection(&mutex);
332-
fse = fsentry_create_list(future);
344+
fse = fsentry_create_list(future, &dir_not_found);
333345
EnterCriticalSection(&mutex);
334346

335347
/* remove future entry and signal waiting threads */
@@ -343,6 +355,17 @@ static struct fsentry *fscache_get(struct fsentry *key)
343355

344356
/* leave on error (errno set by fsentry_create_list) */
345357
if (!fse) {
358+
if (dir_not_found && key->list) {
359+
/*
360+
* Record that the directory does not exist (or is
361+
* empty, which for all practical matters is the same
362+
* thing as far as fscache is concerned).
363+
*/
364+
fse = fsentry_alloc(key->list->list,
365+
key->list->name, key->list->len);
366+
fse->st_mode = 0;
367+
hashmap_add(&map, fse);
368+
}
346369
LeaveCriticalSection(&mutex);
347370
return NULL;
348371
}
@@ -354,6 +377,9 @@ static struct fsentry *fscache_get(struct fsentry *key)
354377
if (key->list)
355378
fse = hashmap_get(&map, key, NULL);
356379

380+
if (fse && !fse->st_mode)
381+
fse = NULL; /* non-existing directory */
382+
357383
/* return entry or ENOENT */
358384
if (fse)
359385
fsentry_addref(fse);

0 commit comments

Comments
 (0)