]> git.ipfire.org Git - thirdparty/git.git/commitdiff
unpack-trees: avoid quadratic index scan in next_cache_entry()
authorHenrique Ferreiro <hferreiro@igalia.com>
Wed, 8 Jul 2026 21:42:13 +0000 (21:42 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 8 Jul 2026 21:54:21 +0000 (14:54 -0700)
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>
t/perf/p0009-diff-pathspec.sh [new file with mode: 0755]
unpack-trees.c

diff --git a/t/perf/p0009-diff-pathspec.sh b/t/perf/p0009-diff-pathspec.sh
new file mode 100755 (executable)
index 0000000..6079db5
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='Tests performance of diffing the working tree with a pathspec'
+
+. ./perf-lib.sh
+
+test_perf_fresh_repo
+
+count=10000
+if test_have_prereq EXPENSIVE
+then
+       count=100000
+fi
+
+# The entries exist only in the index, which is enough to
+# exercise the index scan.
+test_expect_success 'setup' '
+       blob=$(echo content | git hash-object -w --stdin) &&
+       {
+               printf "100644 $blob\taaa/file\n" &&
+               printf "100644 $blob\tf%s\n" $(test_seq $count)
+       } | git update-index --index-info &&
+       git commit -q -m initial &&
+       mkdir -p aaa &&
+       echo content >aaa/file
+'
+
+test_perf 'diff pathspec subtree' '
+       git diff HEAD -- aaa/file
+'
+
+test_done
index f38c761ab987a6647ffd414253c81e608d9c67a7..4ecc07e4a1304b8657b7fcf6490743724c84ef03 100644 (file)
@@ -671,8 +671,10 @@ static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
 
        while (pos < index->cache_nr) {
                struct cache_entry *ce = index->cache[pos];
-               if (!(ce->ce_flags & CE_UNPACKED))
+               if (!(ce->ce_flags & CE_UNPACKED)) {
+                       o->internal.cache_bottom = pos;
                        return ce;
+               }
                pos++;
        }
        return NULL;