Skip to content

Commit f11e964

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 3e76d7a commit f11e964

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;
@@ -298,12 +303,16 @@ static struct fsentry *fscache_get_wait(struct fsentry *key)
298303
static struct fsentry *fscache_get(struct fsentry *key)
299304
{
300305
struct fsentry *fse, *future, *waiter;
306+
int dir_not_found;
301307

302308
EnterCriticalSection(&mutex);
303309
/* check if entry is in cache */
304310
fse = fscache_get_wait(key);
305311
if (fse) {
306-
fsentry_addref(fse);
312+
if (fse->st_mode)
313+
fsentry_addref(fse);
314+
else
315+
fse = NULL; /* non-existing directory */
307316
LeaveCriticalSection(&mutex);
308317
return fse;
309318
}
@@ -312,7 +321,10 @@ static struct fsentry *fscache_get(struct fsentry *key)
312321
fse = fscache_get_wait(key->list);
313322
if (fse) {
314323
LeaveCriticalSection(&mutex);
315-
/* dir entry without file entry -> file doesn't exist */
324+
/*
325+
* dir entry without file entry, or dir does not
326+
* exist -> file doesn't exist
327+
*/
316328
errno = ENOENT;
317329
return NULL;
318330
}
@@ -326,7 +338,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
326338

327339
/* create the directory listing (outside mutex!) */
328340
LeaveCriticalSection(&mutex);
329-
fse = fsentry_create_list(future);
341+
fse = fsentry_create_list(future, &dir_not_found);
330342
EnterCriticalSection(&mutex);
331343

332344
/* remove future entry and signal waiting threads */
@@ -340,6 +352,17 @@ static struct fsentry *fscache_get(struct fsentry *key)
340352

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

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

0 commit comments

Comments
 (0)