Skip to content

Commit 9799889

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 81afdf7 commit 9799889

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

diffcore-rename.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,12 @@ static int find_renames(struct diff_score *mx,
991991
return count;
992992
}
993993

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)
995996
{
996997
int i, new_num_src;
997998

998-
if (detecting_copies)
999+
if (detecting_copies && !interesting)
9991000
return; /* nothing to remove */
10001001
if (break_idx)
10011002
return; /* culling incompatible with break detection */
@@ -1022,12 +1023,18 @@ static void remove_unneeded_paths_from_src(int detecting_copies)
10221023
* from rename_src here.
10231024
*/
10241025
for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
1026+
struct diff_filespec *one = rename_src[i].p->one;
1027+
10251028
/*
10261029
* renames are stored in rename_dst, so if a rename has
10271030
* already been detected using this source, we can just
10281031
* remove the source knowing rename_dst has its info.
10291032
*/
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))
10311038
continue;
10321039

10331040
if (new_num_src < i)
@@ -1040,6 +1047,7 @@ static void remove_unneeded_paths_from_src(int detecting_copies)
10401047
}
10411048

10421049
void diffcore_rename_extended(struct diff_options *options,
1050+
struct strset *relevant_sources,
10431051
struct strset *dirs_removed,
10441052
struct strmap *dir_rename_count)
10451053
{
@@ -1060,6 +1068,8 @@ void diffcore_rename_extended(struct diff_options *options,
10601068
want_copies = (detect_rename == DIFF_DETECT_COPY);
10611069
if (dirs_removed && (break_idx || want_copies))
10621070
BUG("dirs_removed incompatible with break/copy detection");
1071+
if (break_idx && relevant_sources)
1072+
BUG("break detection incompatible with source specification");
10631073
if (!minimum_score)
10641074
minimum_score = DEFAULT_RENAME_SCORE;
10651075

@@ -1127,9 +1137,10 @@ void diffcore_rename_extended(struct diff_options *options,
11271137
/*
11281138
* Cull sources:
11291139
* - remove ones corresponding to exact renames
1140+
* - remove ones not found in relevant_sources
11301141
*/
11311142
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);
11331144
trace2_region_leave("diff", "cull after exact", options->repo);
11341145
} else {
11351146
/* Determine minimum score to match basenames */
@@ -1148,7 +1159,7 @@ void diffcore_rename_extended(struct diff_options *options,
11481159
* - remove ones involved in renames (found via exact match)
11491160
*/
11501161
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);
11521163
trace2_region_leave("diff", "cull after exact", options->repo);
11531164

11541165
/* Preparation for basename-driven matching. */
@@ -1167,9 +1178,10 @@ void diffcore_rename_extended(struct diff_options *options,
11671178
/*
11681179
* Cull sources, again:
11691180
* - remove ones involved in renames (found via basenames)
1181+
* - remove ones not found in relevant_sources
11701182
*/
11711183
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);
11731185
trace2_region_leave("diff", "cull basename", options->repo);
11741186
}
11751187

@@ -1342,5 +1354,5 @@ void diffcore_rename_extended(struct diff_options *options,
13421354

13431355
void diffcore_rename(struct diff_options *options)
13441356
{
1345-
diffcore_rename_extended(options, NULL, NULL);
1357+
diffcore_rename_extended(options, NULL, NULL, NULL);
13461358
}

diffcore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ void partial_clear_dir_rename_count(struct strmap *dir_rename_count);
166166
void diffcore_break(struct repository *, int);
167167
void diffcore_rename(struct diff_options *);
168168
void diffcore_rename_extended(struct diff_options *options,
169+
struct strset *relevant_sources,
169170
struct strset *dirs_removed,
170171
struct strmap *dir_rename_count);
171172
void diffcore_merge_broken(void);

merge-ort.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,7 @@ static void detect_regular_renames(struct merge_options *opt,
20292029
diff_queued_diff = renames->pairs[side_index];
20302030
trace2_region_enter("diff", "diffcore_rename", opt->repo);
20312031
diffcore_rename_extended(&diff_opts,
2032+
NULL,
20322033
&renames->dirs_removed[side_index],
20332034
&renames->dir_rename_count[side_index]);
20342035
trace2_region_leave("diff", "diffcore_rename", opt->repo);

0 commit comments

Comments
 (0)