Skip to content

Commit

Permalink
Merge pull request git-for-windows#429 from vdye/unpack-trees/fast-ca…
Browse files Browse the repository at this point in the history
…che-traversal

Improve `next_cache_entry` cache traversal performance
  • Loading branch information
vdye authored Sep 21, 2021
2 parents c8c6379 + c5fbbad commit f2bf729
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions t/perf/p2000-sparse-operations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ test_perf_on_all git commit -a -m A
test_perf_on_all git checkout -f -
test_perf_on_all git reset
test_perf_on_all git reset --hard
test_perf_on_all git reset -- does-not-exist
test_perf_on_all git read-tree -mu HEAD
test_perf_on_all git checkout-index -f --all
test_perf_on_all git update-index --add --remove
Expand Down
23 changes: 17 additions & 6 deletions unpack-trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,17 +665,24 @@ static void mark_ce_used_same_name(struct cache_entry *ce,
}
}

static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
static struct cache_entry *next_cache_entry(struct unpack_trees_options *o, int *hint)
{
const struct index_state *index = o->src_index;
int pos = o->cache_bottom;

if (*hint > pos)
pos = *hint;

while (pos < index->cache_nr) {
struct cache_entry *ce = index->cache[pos];
if (!(ce->ce_flags & CE_UNPACKED))
if (!(ce->ce_flags & CE_UNPACKED)) {
*hint = pos + 1;
return ce;
}
pos++;
}

*hint = pos;
return NULL;
}

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

/* Are we supposed to look at the index too? */
if (o->merge) {
int hint = -1;
while (1) {
int cmp;
struct cache_entry *ce;

if (o->diff_index_cached)
ce = next_cache_entry(o);
ce = next_cache_entry(o, &hint);
else
ce = find_cache_entry(info, p);

Expand Down Expand Up @@ -1720,7 +1728,7 @@ static int verify_absent(const struct cache_entry *,
int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
{
struct repository *repo = the_repository;
int i, ret;
int i, hint, ret;
static struct cache_entry *dfc;
struct pattern_list pl;
int free_pattern_list = 0;
Expand Down Expand Up @@ -1852,13 +1860,15 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
info.pathspec = o->pathspec;

if (o->prefix) {
hint = -1;

/*
* Unpack existing index entries that sort before the
* prefix the tree is spliced into. Note that o->merge
* is always true in this case.
*/
while (1) {
struct cache_entry *ce = next_cache_entry(o);
struct cache_entry *ce = next_cache_entry(o, &hint);
if (!ce)
break;
if (ce_in_traverse_path(ce, &info))
Expand All @@ -1879,8 +1889,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options

/* Any left-over entries in the index? */
if (o->merge) {
hint = -1;
while (1) {
struct cache_entry *ce = next_cache_entry(o);
struct cache_entry *ce = next_cache_entry(o, &hint);
if (!ce)
break;
if (unpack_index_entry(ce, o) < 0)
Expand Down

0 comments on commit f2bf729

Please sign in to comment.