Skip to content

Commit 80b8ada

Browse files
derrickstoleegitster
authored andcommitted
commit-reach: use fast logic in repo_in_merge_base
The repo_is_descendant_of() method is aware of the existence of the commit-graph file. It checks for generation_numbers_enabled() before deciding on using can_all_from_reach() or repo_in_merge_bases() depending on the situation. The reason here is that can_all_from_reach() uses a depth-first search that is limited by the minimum generation number of the target commits, and that algorithm can be very slow when generation numbers are not present. The alternative uses paint_down_to_common() which will walk the entire merge-base boundary, which is typically slower. This method is used by commands like "git tag --contains" and "git branch --contains" for very fast results when a commit-graph file exists. Unfortunately, it is _not_ used in commands like "git merge-base --is-ancestor" which is doing an even simpler request. This issue was raised recently [1] with respect to a change to how generation numbers are stored, but was also reported much earlier [2] before commit-reach.c existed to simplify these reachability queries. [1] https://lore.kernel.org/git/[email protected]/ [2] https://lore.kernel.org/git/[email protected]/ The root cause is that builtin/merge-base.c has a method handle_is_ancestor() that calls in_merge_bases(), an older version of repo_in_merge_bases(). It would be better if we have every caller to in_merge_bases() use the logic in can_all_from_reach() when possible. This is where things get a little tricky: repo_is_descendant_of() calls repo_in_merge_bases() in the non-generation numbers enabled case! If we simply update repo_in_merge_bases() to call repo_is_descendant_of() instead of repo_in_merge_bases_many(), then we will get a recursive call loop. Thankfully, this is caught by the test suite in the default mode (i.e. GIT_TEST_COMMIT_GRAPH=0). The trick, then, is to make the non-generation number case for repo_is_descendant_of() call repo_in_merge_bases_many() directly, skipping the non-_many version. This allows us to take advantage of this faster code path, when possible. The easiest way to measure the performance impact is to test the following command on the Linux kernel repository: git merge-base --is-ancestor <A> <B> | A | B | Time Before | Time After | |------|------|-------------|------------| | v3.0 | v5.7 | 0.459s | 0.028s | | v4.0 | v5.7 | 0.267s | 0.021s | | v5.0 | v5.7 | 0.074s | 0.013s | Note that each of these samples return success. The old code performed the same operation when <A> and <B> are swapped. However, can_all_from_reach() will return immediately if the generation numbers show that <A> has larger generation number than <B>. Thus, the time for the swapped case is universally 0.004s in each case. Reported-by: Ævar Arnfjörð Bjarmason <[email protected]> Reported-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d91d6fb commit 80b8ada

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

commit-reach.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static int repo_is_descendant_of(struct repository *r,
303303

304304
other = with_commit->item;
305305
with_commit = with_commit->next;
306-
if (repo_in_merge_bases(r, other, commit))
306+
if (repo_in_merge_bases_many(r, other, 1, &commit))
307307
return 1;
308308
}
309309
return 0;
@@ -355,7 +355,15 @@ int repo_in_merge_bases(struct repository *r,
355355
struct commit *commit,
356356
struct commit *reference)
357357
{
358-
return repo_in_merge_bases_many(r, commit, 1, &reference);
358+
int res;
359+
struct commit_list *list = NULL;
360+
struct commit_list **next = &list;
361+
362+
next = commit_list_append(commit, next);
363+
res = repo_is_descendant_of(r, reference, list);
364+
free_commit_list(list);
365+
366+
return res;
359367
}
360368

361369
struct commit_list *reduce_heads(struct commit_list *heads)

0 commit comments

Comments
 (0)