Skip to content

Commit c8f1ca3

Browse files
committed
unpack-trees: avoid quadratic index scan in next_cache_entry()
Diffing the working tree against a commit with a pathspec can take time quadratic in the size of the index when the pathspec matches a subtree whose entries are the first entries of the index. Fix it by having next_cache_entry() record how far it scanned in cache_bottom, so repeated calls no longer rescan the growing prefix of already-unpacked entries. On a Chromium checkout (~500k index entries), git diff HEAD -- .agents/OWNERS took about 8 minutes before this change and 0.07 seconds after it. The same diff without the commit, without the pathspec, or with --cached was already instant. Add p0009-diff-pathspec.sh, which builds a 10,000-entry index whose first path lives in a subtree (100,000 entries under --long-tests), to guard against the regression. Comparing v2.55.0 with this change using GIT_TEST_LONG=t: Test v2.55.0 HEAD ------------------------------------------------------------------------ 0009.2: diff pathspec subtree 7.16(7.12+0.01) 0.02(0.01+0.00) -99.7% Signed-off-by: Henrique Ferreiro <hferreiro@igalia.com>
1 parent e9019fc commit c8f1ca3

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

t/perf/p0009-diff-pathspec.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
test_description='Tests performance of diffing the working tree with a pathspec'
4+
5+
. ./perf-lib.sh
6+
7+
test_perf_fresh_repo
8+
9+
count=10000
10+
if test_have_prereq EXPENSIVE
11+
then
12+
count=100000
13+
fi
14+
15+
# The entries exist only in the index, which is enough to
16+
# exercise the index scan.
17+
test_expect_success 'setup' '
18+
blob=$(echo content | git hash-object -w --stdin) &&
19+
{
20+
printf "100644 $blob\taaa/file\n" &&
21+
printf "100644 $blob\tf%s\n" $(test_seq $count)
22+
} | git update-index --index-info &&
23+
git commit -q -m initial &&
24+
mkdir -p aaa &&
25+
echo content >aaa/file
26+
'
27+
28+
test_perf 'diff pathspec subtree' '
29+
git diff HEAD -- aaa/file
30+
'
31+
32+
test_done

unpack-trees.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,10 @@ static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
671671

672672
while (pos < index->cache_nr) {
673673
struct cache_entry *ce = index->cache[pos];
674-
if (!(ce->ce_flags & CE_UNPACKED))
674+
if (!(ce->ce_flags & CE_UNPACKED)) {
675+
o->internal.cache_bottom = pos;
675676
return ce;
677+
}
676678
pos++;
677679
}
678680
return NULL;

0 commit comments

Comments
 (0)