Skip to content

Commit ca96eee

Browse files
tamirdgitster
authored andcommitted
ref-filter: memoize --contains with generations
git branch and git for-each-ref run a separate reachability walk for each ref considered by --contains and --no-contains. Refs with shared history therefore traverse the same commits repeatedly. git tag instead uses a depth-first walk that caches results across refs. That walk can perform poorly without generation numbers: a negative check may walk to the root instead of stopping at a nearby divergence. Generation numbers let it stop below the oldest target. Use the memoized walk for all ref-filter callers when generation numbers are available. Keep git tag on its existing path without generations. Caching still helps when many tags share deep history: ffc4b80 (tag: speed up --contains calculation, 2011-06-11) reduced git tag --contains HEAD~200 in linux-2.6 from 15.417 to 5.329 seconds. The new shared-history perf test improves from 0.72 to 0.03 seconds. In a repository with 62,174 remote-tracking refs, running: git branch -r --contains c78ae85f3ce7e improves from 104.365 seconds to 468 milliseconds. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5bd3978 commit ca96eee

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

commit-reach.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,8 @@ static enum contains_result contains_tag_algo(struct commit *candidate,
854854
int commit_contains(struct ref_filter *filter, struct commit *commit,
855855
struct commit_list *list, struct contains_cache *cache)
856856
{
857-
if (filter->with_commit_tag_algo)
857+
if (filter->with_commit_tag_algo ||
858+
generation_numbers_enabled(the_repository))
858859
return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
859860
return repo_is_descendant_of(the_repository, commit, list);
860861
}

t/perf/p1500-graph-walks.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ test_expect_success 'setup' '
3232
echo "X:$line" >>test-tool-tags || return 1
3333
done &&
3434
35-
commit=$(git commit-tree $(git rev-parse HEAD^{tree})) &&
35+
git rev-list --first-parent --max-count=8192 HEAD >contains-commits &&
36+
test_file_not_empty contains-commits &&
37+
git update-ref refs/contains-perf-base "$(tail -n 1 contains-commits)" &&
38+
awk "{
39+
printf \"update refs/contains-perf/%04d %s\\n\", NR, \$1
40+
}" contains-commits |
41+
git update-ref --stdin &&
42+
git pack-refs --include "refs/contains-perf/*" &&
43+
44+
commit=$(git commit-tree HEAD^{tree}) &&
3645
git update-ref refs/heads/disjoint-base $commit &&
3746
3847
git commit-graph write --reachable
@@ -62,6 +71,23 @@ test_perf 'contains: git tag --merged' '
6271
xargs git tag --merged=HEAD <tags
6372
'
6473

74+
test_perf 'contains: git for-each-ref' '
75+
git for-each-ref --contains=refs/contains-perf-base --stdin <refs
76+
'
77+
78+
test_perf 'contains: git branch' '
79+
xargs git branch --contains=refs/contains-perf-base <branches
80+
'
81+
82+
test_perf 'contains: git tag' '
83+
xargs git tag --contains=refs/contains-perf-base <tags
84+
'
85+
86+
test_perf 'contains: synthetic shared history' '
87+
git for-each-ref --contains=refs/contains-perf-base \
88+
refs/contains-perf/ >/dev/null
89+
'
90+
6591
test_perf 'is-base check: test-tool reach (refs)' '
6692
test-tool reach get_branch_base_for_tip <test-tool-refs
6793
'

0 commit comments

Comments
 (0)