]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit-reach: early exit paint_down_to_common for single merge-base
authorKristofer Karlsson <krka@spotify.com>
Mon, 11 May 2026 12:59:12 +0000 (12:59 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 May 2026 00:33:43 +0000 (09:33 +0900)
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>
builtin/merge-base.c
commit-reach.c
commit-reach.h
t/t6600-test-reach.sh

index 9b50b4660ea64ff8115df95093cd44ae8dc6a723..a87011c6cdab465349d02ec74d7ca3dca15e2752 100644 (file)
 
 static int show_merge_base(struct commit **rev, size_t rev_nr, int show_all)
 {
+       enum merge_base_flags flags = show_all ? MERGE_BASE_FIND_ALL : 0;
        struct commit_list *result = NULL, *r;
 
        if (repo_get_merge_bases_many_dirty(the_repository, rev[0],
                                            rev_nr - 1, rev + 1,
-                                           0, &result) < 0) {
+                                           flags, &result) < 0) {
                commit_list_free(result);
                return -1;
        }
index 766ba1156af63eb4fec06881278914076a081b6f..5a52be90a66f5a5b389f88cbc4cc35abf36ed244 100644 (file)
@@ -97,6 +97,14 @@ static int paint_down_to_common(struct repository *r,
                        if (!(commit->object.flags & RESULT)) {
                                commit->object.flags |= RESULT;
                                tail = commit_list_append(commit, tail);
+                               /*
+                                * The queue is generation-ordered; no
+                                * remaining common ancestor can be a
+                                * descendant of this one.
+                                */
+                               if (!(mb_flags & MERGE_BASE_FIND_ALL) &&
+                                   generation < GENERATION_NUMBER_INFINITY)
+                                       break;
                        }
                        /* Mark parents of a found merge stale */
                        flags |= STALE;
@@ -247,7 +255,8 @@ static int remove_redundant_no_gen(struct repository *r,
                                min_generation = curr_generation;
                }
                if (paint_down_to_common(r, array[i], filled,
-                                        work, min_generation, 0, &common)) {
+                                        work, min_generation,
+                                        MERGE_BASE_FIND_ALL, &common)) {
                        clear_commit_marks(array[i], all_flags);
                        clear_commit_marks_many(filled, work, all_flags);
                        commit_list_free(common);
@@ -477,7 +486,8 @@ int repo_get_merge_bases_many(struct repository *r,
                              struct commit **twos,
                              struct commit_list **result)
 {
-       return get_merge_bases_many_0(r, one, n, twos, 1, 0, result);
+       return get_merge_bases_many_0(r, one, n, twos, 1,
+                                    MERGE_BASE_FIND_ALL, result);
 }
 
 int repo_get_merge_bases_many_dirty(struct repository *r,
@@ -495,7 +505,8 @@ int repo_get_merge_bases(struct repository *r,
                         struct commit *two,
                         struct commit_list **result)
 {
-       return get_merge_bases_many_0(r, one, 1, &two, 1, 0, result);
+       return get_merge_bases_many_0(r, one, 1, &two, 1,
+                                    MERGE_BASE_FIND_ALL, result);
 }
 
 /*
@@ -540,7 +551,7 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
        struct commit_list *bases = NULL;
        int ret = 0, i;
        timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
-       enum merge_base_flags mb_flags = 0;
+       enum merge_base_flags mb_flags = MERGE_BASE_FIND_ALL;
 
        if (ignore_missing_commits)
                mb_flags |= MERGE_BASE_IGNORE_MISSING_COMMITS;
index a3f2cd80ebf180cb57d7331643dfe61b8fcc33d3..3f3a563d8a5dd11cc928beee481e914f20cd3aea 100644 (file)
@@ -19,9 +19,14 @@ int repo_get_merge_bases_many(struct repository *r,
                              struct commit_list **result);
 enum merge_base_flags {
        MERGE_BASE_IGNORE_MISSING_COMMITS = (1 << 0),
+       MERGE_BASE_FIND_ALL               = (1 << 1),
 };
 
-/* To be used only when object flags after this call no longer matter */
+/*
+ * To be used only when object flags after this call no longer matter.
+ * Without MERGE_BASE_FIND_ALL and with generation numbers available,
+ * returns after finding the first merge-base, skipping the STALE drain.
+ */
 int repo_get_merge_bases_many_dirty(struct repository *r,
                                    struct commit *one, size_t n,
                                    struct commit **twos,
index dc0421ed2f3726769a337a8f47be1e69b850983c..51c23b76833126d697d8c3ecde0e8b8ed9ebe8be 100755 (executable)
@@ -882,4 +882,44 @@ test_expect_success 'rev-list --maximal-only matches merge-base --independent' '
        test_cmp expect.sorted actual.sorted
 '
 
+# The following tests verify the early-exit optimisation in
+# paint_down_to_common when merge-base is invoked without --all.
+# Each test checks all four commit-graph configurations.
+
+merge_base_all_modes () {
+       test_when_finished rm -rf .git/objects/info/commit-graph &&
+       git merge-base "$@" >actual &&
+       test_cmp expect actual &&
+       cp commit-graph-full .git/objects/info/commit-graph &&
+       git merge-base "$@" >actual &&
+       test_cmp expect actual &&
+       cp commit-graph-half .git/objects/info/commit-graph &&
+       git merge-base "$@" >actual &&
+       test_cmp expect actual &&
+       cp commit-graph-no-gdat .git/objects/info/commit-graph &&
+       git merge-base "$@" >actual &&
+       test_cmp expect actual
+}
+
+test_expect_success 'merge-base without --all (unique base)' '
+       git rev-parse commit-5-3 >expect &&
+       merge_base_all_modes commit-5-7 commit-8-3
+'
+
+test_expect_success 'merge-base without --all is one of --all results' '
+       test_when_finished rm -rf .git/objects/info/commit-graph &&
+
+       cp commit-graph-full .git/objects/info/commit-graph &&
+       git merge-base --all commit-5-7 commit-4-8 commit-6-6 commit-8-3 >all &&
+       git merge-base commit-5-7 commit-4-8 commit-6-6 commit-8-3 >single &&
+       test_line_count = 1 single &&
+       grep -F -f single all &&
+
+       cp commit-graph-half .git/objects/info/commit-graph &&
+       git merge-base --all commit-5-7 commit-4-8 commit-6-6 commit-8-3 >all &&
+       git merge-base commit-5-7 commit-4-8 commit-6-6 commit-8-3 >single &&
+       test_line_count = 1 single &&
+       grep -F -f single all
+'
+
 test_done