Skip to content

Commit f848739

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 dc703e1 + 59476ce commit f848739

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
@@ -746,6 +746,26 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
746746
return NULL;
747747
}
748748

749+
struct cache_entry *index_file_next_match(struct index_state *istate, struct cache_entry *ce, int igncase)
750+
{
751+
struct cache_entry *next;
752+
753+
if (!igncase || !ce) {
754+
return NULL;
755+
}
756+
757+
next = hashmap_get_next_entry(&istate->name_hash, ce, ent);
758+
if (!next)
759+
return NULL;
760+
761+
hashmap_for_each_entry_from(&istate->name_hash, next, ent) {
762+
if (same_name(next, ce->name, ce_namelen(ce), igncase))
763+
return next;
764+
}
765+
766+
return NULL;
767+
}
768+
749769
void free_name_hash(struct index_state *istate)
750770
{
751771
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
@@ -1721,14 +1721,6 @@ static int clear_ce_flags_1(struct index_state *istate,
17211721
continue;
17221722
}
17231723

1724-
/* if it's not in the virtual file system, exit early */
1725-
if (core_virtualfilesystem) {
1726-
if (is_included_in_virtualfilesystem(ce->name, ce->ce_namelen) > 0)
1727-
ce->ce_flags &= ~clear_mask;
1728-
cache++;
1729-
continue;
1730-
}
1731-
17321724
if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
17331725
break;
17341726

@@ -1805,12 +1797,19 @@ static int clear_ce_flags(struct index_state *istate,
18051797
xsnprintf(label, sizeof(label), "clear_ce_flags/0x%08lx_0x%08lx",
18061798
(unsigned long)select_mask, (unsigned long)clear_mask);
18071799
trace2_region_enter("unpack_trees", label, the_repository);
1808-
rval = clear_ce_flags_1(istate,
1809-
istate->cache,
1810-
istate->cache_nr,
1811-
&prefix,
1812-
select_mask, clear_mask,
1813-
pl, 0, 0);
1800+
if (core_virtualfilesystem) {
1801+
rval = clear_ce_flags_virtualfilesystem(istate,
1802+
select_mask,
1803+
clear_mask);
1804+
} else {
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);
1811+
}
1812+
18141813
trace2_region_leave("unpack_trees", label, the_repository);
18151814

18161815
stop_progress(&istate->progress);

virtualfilesystem.c

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

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

275271
if (!virtual_filesystem_data.len)
276272
get_virtual_filesystem_data(istate->repo, &virtual_filesystem_data);
277273

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

288-
nr_vfs_rows++;
281+
stats->nr_vfs_rows++;
289282

290283
len = buf + i - entry;
291284

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

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

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

338-
trace2_data_intmax("vfs", the_repository, "apply/vfs_rows", nr_vfs_rows);
339-
trace2_data_intmax("vfs", the_repository, "apply/vfs_dirs", nr_vfs_dirs);
378+
trace2_data_intmax("vfs", the_repository, "apply/vfs_rows", stats.nr_vfs_rows);
379+
trace2_data_intmax("vfs", the_repository, "apply/vfs_dirs", stats.nr_vfs_dirs);
340380

341-
trace2_data_intmax("vfs", the_repository, "apply/nr_unknown", nr_unknown);
342-
trace2_data_intmax("vfs", the_repository, "apply/nr_bulk_skip", nr_bulk_skip);
343-
trace2_data_intmax("vfs", the_repository, "apply/nr_explicit_skip", nr_explicit_skip);
381+
trace2_data_intmax("vfs", the_repository, "apply/nr_unknown", stats.nr_unknown);
382+
trace2_data_intmax("vfs", the_repository, "apply/nr_bulk_skip", stats.nr_bulk_skip);
383+
trace2_data_intmax("vfs", the_repository, "apply/nr_explicit_skip", stats.nr_explicit_skip);
344384
}
345385

346386
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)