Skip to content

Commit b07dd90

Browse files
inosmeetgitster
authored andcommitted
merge-recursive: optimize time complexity for process_renames
Avoid O(n^2) complexity in `process_renames()` when building a sorted `string_list` by constructing it unsorted and sorting it afterward, reducing the complexity to O(n log n). Signed-off-by: Meet Soni <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e2067b4 commit b07dd90

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

merge-recursive.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,23 +2758,22 @@ static int process_renames(struct merge_options *opt,
27582758
const struct rename *sre;
27592759

27602760
/*
2761-
* FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2762-
* string_list one-by-one, but O(n log n) to build it unsorted and
2763-
* then sort it. Note that as we build the list, we do not need to
2764-
* check if the existing destination path is already in the list,
2765-
* because the structure of diffcore_rename guarantees we won't
2766-
* have duplicates.
2761+
* Note that as we build the list, we do not need to check if the
2762+
* existing destination path is already in the list, because the
2763+
* structure of diffcore_rename guarantees we won't have duplicates.
27672764
*/
27682765
for (i = 0; i < a_renames->nr; i++) {
27692766
sre = a_renames->items[i].util;
2770-
string_list_insert(&a_by_dst, sre->pair->two->path)->util
2767+
string_list_append(&a_by_dst, sre->pair->two->path)->util
27712768
= (void *)sre;
27722769
}
27732770
for (i = 0; i < b_renames->nr; i++) {
27742771
sre = b_renames->items[i].util;
2775-
string_list_insert(&b_by_dst, sre->pair->two->path)->util
2772+
string_list_append(&b_by_dst, sre->pair->two->path)->util
27762773
= (void *)sre;
27772774
}
2775+
string_list_sort(&a_by_dst);
2776+
string_list_sort(&b_by_dst);
27782777

27792778
for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
27802779
struct string_list *renames1, *renames2Dst;

0 commit comments

Comments
 (0)