Skip to content

Commit cc1aaf0

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 100,000-entry index whose first path lives in a subtree, to guard against the regression. Comparing v2.55.0 with this change: 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 cc1aaf0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

t/perf/p0009-diff-pathspec.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
# The entries exist only in the index, which is enough to
10+
# exercise the index scan.
11+
test_expect_success 'setup' '
12+
count=100000 &&
13+
blob=$(echo content | git hash-object -w --stdin) &&
14+
{
15+
printf "100644 $blob\taaa/file\n" &&
16+
printf "100644 $blob\tf%s\n" $(test_seq $count)
17+
} | git update-index --index-info &&
18+
git commit -q -m initial &&
19+
mkdir -p aaa &&
20+
echo content >aaa/file
21+
'
22+
23+
test_perf 'diff pathspec subtree' '
24+
git diff HEAD -- aaa/file
25+
'
26+
27+
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)