Skip to content

Commit 0156716

Browse files
derrickstoleedscho
authored andcommitted
Merge pull request #315: unpack-trees:virtualfilesystem: Improve efficiency of clear_ce_flags
When the virtualfilesystem is enabled the previous implementation of clear_ce_flags would iterate all of the cache entries and query whether each one is in the virtual filesystem to determine whether to clear one of the SKIP_WORKTREE bits. For each cache entry, we would do a hash lookup for each parent directory in the is_included_in_virtualfilesystem function. The former approach is slow for a typical Windows OS enlistment with 3 million files where only a small percentage is in the virtual filesystem. The cost is O(n_index_entries * n_chars_per_path * n_parent_directories_per_path). In this change, we use the same approach as apply_virtualfilesystem, which iterates the set of entries in the virtualfilesystem and searches in the cache for the corresponding entries in order to clear their flags. This approach has a cost of O(n_virtual_filesystem_entries * n_chars_per_path * log(n_index_entries)). The apply_virtualfilesystem code was refactored a bit and modified to clear flags for all names that 'alias' a given virtual filesystem name when ignore_case is set. n_virtual_filesystem_entries is typically much less than n_index_entries, in which case the new approach is much faster. We wind up building the name hash for the index, but this occurs quickly thanks to the multi-threading.
2 parents cdbb766 + 7896a46 commit 0156716

File tree

5 files changed

+132
-65
lines changed

5 files changed

+132
-65
lines changed

name-hash.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,26 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
743743
return NULL;
744744
}
745745

