commit: use generation numbers for in_merge_bases()

The containment algorithm for 'git branch --contains' is different from that for 'git tag --contains' in that it uses is_descendant_of() instead of contains_tag_algo(). The expensive portion of the branch algorithm is computing merge bases. When a commit-graph file exists with generation numbers computed, we can avoid this merge-base calculation when the target commit has a larger generation number than the initial commits. Performance tests were run on a copy of the Linux repository where HEAD is contained in v4.13 but no earlier tag. Also, all tags were copied to branches and 'git branch --contains' was tested: Before: 60.0s After: 0.4s Rel %: -99.3% Reported-by: Jeff King <peff@peff.net> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 1, 2018 at 12:47 UTC f9b8908b85247ef60001a683c281af0080e9ee77
1 file changed +8 -1
commit.c
+8 -1
@@ -1056,12 +1056,19 @@ int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit *
1056 {
1057 struct commit_list *bases;
1058 int ret = 0, i;
1059 + uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1060
1061 if (parse_commit(commit))
1062 return ret;
1062 - for (i = 0; i < nr_reference; i++)
1063 + for (i = 0; i < nr_reference; i++) {
1064 if (parse_commit(reference[i]))
1065 return ret;
1066 + if (reference[i]->generation < min_generation)
1067 + min_generation = reference[i]->generation;
1068 + }
1069 +
1070 + if (commit->generation > min_generation)
1071 + return ret;
1072
1073 bases = paint_down_to_common(commit, nr_reference, reference);
1074 if (commit->object.flags & PARENT2)