Merge branch 'hf/unpack-trees-quadratic-scan' into next

The cache-scanning loop in 'next_cache_entry()' has been optimized
to avoid rescanning already-unpacked index entries, preventing a
quadratic performance slow-down when diffing the working tree
against a commit with a pathspec matching early index entries.

* hf/unpack-trees-quadratic-scan:
  unpack-trees: avoid quadratic index scan in next_cache_entry()
next
Junio C Hamano 2026-07-12 11:14:31 -07:00
commit 744f1aede4
2 changed files with 35 additions and 1 deletions

32
t/perf/p0009-diff-pathspec.sh Executable file
View File

@ -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

View 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;