Skip to content

Commit c6eb2a5

Browse files
vdyederrickstolee
authored andcommitted
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 <vdye@github.com>
1 parent c16a22b commit c6eb2a5

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

unpack-trees.c

Lines changed: 17 additions & 6 deletions
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

@@ -1385,12 +1392,13 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
13851392

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

13921400
if (o->diff_index_cached)
1393-
ce = next_cache_entry(o);
1401+
ce = next_cache_entry(o, &hint);
13941402
else
13951403
ce = find_cache_entry(info, p);
13961404

@@ -1719,7 +1727,7 @@ static int verify_absent(const struct cache_entry *,
17191727
int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
17201728
{
17211729
struct repository *repo = the_repository;
1722-
int i, ret;
1730+
int i, hint, ret;
17231731
static struct cache_entry *dfc;
17241732
struct pattern_list pl;
17251733
int free_pattern_list = 0;
@@ -1868,13 +1876,15 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
18681876
info.pathspec = o->pathspec;
18691877

18701878
if (o->prefix) {
1879+
hint = -1;
1880+
18711881
/*
18721882
* Unpack existing index entries that sort before the
18731883
* prefix the tree is spliced into. Note that o->merge
18741884
* is always true in this case.
18751885
*/
18761886
while (1) {
1877-
struct cache_entry *ce = next_cache_entry(o);
1887+
struct cache_entry *ce = next_cache_entry(o, &hint);
18781888
if (!ce)
18791889
break;
18801890
if (ce_in_traverse_path(ce, &info))
@@ -1895,8 +1905,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
18951905

18961906
/* Any left-over entries in the index? */
18971907
if (o->merge) {
1908+
hint = -1;
18981909
while (1) {
1899-
struct cache_entry *ce = next_cache_entry(o);
1910+
struct cache_entry *ce = next_cache_entry(o, &hint);
19001911
if (!ce)
19011912
break;
19021913
if (unpack_index_entry(ce, o) < 0)

0 commit comments

Comments
 (0)