Merge branch 'kk/paint-down-to-common-optim'

"git merge-base" optimization.

* kk/paint-down-to-common-optim:
  commit-reach: early exit paint_down_to_common for single merge-base
  commit-reach: introduce merge_base_flags enum
This commit is contained in:
Junio C Hamano
2026-05-25 09:40:07 +09:00
4 changed files with 81 additions and 11 deletions
+3 -1
View File
@@ -11,10 +11,12 @@
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, &result) < 0) {
rev_nr - 1, rev + 1,
flags, &result) < 0) {
commit_list_free(result);
return -1;
}
+27 -9
View File
@@ -54,7 +54,7 @@ static int paint_down_to_common(struct repository *r,
struct commit *one, int n,
struct commit **twos,
timestamp_t min_generation,
int ignore_missing_commits,
enum merge_base_flags mb_flags,
struct commit_list **result)
{
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
@@ -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;
@@ -118,7 +126,7 @@ static int paint_down_to_common(struct repository *r,
* corrupt commits would already have been
* dispatched with a `die()`.
*/
if (ignore_missing_commits)
if (mb_flags & MERGE_BASE_IGNORE_MISSING_COMMITS)
return 0;
return error(_("could not parse commit %s"),
oid_to_hex(&p->object.oid));
@@ -136,6 +144,7 @@ static int paint_down_to_common(struct repository *r,
static int merge_bases_many(struct repository *r,
struct commit *one, int n,
struct commit **twos,
enum merge_base_flags mb_flags,
struct commit_list **result)
{
struct commit_list *list = NULL, **tail = result;
@@ -165,7 +174,7 @@ static int merge_bases_many(struct repository *r,
oid_to_hex(&twos[i]->object.oid));
}
if (paint_down_to_common(r, one, n, twos, 0, 0, &list)) {
if (paint_down_to_common(r, one, n, twos, 0, mb_flags, &list)) {
commit_list_free(list);
return -1;
}
@@ -246,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);
@@ -425,6 +435,7 @@ static int get_merge_bases_many_0(struct repository *r,
size_t n,
struct commit **twos,
int cleanup,
enum merge_base_flags mb_flags,
struct commit_list **result)
{
struct commit_list *list, **tail = result;
@@ -432,7 +443,7 @@ static int get_merge_bases_many_0(struct repository *r,
size_t cnt, i;
int ret;
if (merge_bases_many(r, one, n, twos, result) < 0)
if (merge_bases_many(r, one, n, twos, mb_flags, result) < 0)
return -1;
for (i = 0; i < n; i++) {
if (one == twos[i])
@@ -475,16 +486,18 @@ 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, 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,
struct commit *one,
size_t n,
struct commit **twos,
enum merge_base_flags mb_flags,
struct commit_list **result)
{
return get_merge_bases_many_0(r, one, n, twos, 0, result);
return get_merge_bases_many_0(r, one, n, twos, 0, mb_flags, result);
}
int repo_get_merge_bases(struct repository *r,
@@ -492,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, result);
return get_merge_bases_many_0(r, one, 1, &two, 1,
MERGE_BASE_FIND_ALL, result);
}
/*
@@ -537,6 +551,10 @@ 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 = MERGE_BASE_FIND_ALL;
if (ignore_missing_commits)
mb_flags |= MERGE_BASE_IGNORE_MISSING_COMMITS;
if (repo_parse_commit(r, commit))
return ignore_missing_commits ? 0 : -1;
@@ -555,7 +573,7 @@ int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
if (paint_down_to_common(r, commit,
nr_reference, reference,
generation, ignore_missing_commits, &bases))
generation, mb_flags, &bases))
ret = -1;
else if (commit->object.flags & PARENT2)
ret = 1;
+11 -1
View File
@@ -17,10 +17,20 @@ int repo_get_merge_bases_many(struct repository *r,
struct commit *one, size_t n,
struct commit **twos,
struct commit_list **result);
/* To be used only when object flags after this call no longer matter */
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.
* 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,
enum merge_base_flags mb_flags,
struct commit_list **result);
int get_octopus_merge_bases(struct commit_list *in, struct commit_list **result);
+40
View File
@@ -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