Skip to content

Commit 738762b

Browse files
vdyederrickstolee
authored andcommitted
Merge pull request #429 from vdye/unpack-trees/fast-cache-traversal
Improve `next_cache_entry` cache traversal performance
2 parents 9a04102 + ff39fef commit 738762b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

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)