commit-reach: early exit paint_down_to_common for single merge-base

Commits not in the commit-graph get GENERATION_NUMBER_INFINITY and sort to the top of the priority queue. After those, commits with finite generation numbers are popped in non-increasing order. When MERGE_BASE_FIND_ALL is not set the first doubly-painted commit with a finite generation is therefore a best merge-base: no commit still in the queue can be a descendant of it. Skip the expensive STALE drain in this case. Add MERGE_BASE_FIND_ALL to the merge_base_flags enum. Callers that need every merge-base (repo_get_merge_bases_many, repo_get_merge_bases, repo_in_merge_bases_many, remove_redundant_no_gen) pass the flag to preserve existing behavior. git merge-base (without --all) passes 0, triggering the early exit. On a 2.2M-commit merge-heavy monorepo with commit-graph: HEAD vs ~500: 5,229ms -> 24ms HEAD vs ~1000: 4,214ms -> 39ms HEAD vs ~5000: 3,799ms -> 46ms HEAD vs ~10000: 3,827ms -> 61ms Signed-off-by: Kristofer Karlsson <krka@spotify.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristofer Karlsson committed May 11, 2026 at 12:59 UTC 93e5b1680e30650ceca889a56429a055f17033bd
4 files changed +63 -6
builtin/merge-base.c
+2 -1
@@ -11,11 +11,12 @@
11
12 static int show_merge_base(struct commit **rev, size_t rev_nr, int show_all)
13 {
14 + enum merge_base_flags flags = show_all ? MERGE_BASE_FIND_ALL : 0;
15 struct commit_list *result = NULL, *r;
16
17 if (repo_get_merge_bases_many_dirty(the_repository, rev[0],
18 rev_nr - 1, rev + 1,
18 - 0, &result) < 0) {
19 + flags, &result) < 0) {
20 commit_list_free(result);
21 return -1;
22 }
commit-reach.c
+15 -4
@@ -97,6 +97,14 @@ static int paint_down_to_common(struct repository *r,
97 if (!(commit->object.flags & RESULT)) {
98 commit->object.flags |= RESULT;
99 tail = commit_list_append(commit, tail);
100 + /*
101 + * The queue is generation-ordered; no
102 + * remaining common ancestor can be a
103 + * descendant of this one.
104 + */
105 + if (!(mb_flags & MERGE_BASE_FIND_ALL) &&
106 + generation < GENERATION_NUMBER_INFINITY)
107 + break;
108 }
109 /* Mark parents of a found merge stale */
110 flags |= STALE;
@@ -247,7 +255,8 @@ static int remove_redundant_no_gen(struct repository *r,
255 min_generation = curr_generation;
256 }
257 if (paint_down_to_common(r, array[i], filled,
250 - work, min_generation, 0, &common)) {
258 + work, min_generation,
259 + MERGE_BASE_FIND_ALL, &common)) {
260 clear_commit_marks(array[i], all_flags);
261 clear_commit_marks_many(filled, work, all_flags);
262 commit_list_free(common);
@@ -477,7 +486,8 @@ int repo_get_merge_bases_many(struct repository *r,
486 struct commit **twos,
487 struct commit_list **result)
488 {
480 - return get_merge_bases_many_0(r, one, n, twos, 1, 0, result);
489 + return get_merge_bases_many_0(r, one, n, twos, 1,
490 + MERGE_BASE_FIND_ALL, result);
491 }
492
493 int repo_get_merge_bases_many_dirty(struct repository *r,
@@ -495,7 +505,8 @@ int repo_get_merge_bases(struct repository *r,
505 struct commit *two,
506 struct commit_list **result)
507 {
498 - return get_merge_bases_many_0(r, one, 1, &two, 1, 0, result);
508 + return get_merge_bases_many_0(r, one, 1, &two, 1,
509 + MERGE_BASE_FIND_ALL, result);
510 }
511
512 /*
@@ -540,7 +551,7 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
551 struct commit_list *bases = NULL;
552 int ret = 0, i;
553 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
543 - enum merge_base_flags mb_flags = 0;
554 + enum merge_base_flags mb_flags = MERGE_BASE_FIND_ALL;
555
556 if (ignore_missing_commits)
557 mb_flags |= MERGE_BASE_IGNORE_MISSING_COMMITS;
commit-reach.h
+6 -1
@@ -19,9 +19,14 @@ int repo_get_merge_bases_many(struct repository *r,
19 struct commit_list **result);
20 enum merge_base_flags {
21 MERGE_BASE_IGNORE_MISSING_COMMITS = (1 << 0),
22 + MERGE_BASE_FIND_ALL = (1 << 1),
23 };
24
24 -/* To be used only when object flags after this call no longer matter */
25 +/*
26 + * To be used only when object flags after this call no longer matter.
27 + * Without MERGE_BASE_FIND_ALL and with generation numbers available,
28 + * returns after finding the first merge-base, skipping the STALE drain.
29 + */
30 int repo_get_merge_bases_many_dirty(struct repository *r,
31 struct commit *one, size_t n,
32 struct commit **twos,
t/t6600-test-reach.sh
+40
@@ -882,4 +882,44 @@ test_expect_success 'rev-list --maximal-only matches merge-base --independent' '
882 test_cmp expect.sorted actual.sorted
883 '
884
885 +# The following tests verify the early-exit optimisation in
886 +# paint_down_to_common when merge-base is invoked without --all.
887 +# Each test checks all four commit-graph configurations.
888 +
889 +merge_base_all_modes () {
890 + test_when_finished rm -rf .git/objects/info/commit-graph &&
891 + git merge-base "$@" >actual &&
892 + test_cmp expect actual &&
893 + cp commit-graph-full .git/objects/info/commit-graph &&
894 + git merge-base "$@" >actual &&
895 + test_cmp expect actual &&
896 + cp commit-graph-half .git/objects/info/commit-graph &&
897 + git merge-base "$@" >actual &&
898 + test_cmp expect actual &&
899 + cp commit-graph-no-gdat .git/objects/info/commit-graph &&
900 + git merge-base "$@" >actual &&
901 + test_cmp expect actual
902 +}
903 +
904 +test_expect_success 'merge-base without --all (unique base)' '
905 + git rev-parse commit-5-3 >expect &&
906 + merge_base_all_modes commit-5-7 commit-8-3
907 +'
908 +
909 +test_expect_success 'merge-base without --all is one of --all results' '
910 + test_when_finished rm -rf .git/objects/info/commit-graph &&
911 +
912 + cp commit-graph-full .git/objects/info/commit-graph &&
913 + git merge-base --all commit-5-7 commit-4-8 commit-6-6 commit-8-3 >all &&
914 + git merge-base commit-5-7 commit-4-8 commit-6-6 commit-8-3 >single &&
915 + test_line_count = 1 single &&
916 + grep -F -f single all &&
917 +
918 + cp commit-graph-half .git/objects/info/commit-graph &&
919 + git merge-base --all commit-5-7 commit-4-8 commit-6-6 commit-8-3 >all &&
920 + git merge-base commit-5-7 commit-4-8 commit-6-6 commit-8-3 >single &&
921 + test_line_count = 1 single &&
922 + grep -F -f single all
923 +'
924 +
925 test_done