746+
struct cache_entry *index_file_next_match(struct index_state *istate, struct cache_entry *ce, int igncase)
747+
{
748+
struct cache_entry *next;
749+
750+
if (!igncase || !ce) {
751+
return NULL;
752+
}
753+
754+
next = hashmap_get_next_entry(&istate->name_hash, ce, ent);
755+
if (!next)
756+
return NULL;
757+
758+
hashmap_for_each_entry_from(&istate->name_hash, next, ent) {
759+
if (same_name(next, ce->name, ce_namelen(ce), igncase))
760+
return next;
761+
}
762+
763+
return NULL;
764+
}
765+
746766
void free_name_hash(struct index_state *istate)
747767
{
748768
if (!istate->name_hash_initialized)

name-hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int index_dir_find(struct index_state *istate, const char *name, int namelen,
1212

1313
void adjust_dirname_case(struct index_state *istate, char *name);
1414
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
15+
struct cache_entry *index_file_next_match(struct index_state *istate, struct cache_entry *ce, int igncase);
1516

1617
int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
1718
void add_name_hash(struct index_state *istate, struct cache_entry *ce);

unpack-trees.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,14 +1718,6 @@ static int clear_ce_flags_1(struct index_state *istate,
17181718
continue;
17191719
}
17201720

1721-
/* if it's not in the virtual file system, exit early */
1722-
if (core_virtualfilesystem) {
1723-
if (is_included_in_virtualfilesystem(ce->name, ce->ce_namelen) > 0)
1724-
ce->ce_flags &= ~clear_mask;
1725-
cache++;
1726-
continue;
1727-
}
1728-
17291721
if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
17301722
break;
17311723

@@ -1802,12 +1794,19 @@ static int clear_ce_flags(struct index_state *istate,
18021794
xsnprintf(label, sizeof(label), "clear_ce_flags/0x%08lx_0x%08lx",
18031795
(unsigned long)select_mask, (unsigned long)clear_mask);
18041796
trace2_region_enter("unpack_trees", label, the_repository);
1805-
rval = clear_ce_flags_1(istate,
1806-
istate->cache,
1807-
istate->cache_nr,
1808-
&prefix,
1809-
select_mask, clear_mask,
1810-
pl, 0, 0);
1797+
if (core_virtualfilesystem) {
1798+
rval = clear_ce_flags_virtualfilesystem(istate,
1799+
select_mask,
1800+
clear_mask);
1801+
} else {
1802+
rval = clear_ce_flags_1(istate,
1803+
istate->cache,
1804+
istate->cache_nr,
1805+
&prefix,
1806+
select_mask, clear_mask,
1807+
pl, 0, 0);
1808+
}
1809+
18111810
trace2_region_leave("unpack_trees", label, the_repository);
18121811

18131812
stop_progress(&istate->progress);

virtualfilesystem.c

Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -253,93 +253,133 @@ int is_excluded_from_virtualfilesystem(const char *pathname, int pathlen, int dt
253253
return -1;
254254
}
255255

256-
/*
257-
* Update the CE_SKIP_WORKTREE bits based on the virtual file system.
258-
*/
259-
void apply_virtualfilesystem(struct index_state *istate)
256+
struct apply_virtual_filesystem_stats {
257+
int nr_unknown;
258+
int nr_vfs_dirs;
259+
int nr_vfs_rows;
260+
int nr_bulk_skip;
261+
int nr_explicit_skip;
262+
};
263+
264+
static void clear_ce_flags_virtualfilesystem_1(struct index_state *istate, int select_mask, int clear_mask,
265+
struct apply_virtual_filesystem_stats *stats)
260266
{
261267
char *buf, *entry;
262268
int i;
263-
int nr_unknown = 0;
264-
int nr_vfs_dirs = 0;
265-
int nr_vfs_rows = 0;
266-
int nr_bulk_skip = 0;
267-
int nr_explicit_skip = 0;
268-
269-
if (!git_config_get_virtualfilesystem())
270-
return;
271-
272-
trace2_region_enter("vfs", "apply", the_repository);
273269

274270
if (!virtual_filesystem_data.len)
275271
get_virtual_filesystem_data(&virtual_filesystem_data);
276272

277-
/* set CE_SKIP_WORKTREE bit on all entries */
278-
for (i = 0; i < istate->cache_nr; i++)
279-
istate->cache[i]->ce_flags |= CE_SKIP_WORKTREE;
280-
281-
/* clear CE_SKIP_WORKTREE bit for everything in the virtual file system */
273+
/* clear specified flag bits for everything in the virtual file system */
282274
entry = buf = virtual_filesystem_data.buf;
283275
for (i = 0; i < virtual_filesystem_data.len; i++) {
284276
if (buf[i] == '\0') {
277+
struct cache_entry *ce;
285278
int pos, len;
286279

287-
nr_vfs_rows++;
280+
stats->nr_vfs_rows++;
288281

289282
len = buf + i - entry;
290283

291284
/* look for a directory wild card (ie "dir1/") */
292285
if (buf[i - 1] == '/') {
293-
nr_vfs_dirs++;
286+
stats->nr_vfs_dirs++;
294287
if (ignore_case)
295288
adjust_dirname_case(istate, entry);
296289
pos = index_name_pos(istate, entry, len);
297290
if (pos < 0) {
298-
pos = -pos - 1;
299-
while (pos < istate->cache_nr && !fspathncmp(istate->cache[pos]->name, entry, len)) {
300-
if (istate->cache[pos]->ce_flags & CE_SKIP_WORKTREE)
301-
nr_bulk_skip++;
302-
istate->cache[pos]->ce_flags &= ~CE_SKIP_WORKTREE;
303-
pos++;
291+
for (pos = -pos - 1; pos < istate->cache_nr; pos++) {
292+
ce = istate->cache[pos];
293+
if (fspathncmp(ce->name, entry, len))
294+
break;
295+
296+
if (select_mask && !(ce->ce_flags & select_mask))
297+
continue;
298+
299+
if (ce->ce_flags & clear_mask)
300+
stats->nr_bulk_skip++;
301+
ce->ce_flags &= ~clear_mask;
304302
}
305303
}
306304
} else {
307305
if (ignore_case) {
308-
struct cache_entry *ce = index_file_exists(istate, entry, len, ignore_case);
309-
if (ce) {
310-
if (ce->ce_flags & CE_SKIP_WORKTREE)
311-
nr_explicit_skip++;
312-
ce->ce_flags &= ~CE_SKIP_WORKTREE;
313-
}
314-
else {
315-
nr_unknown++;
316-
}
306+
ce = index_file_exists(istate, entry, len, ignore_case);
317307
} else {
318308
int pos = index_name_pos(istate, entry, len);
319-
if (pos >= 0) {
320-
if (istate->cache[pos]->ce_flags & CE_SKIP_WORKTREE)
321-
nr_explicit_skip++;
322-
istate->cache[pos]->ce_flags &= ~CE_SKIP_WORKTREE;
323-
}
324-
else {
325-
nr_unknown++;
326-
}
309+
310+
ce = NULL;
311+
if (pos >= 0)
312+
ce = istate->cache[pos];
313+
}
314+
315+
if (ce) {
316+
do {
317+
if (!select_mask || (ce->ce_flags & select_mask)) {
318+
if (ce->ce_flags & clear_mask)
319+
stats->nr_explicit_skip++;
320+
ce->ce_flags &= ~clear_mask;
321+
}
322+
323+
/*
324+
* There may be aliases with different cases of the same
325+
* name that also need to be modified.
326+
*/
327+
if (ignore_case)
328+
ce = index_file_next_match(istate, ce, ignore_case);
329+
else
330+
break;
331+
332+
} while (ce);
333+
} else {
334+
stats->nr_unknown++;
327335
}
328336
}
329337

330338
entry += len + 1;
331339
}
332340
}
341+
}
342+
343+
/*
344+
* Clear the specified flags for all entries in the virtual file system
345+
* that match the specified select mask. Returns the number of entries
346+
* processed.
347+
*/
348+
int clear_ce_flags_virtualfilesystem(struct index_state *istate, int select_mask, int clear_mask)
349+
{
350+
struct apply_virtual_filesystem_stats stats = {0};
351+
352+
clear_ce_flags_virtualfilesystem_1(istate, select_mask, clear_mask, &stats);
353+
return istate->cache_nr;
354+
}
355+
356+
/*
357+
* Update the CE_SKIP_WORKTREE bits based on the virtual file system.
358+
*/
359+
void apply_virtualfilesystem(struct index_state *istate)
360+
{
361+
int i;
362+
struct apply_virtual_filesystem_stats stats = {0};
363+
364+
if (!git_config_get_virtualfilesystem())
365+
return;
366+
367+
trace2_region_enter("vfs", "apply", the_repository);
368+
369+
/* set CE_SKIP_WORKTREE bit on all entries */
370+
for (i = 0; i < istate->cache_nr; i++)
371+
istate->cache[i]->ce_flags |= CE_SKIP_WORKTREE;
333372

334-
if (nr_vfs_rows > 0) {
335-
trace2_data_intmax("vfs", the_repository, "apply/tracked", nr_bulk_skip + nr_explicit_skip);
373+
clear_ce_flags_virtualfilesystem_1(istate, 0, CE_SKIP_WORKTREE, &stats);
374+
if (stats.nr_vfs_rows > 0) {
375+
trace2_data_intmax("vfs", the_repository, "apply/tracked", stats.nr_bulk_skip + stats.nr_explicit_skip);
336376

337-
trace2_data_intmax("vfs", the_repository, "apply/vfs_rows", nr_vfs_rows);
338-
trace2_data_intmax("vfs", the_repository, "apply/vfs_dirs", nr_vfs_dirs);
377+
trace2_data_intmax("vfs", the_repository, "apply/vfs_rows", stats.nr_vfs_rows);
378+
trace2_data_intmax("vfs", the_repository, "apply/vfs_dirs", stats.nr_vfs_dirs);
339379

340-
trace2_data_intmax("vfs", the_repository, "apply/nr_unknown", nr_unknown);
341-
trace2_data_intmax("vfs", the_repository, "apply/nr_bulk_skip", nr_bulk_skip);
342-
trace2_data_intmax("vfs", the_repository, "apply/nr_explicit_skip", nr_explicit_skip);
380+
trace2_data_intmax("vfs", the_repository, "apply/nr_unknown", stats.nr_unknown);
381+
trace2_data_intmax("vfs", the_repository, "apply/nr_bulk_skip", stats.nr_bulk_skip);
382+
trace2_data_intmax("vfs", the_repository, "apply/nr_explicit_skip", stats.nr_explicit_skip);
343383
}
344384

345385
trace2_region_leave("vfs", "apply", the_repository);

virtualfilesystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
*/
77
void apply_virtualfilesystem(struct index_state *istate);
88

9+
/*
10+
* Clear the specified flags for all entries in the virtual file system
11+
* that match the specified select mask. Returns the number of entries
12+
* processed.
13+
*/
14+
int clear_ce_flags_virtualfilesystem(struct index_state *istate, int select_mask, int clear_mask);
15+
916
/*
1017
* Return 1 if the requested item is found in the virtual file system,
1118
* 0 for not found and -1 for undecided.

0 commit comments

Comments
 (0)