Skip to content

Commit f2bf729

Browse files
authored
Merge pull request git-for-windows#429 from vdye/unpack-trees/fast-cache-traversal
Improve `next_cache_entry` cache traversal performance
2 parents c8c6379 + c5fbbad commit f2bf729

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

t/perf/p2000-sparse-operations.sh

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ test_perf_on_all git commit -a -m A
113113
test_perf_on_all git checkout -f -
114114
test_perf_on_all git reset
115115
test_perf_on_all git reset --hard
116+
test_perf_on_all git reset -- does-not-exist
116117
test_perf_on_all git read-tree -mu HEAD
117118
test_perf_on_all git checkout-index -f --all
118119
test_perf_on_all git update-index --add --remove

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)