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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Henrique Ferreiro committed Jul 8, 2026 at 21:42 UTC 716766765c945c9ca62f24f6debc3f255ff3b6a6
2 files changed +35 -1
t/perf/p0009-diff-pathspec.sh new
+32
@@ -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
+3 -1
@@ -671,8 +671,10 @@ static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
671
672 while (pos < index->cache_nr) {
673 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;
676 return ce;
677 + }
678 pos++;
679 }
680 return NULL;