Skip to content

Commit c5fbbad

Browse files
committed
unpack-trees: improve performance of next_cache_entry
To find the first non-unpacked cache entry, `next_cache_entry` iterates through index, starting at `cache_bottom`, to find the first cache entry. The performance of this in full indexes is helped by `cache_bottom` advancing with each invocation of `mark_ce_used` (called by `unpack_index_entry`). However, the presence of sparse directories can prevent the `cache_bottom` from advancing in a sparse index case, effectively forcing `next_cache_entry` to search from the beginning of the index each time it is called. The need to preserve `cache_bottom` for the sparse index is documented in 17a1bb5, so to get the benefit it provides in "shortcutting" already-searched entries a separate `hint` position is used. The hint position is set inside of `next_cache_entry` to `last_searched_position + 1`, allowing full _and_ sparse index iterations to skip already-searched entries. The performance is significantly improved for the sparse index case based on the `p2000` results for a `git reset` with a non-matching pathspec (heavily using `next_cache_entry`): Test ms/vfs-2.33.0 HEAD ------------------------------------------------------ (full-v3) 0.79(0.38+0.30) 0.91(0.43+0.34) +15.2% (full-v4) 0.80(0.38+0.29) 0.85(0.40+0.35) +6.2% (sparse-v3) 0.76(0.43+0.69) 0.44(0.08+0.67) -42.1% (sparse-v4) 0.71(0.40+0.65) 0.41(0.09+0.65) -42.3% Signed-off-by: Victoria Dye <[email protected]>
1 parent aed0843 commit c5fbbad

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

unpack-trees.c

+17-6
Original file line numberDiff line numberDiff line change
@@ -665,17 +665,24 @@ static void mark_ce_used_same_name(struct cache_entry *ce,
665665
}
666666
}
667667

668-
static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
668+
static struct cache_entry *next_cache_entry(struct unpack_trees_options *o, int *hint)
669669
{
670670
const struct index_state *index = o->src_index;
671671
int pos = o->cache_bottom;
672672

673+
if (*hint > pos)
674+
pos = *hint;
675+
673676
while (pos < index->cache_nr) {
674677
struct cache_entry *ce = index->cache[pos];
675-
if (!(ce->ce_flags & CE_UNPACKED))
678+
if (!(ce->ce_flags & CE_UNPACKED)) {
679+
*hint = pos + 1;
676680
return ce;
681+
}
677682
pos++;
678683
}
684+
685+
*hint = pos;
679686
return NULL;
680687
}
681688

@@ -1386,12 +1393,13 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
13861393

13871394
/* Are we supposed to look at the index too? */
13881395
if (o->merge) {
1396+
int hint = -1;
13891397
while (1) {
13901398
int cmp;
13911399
struct cache_entry *ce;
13921400

13931401
if (o->diff_index_cached)
1394-
ce = next_cache_entry(o);
1402+
ce = next_cache_entry(o, &hint);
13951403
else
13961404
ce = find_cache_entry(info, p);
13971405

@@ -1720,7 +1728,7 @@ static int verify_absent(const struct cache_entry *,
17201728
int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
17211729
{
17221730
struct repository *repo = the_repository;
1723-
int i, ret;
1731+
int i, hint, ret;
17241732
static struct cache_entry *dfc;
17251733
struct pattern_list pl;
17261734
int free_pattern_list = 0;
@@ -1852,13 +1860,15 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
18521860
info.pathspec = o->pathspec;
18531861

18541862
if (o->prefix) {
1863+
hint = -1;
1864+
18551865
/*
18561866
* Unpack existing index entries that sort before the
18571867
* prefix the tree is spliced into. Note that o->merge
18581868
* is always true in this case.
18591869
*/
18601870
while (1) {
1861-
struct cache_entry *ce = next_cache_entry(o);
1871+
struct cache_entry *ce = next_cache_entry(o, &hint);
18621872
if (!ce)
18631873
break;
18641874
if (ce_in_traverse_path(ce, &info))
@@ -1879,8 +1889,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
18791889

18801890
/* Any left-over entries in the index? */
18811891
if (o->merge) {
1892+
hint = -1;
18821893
while (1) {
1883-
struct cache_entry *ce = next_cache_entry(o);
1894+
struct cache_entry *ce = next_cache_entry(o, &hint);
18841895
if (!ce)
18851896
break;
18861897
if (unpack_index_entry(ce, o) < 0)

0 commit comments

Comments
 (0)