diffcore-rename: guide inexact rename detection based on basenames

Make use of the new find_basename_matches() function added in the last two patches, to find renames more rapidly in cases where we can match up files based on basenames. As a quick reminder (see the last two commit messages for more details), this means for example that docs/extensions.txt and docs/config/extensions.txt are considered likely renames if there are no remaining 'extensions.txt' files elsewhere among the added and deleted files, and if a similarity check confirms they are similar, then they are marked as a rename without looking for a better similarity match among other files. This is a behavioral change, as covered in more detail in the previous commit message. We do not use this heuristic together with either break or copy detection. The point of break detection is to say that filename similarity does not imply file content similarity, and we only want to know about file content similarity. The point of copy detection is to use more resources to check for additional similarities, while this is an optimization that uses far less resources but which might also result in finding slightly fewer similarities. So the idea behind this optimization goes against both of those features, and will be turned off for both. For the testcases mentioned in commit 557ac0350d ("merge-ort: begin performance work; instrument with trace2_region_* calls", 2020-10-28), this change improves the performance as follows: Before After no-renames: 13.815 s ± 0.062 s 13.294 s ± 0.103 s mega-renames: 1799.937 s ± 0.493 s 187.248 s ± 0.882 s just-one-mega: 51.289 s ± 0.019 s 5.557 s ± 0.017 s Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Feb 14, 2021 at 07:51 UTC bd24aa2f97a08fdd5a4982bc6268f70c6bb7b747
2 files changed +52 -8
diffcore-rename.c
+48 -5
@@ -379,7 +379,6 @@ static const char *get_basename(const char *filename)
379 return base ? base + 1 : filename;
380 }
381
382 -MAYBE_UNUSED
382 static int find_basename_matches(struct diff_options *options,
383 int minimum_score)
384 {
@@ -716,11 +715,55 @@ void diffcore_rename(struct diff_options *options)
715 if (minimum_score == MAX_SCORE)
716 goto cleanup;
717
719 - /* Calculate how many renames are left */
720 - num_destinations = (rename_dst_nr - rename_count);
721 - remove_unneeded_paths_from_src(want_copies);
718 num_sources = rename_src_nr;
719
720 + if (want_copies || break_idx) {
721 + /*
722 + * Cull sources:
723 + * - remove ones corresponding to exact renames
724 + */
725 + trace2_region_enter("diff", "cull after exact", options->repo);
726 + remove_unneeded_paths_from_src(want_copies);
727 + trace2_region_leave("diff", "cull after exact", options->repo);
728 + } else {
729 + /* Determine minimum score to match basenames */
730 + double factor = 0.5;
731 + char *basename_factor = getenv("GIT_BASENAME_FACTOR");
732 + int min_basename_score;
733 +
734 + if (basename_factor)
735 + factor = strtol(basename_factor, NULL, 10)/100.0;
736 + assert(factor >= 0.0 && factor <= 1.0);
737 + min_basename_score = minimum_score +
738 + (int)(factor * (MAX_SCORE - minimum_score));
739 +
740 + /*
741 + * Cull sources:
742 + * - remove ones involved in renames (found via exact match)
743 + */
744 + trace2_region_enter("diff", "cull after exact", options->repo);
745 + remove_unneeded_paths_from_src(want_copies);
746 + trace2_region_leave("diff", "cull after exact", options->repo);
747 +
748 + /* Utilize file basenames to quickly find renames. */
749 + trace2_region_enter("diff", "basename matches", options->repo);
750 + rename_count += find_basename_matches(options,
751 + min_basename_score);
752 + trace2_region_leave("diff", "basename matches", options->repo);
753 +
754 + /*
755 + * Cull sources, again:
756 + * - remove ones involved in renames (found via basenames)
757 + */
758 + trace2_region_enter("diff", "cull basename", options->repo);
759 + remove_unneeded_paths_from_src(want_copies);
760 + trace2_region_leave("diff", "cull basename", options->repo);
761 + }
762 +
763 + /* Calculate how many rename destinations are left */
764 + num_destinations = (rename_dst_nr - rename_count);
765 + num_sources = rename_src_nr; /* rename_src_nr reflects lower number */
766 +
767 /* All done? */
768 if (!num_destinations || !num_sources)
769 goto cleanup;
@@ -751,7 +794,7 @@ void diffcore_rename(struct diff_options *options)
794 struct diff_score *m;
795
796 if (rename_dst[i].is_rename)
754 - continue; /* dealt with exact match already. */
797 + continue; /* exact or basename match already handled */
798
799 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
800 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
t/t4001-diff-rename.sh
+4 -3
@@ -277,10 +277,11 @@ test_expect_success 'basename similarity vs best similarity' '
277 git add file.txt file.md &&
278 git commit -a -m "rename" &&
279 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
280 - # subdir/file.txt is 88% similar to file.md and 78% similar to file.txt
280 + # subdir/file.txt is 88% similar to file.md, 78% similar to file.txt,
281 + # but since same basenames are checked first...
282 cat >expected <<-\EOF &&
282 - R088 subdir/file.txt file.md
283 - A file.txt
283 + A file.md
284 + R078 subdir/file.txt file.txt
285 EOF
286 test_cmp expected actual
287 '