diffcore-rename: enable filtering possible rename sources

Add the ability to diffcore_rename_extended() to allow external callers to declare that they only need renames detected for a subset of source files, and use that information to skip detecting renames for them. There are two important pieces to this optimization that may not be obvious at first glance: * We do not require callers to just filter the filepairs out to remove the non-relevant sources, because exact rename detection is fast and when it finds a match it can remove both a source and a destination whereas the relevant_sources filter can only remove a source. * We need to filter out the source pairs in a preliminary pass instead of adding a strset_contains(relevant_sources, one->path) check within the nested matrix loop. The reason for that is if we have 30k renames, doing 30k * 30k = 900M strset_contains() calls becomes extraordinarily expensive and defeats the performance gains from this change; we only want to do 30k such calls instead. If callers pass NULL for relevant_sources, that is special cases to treat all sources as relevant. Since all callers currently pass NULL, this optimization does not yet have any effect. Subsequent commits will have merge-ort compute a set of relevant_sources to restrict which sources we detect renames for, and have merge-ort pass that set of relevant_sources to diffcore_rename_extended(). A note about filtering order: Some may be curious why we don't filter out irrelevant sources at the same time we filter out exact renames. While that technically could be done at this point, there are two reasons to defer it: First, was to reinforce a lesson that was too easy to forget. As I mentioned above, in the past I filtered irrelevant sources out before exact rename checking, and then discovered that exact renames' ability to remove both sources and destinations was an important consideration and thus doing the filtering after exact rename checking would speed things up. Then at some point I realized that basename matching could also remove both sources and destinations, and decided to put irrelevant source filtering after basename filtering. That slowed things down a lot. But, despite learning about this important ordering, in later restructuring I forgot and made the same mistake of putting the filtering after basename guided rename detection again. So, I have this series of patches structured to do the irrelevant filtering last to start to show how much extra it costs, and then add relevant filtering in to find_basename_matches() to show how much it speeds things up. Basically, it's a way to reinforce something that apparently was too easy to forget, and make sure the commit messages record this lesson. Second, the items in the "relevant_sources" in this patch series will include all sources that *might be* relevant. It has to be conservative and catch anything that might need a rename, but in the patch series after this one we'll find ways to weed out more of the *might be* relevant ones. Unfortunately, merge-ort does not have sufficient information to weed those ones out, and there isn't enough information at the time of filtering exact renames out to remove the extra ones either. It has to be deferred. So the deferral is in part to simplify some later additions. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Mar 11, 2021 at 00:38 UTC 9799889f2e2caddee00d59aec3e4ff605a275f98
3 files changed +21 -7
diffcore-rename.c
+19 -7
@@ -991,11 +991,12 @@ static int find_renames(struct diff_score *mx,
991 return count;
992 }
993
994 -static void remove_unneeded_paths_from_src(int detecting_copies)
994 +static void remove_unneeded_paths_from_src(int detecting_copies,
995 + struct strset *interesting)
996 {
997 int i, new_num_src;
998
998 - if (detecting_copies)
999 + if (detecting_copies && !interesting)
1000 return; /* nothing to remove */
1001 if (break_idx)
1002 return; /* culling incompatible with break detection */
@@ -1022,12 +1023,18 @@ static void remove_unneeded_paths_from_src(int detecting_copies)
1023 * from rename_src here.
1024 */
1025 for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
1026 + struct diff_filespec *one = rename_src[i].p->one;
1027 +
1028 /*
1029 * renames are stored in rename_dst, so if a rename has
1030 * already been detected using this source, we can just
1031 * remove the source knowing rename_dst has its info.
1032 */
1030 - if (rename_src[i].p->one->rename_used)
1033 + if (!detecting_copies && one->rename_used)
1034 + continue;
1035 +
1036 + /* If we don't care about the source path, skip it */
1037 + if (interesting && !strset_contains(interesting, one->path))
1038 continue;
1039
1040 if (new_num_src < i)
@@ -1040,6 +1047,7 @@ static void remove_unneeded_paths_from_src(int detecting_copies)
1047 }
1048
1049 void diffcore_rename_extended(struct diff_options *options,
1050 + struct strset *relevant_sources,
1051 struct strset *dirs_removed,
1052 struct strmap *dir_rename_count)
1053 {
@@ -1060,6 +1068,8 @@ void diffcore_rename_extended(struct diff_options *options,
1068 want_copies = (detect_rename == DIFF_DETECT_COPY);
1069 if (dirs_removed && (break_idx || want_copies))
1070 BUG("dirs_removed incompatible with break/copy detection");
1071 + if (break_idx && relevant_sources)
1072 + BUG("break detection incompatible with source specification");
1073 if (!minimum_score)
1074 minimum_score = DEFAULT_RENAME_SCORE;
1075
@@ -1127,9 +1137,10 @@ void diffcore_rename_extended(struct diff_options *options,
1137 /*
1138 * Cull sources:
1139 * - remove ones corresponding to exact renames
1140 + * - remove ones not found in relevant_sources
1141 */
1142 trace2_region_enter("diff", "cull after exact", options->repo);
1132 - remove_unneeded_paths_from_src(want_copies);
1143 + remove_unneeded_paths_from_src(want_copies, relevant_sources);
1144 trace2_region_leave("diff", "cull after exact", options->repo);
1145 } else {
1146 /* Determine minimum score to match basenames */
@@ -1148,7 +1159,7 @@ void diffcore_rename_extended(struct diff_options *options,
1159 * - remove ones involved in renames (found via exact match)
1160 */
1161 trace2_region_enter("diff", "cull after exact", options->repo);
1151 - remove_unneeded_paths_from_src(want_copies);
1162 + remove_unneeded_paths_from_src(want_copies, NULL);
1163 trace2_region_leave("diff", "cull after exact", options->repo);
1164
1165 /* Preparation for basename-driven matching. */
@@ -1167,9 +1178,10 @@ void diffcore_rename_extended(struct diff_options *options,
1178 /*
1179 * Cull sources, again:
1180 * - remove ones involved in renames (found via basenames)
1181 + * - remove ones not found in relevant_sources
1182 */
1183 trace2_region_enter("diff", "cull basename", options->repo);
1172 - remove_unneeded_paths_from_src(want_copies);
1184 + remove_unneeded_paths_from_src(want_copies, relevant_sources);
1185 trace2_region_leave("diff", "cull basename", options->repo);
1186 }
1187
@@ -1342,5 +1354,5 @@ void diffcore_rename_extended(struct diff_options *options,
1354
1355 void diffcore_rename(struct diff_options *options)
1356 {
1345 - diffcore_rename_extended(options, NULL, NULL);
1357 + diffcore_rename_extended(options, NULL, NULL, NULL);
1358 }
diffcore.h
+1
@@ -166,6 +166,7 @@ void partial_clear_dir_rename_count(struct strmap *dir_rename_count);
166 void diffcore_break(struct repository *, int);
167 void diffcore_rename(struct diff_options *);
168 void diffcore_rename_extended(struct diff_options *options,
169 + struct strset *relevant_sources,
170 struct strset *dirs_removed,
171 struct strmap *dir_rename_count);
172 void diffcore_merge_broken(void);
merge-ort.c
+1
@@ -2029,6 +2029,7 @@ static void detect_regular_renames(struct merge_options *opt,
2029 diff_queued_diff = renames->pairs[side_index];
2030 trace2_region_enter("diff", "diffcore_rename", opt->repo);
2031 diffcore_rename_extended(&diff_opts,
2032 + NULL,
2033 &renames->dirs_removed[side_index],
2034 &renames->dir_rename_count[side_index]);
2035 trace2_region_leave("diff", "diffcore_rename", opt->repo);