commit-reach: add tips_reachable_from_bases()

Both 'git for-each-ref --merged=<X>' and 'git branch --merged=<X>' use the ref-filter machinery to select references or branches (respectively) that are reachable from a set of commits presented by one or more --merged arguments. This happens within reach_filter(), which uses the revision-walk machinery to walk history in a standard way. However, the commit-reach.c file is full of custom searches that are more efficient, especially for reachability queries that can terminate early when reachability is discovered. Add a new tips_reachable_from_bases() method to commit-reach.c and call it from within reach_filter() in ref-filter.c. This affects both 'git branch' and 'git for-each-ref' as tested in p1500-graph-walks.sh. For the Linux kernel repository, we take an already-fast algorithm and make it even faster: Test HEAD~1 HEAD ------------------------------------------------------------------- 1500.5: contains: git for-each-ref --merged 0.13 0.02 -84.6% 1500.6: contains: git branch --merged 0.14 0.02 -85.7% 1500.7: contains: git tag --merged 0.15 0.03 -80.0% (Note that we remove the iterative 'git rev-list' test from p1500 because it no longer makes sense as a comparison to 'git for-each-ref' and would just waste time running it for these comparisons.) The algorithm is implemented in commit-reach.c in the method tips_reachable_from_base(). This method takes a string_list of tips and assigns the 'util' for each item with the value 1 if the base commit can reach those tips. Like other reachability queries in commit-reach.c, the fastest way to search for "can A reach B?" is to do a depth-first search up to the generation number of B, preferring to explore first parents before later parents. While we must walk all reachable commits up to that generation number when the answer is "no", the depth-first search can answer "yes" much faster than other approaches in most cases. This search becomes trickier when there are multiple targets for the depth-first search. The commits with lower generation number are more likely to be within the history of the start commit, but we don't want to waste time searching commits of low generation number if the commit target with lowest generation number has already been found. The trick here is to take the input commits and sort them by generation number in ascending order. Track the index within this order as min_generation_index. When we find a commit, if its index in the list is equal to min_generation_index, then we can increase the generation number boundary of our search to the next-lowest value in the list. With this mechanism, the number of commits to search is minimized with respect to the depth-first search heuristic. We will walk all commits up to the minimum generation number of a commit that is _not_ reachable from the start, but we will walk only the necessary portion of the depth-first search for the reachable commits of lower generation. Add extra tests for this behavior in t6600-test-reach.sh as the interesting data shape of that repository can sometimes demonstrate corner case bugs. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Mar 20, 2023 at 11:26 UTC cbfe360b140fe92d9c4a763bf630c3b8ba431522
5 files changed +219 -21
commit-reach.c
+113
@@ -1044,3 +1044,116 @@ void ahead_behind(struct repository *r,
1044 clear_bit_arrays(&bit_arrays);
1045 clear_prio_queue(&queue);
1046 }
1047 +
1048 +struct commit_and_index {
1049 + struct commit *commit;
1050 + unsigned int index;
1051 + timestamp_t generation;
1052 +};
1053 +
1054 +static int compare_commit_and_index_by_generation(const void *va, const void *vb)
1055 +{
1056 + const struct commit_and_index *a = (const struct commit_and_index *)va;
1057 + const struct commit_and_index *b = (const struct commit_and_index *)vb;
1058 +
1059 + if (a->generation > b->generation)
1060 + return 1;
1061 + if (a->generation < b->generation)
1062 + return -1;
1063 + return 0;
1064 +}
1065 +
1066 +void tips_reachable_from_bases(struct repository *r,
1067 + struct commit_list *bases,
1068 + struct commit **tips, size_t tips_nr,
1069 + int mark)
1070 +{
1071 + struct commit_and_index *commits;
1072 + size_t min_generation_index = 0;
1073 + timestamp_t min_generation;
1074 + struct commit_list *stack = NULL;
1075 +
1076 + if (!bases || !tips || !tips_nr)
1077 + return;
1078 +
1079 + /*
1080 + * Do a depth-first search starting at 'bases' to search for the
1081 + * tips. Stop at the lowest (un-found) generation number. When
1082 + * finding the lowest commit, increase the minimum generation
1083 + * number to the next lowest (un-found) generation number.
1084 + */
1085 +
1086 + CALLOC_ARRAY(commits, tips_nr);
1087 +
1088 + for (size_t i = 0; i < tips_nr; i++) {
1089 + commits[i].commit = tips[i];
1090 + commits[i].index = i;
1091 + commits[i].generation = commit_graph_generation(tips[i]);
1092 + }
1093 +
1094 + /* Sort with generation number ascending. */
1095 + QSORT(commits, tips_nr, compare_commit_and_index_by_generation);
1096 + min_generation = commits[0].generation;
1097 +
1098 + while (bases) {
1099 + repo_parse_commit(r, bases->item);
1100 + commit_list_insert(bases->item, &stack);
1101 + bases = bases->next;
1102 + }
1103 +
1104 + while (stack) {
1105 + int explored_all_parents = 1;
1106 + struct commit_list *p;
1107 + struct commit *c = stack->item;
1108 + timestamp_t c_gen = commit_graph_generation(c);
1109 +
1110 + /* Does it match any of our tips? */
1111 + for (size_t j = min_generation_index; j < tips_nr; j++) {
1112 + if (c_gen < commits[j].generation)
1113 + break;
1114 +
1115 + if (commits[j].commit == c) {
1116 + tips[commits[j].index]->object.flags |= mark;
1117 +
1118 + if (j == min_generation_index) {
1119 + unsigned int k = j + 1;
1120 + while (k < tips_nr &&
1121 + (tips[commits[k].index]->object.flags & mark))
1122 + k++;
1123 +
1124 + /* Terminate early if all found. */
1125 + if (k >= tips_nr)
1126 + goto done;
1127 +
1128 + min_generation_index = k;
1129 + min_generation = commits[k].generation;
1130 + }
1131 + }
1132 + }
1133 +
1134 + for (p = c->parents; p; p = p->next) {
1135 + repo_parse_commit(r, p->item);
1136 +
1137 + /* Have we already explored this parent? */
1138 + if (p->item->object.flags & SEEN)
1139 + continue;
1140 +
1141 + /* Is it below the current minimum generation? */
1142 + if (commit_graph_generation(p->item) < min_generation)
1143 + continue;
1144 +
1145 + /* Ok, we will explore from here on. */
1146 + p->item->object.flags |= SEEN;
1147 + explored_all_parents = 0;
1148 + commit_list_insert(p->item, &stack);
1149 + break;
1150 + }
1151 +
1152 + if (explored_all_parents)
1153 + pop_commit(&stack);
1154 + }
1155 +
1156 +done:
1157 + free(commits);
1158 + repo_clear_commit_marks(r, SEEN);
1159 +}
commit-reach.h
+9
@@ -135,4 +135,13 @@ void ahead_behind(struct repository *r,
135 struct commit **commits, size_t commits_nr,
136 struct ahead_behind_count *counts, size_t counts_nr);
137
138 +/*
139 + * For all tip commits, add 'mark' to their flags if and only if they
140 + * are reachable from one of the commits in 'bases'.
141 + */
142 +void tips_reachable_from_bases(struct repository *r,
143 + struct commit_list *bases,
144 + struct commit **tips, size_t tips_nr,
145 + int mark);
146 +
147 #endif
ref-filter.c
+4 -16
@@ -2396,33 +2396,22 @@ static void reach_filter(struct ref_array *array,
2396 struct commit_list *check_reachable,
2397 int include_reached)
2398 {
2399 - struct rev_info revs;
2399 int i, old_nr;
2400 struct commit **to_clear;
2402 - struct commit_list *cr;
2401
2402 if (!check_reachable)
2403 return;
2404
2405 CALLOC_ARRAY(to_clear, array->nr);
2408 -
2409 - repo_init_revisions(the_repository, &revs, NULL);
2410 -
2406 for (i = 0; i < array->nr; i++) {
2407 struct ref_array_item *item = array->items[i];
2413 - add_pending_object(&revs, &item->commit->object, item->refname);
2408 to_clear[i] = item->commit;
2409 }
2410
2417 - for (cr = check_reachable; cr; cr = cr->next) {
2418 - struct commit *merge_commit = cr->item;
2419 - merge_commit->object.flags |= UNINTERESTING;
2420 - add_pending_object(&revs, &merge_commit->object, "");
2421 - }
2422 -
2423 - revs.limited = 1;
2424 - if (prepare_revision_walk(&revs))
2425 - die(_("revision walk setup failed"));
2411 + tips_reachable_from_bases(the_repository,
2412 + check_reachable,
2413 + to_clear, array->nr,
2414 + UNINTERESTING);
2415
2416 old_nr = array->nr;
2417 array->nr = 0;
@@ -2446,7 +2435,6 @@ static void reach_filter(struct ref_array *array,
2435 clear_commit_marks(merge_commit, ALL_REV_FLAGS);
2436 }
2437
2449 - release_revisions(&revs);
2438 free(to_clear);
2439 }
2440
t/perf/p1500-graph-walks.sh
+10 -5
@@ -35,11 +35,16 @@ test_perf 'ahead-behind counts: git tag' '
35 xargs git tag -l --format="%(ahead-behind:HEAD)" <tags
36 '
37
38 -test_perf 'ahead-behind counts: git rev-list' '
39 - for r in $(cat refs)
40 - do
41 - git rev-list --count "HEAD..$r" || return 1
42 - done
38 +test_perf 'contains: git for-each-ref --merged' '
39 + git for-each-ref --merged=HEAD --stdin <refs
40 +'
41 +
42 +test_perf 'contains: git branch --merged' '
43 + xargs git branch --merged=HEAD <branches
44 +'
45 +
46 +test_perf 'contains: git tag --merged' '
47 + xargs git tag --merged=HEAD <tags
48 '
49
50 test_done
t/t6600-test-reach.sh
+83
@@ -529,4 +529,87 @@ test_expect_success 'for-each-ref ahead-behind:none' '
529 --format="%(refname) %(ahead-behind:commit-8-4)" --stdin
530 '
531
532 +test_expect_success 'for-each-ref merged:linear' '
533 + cat >input <<-\EOF &&
534 + refs/heads/commit-1-1
535 + refs/heads/commit-1-3
536 + refs/heads/commit-1-5
537 + refs/heads/commit-1-8
538 + refs/heads/commit-2-1
539 + refs/heads/commit-5-1
540 + refs/heads/commit-9-1
541 + EOF
542 + cat >expect <<-\EOF &&
543 + refs/heads/commit-1-1
544 + refs/heads/commit-1-3
545 + refs/heads/commit-1-5
546 + refs/heads/commit-1-8
547 + EOF
548 + run_all_modes git for-each-ref --merged=commit-1-9 \
549 + --format="%(refname)" --stdin
550 +'
551 +
552 +test_expect_success 'for-each-ref merged:all' '
553 + cat >input <<-\EOF &&
554 + refs/heads/commit-1-1
555 + refs/heads/commit-2-4
556 + refs/heads/commit-4-2
557 + refs/heads/commit-4-4
558 + EOF
559 + cat >expect <<-\EOF &&
560 + refs/heads/commit-1-1
561 + refs/heads/commit-2-4
562 + refs/heads/commit-4-2
563 + refs/heads/commit-4-4
564 + EOF
565 + run_all_modes git for-each-ref --merged=commit-5-5 \
566 + --format="%(refname)" --stdin
567 +'
568 +
569 +test_expect_success 'for-each-ref ahead-behind:some' '
570 + cat >input <<-\EOF &&
571 + refs/heads/commit-1-1
572 + refs/heads/commit-5-3
573 + refs/heads/commit-4-8
574 + refs/heads/commit-9-9
575 + EOF
576 + cat >expect <<-\EOF &&
577 + refs/heads/commit-1-1
578 + refs/heads/commit-5-3
579 + EOF
580 + run_all_modes git for-each-ref --merged=commit-9-6 \
581 + --format="%(refname)" --stdin
582 +'
583 +
584 +test_expect_success 'for-each-ref merged:some, multibase' '
585 + cat >input <<-\EOF &&
586 + refs/heads/commit-1-1
587 + refs/heads/commit-5-3
588 + refs/heads/commit-7-8
589 + refs/heads/commit-4-8
590 + refs/heads/commit-9-9
591 + EOF
592 + cat >expect <<-\EOF &&
593 + refs/heads/commit-1-1
594 + refs/heads/commit-4-8
595 + refs/heads/commit-5-3
596 + EOF
597 + run_all_modes git for-each-ref \
598 + --merged=commit-5-8 \
599 + --merged=commit-8-5 \
600 + --format="%(refname)" \
601 + --stdin
602 +'
603 +
604 +test_expect_success 'for-each-ref merged:none' '
605 + cat >input <<-\EOF &&
606 + refs/heads/commit-7-5
607 + refs/heads/commit-4-8
608 + refs/heads/commit-9-9
609 + EOF
610 + >expect &&
611 + run_all_modes git for-each-ref --merged=commit-8-4 \
612 + --format="%(refname)" --stdin
613 +'
614 +
615 test_done