public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Meet Soni <meetsoni3017@gmail.com>
To: git@vger.kernel.org
Cc: Meet Soni <meetsoni3017@gmail.com>,
	Elijah Newren <newren@gmail.com>,
	Derrick Stolee <stolee@gmail.com>,
	Junio C Hamano <gitster@pobox.com>
Subject: [RFC PATCH 1/2] merge-recursive: optimize time complexity for process_renames
Date: Thu, 13 Feb 2025 14:30:39 +0530	[thread overview]
Message-ID: <20250213090040.16133-2-meetsoni3017@gmail.com> (raw)
In-Reply-To: <20250213090040.16133-1-meetsoni3017@gmail.com>

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 <meetsoni3017@gmail.com>
---
 merge-recursive.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/merge-recursive.c b/merge-recursive.c
index 5dfaf32b2c..884ccf99a5 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -2758,23 +2758,22 @@ static int process_renames(struct merge_options *opt,
 	const struct rename *sre;
 
 	/*
-	 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
-	 * string_list one-by-one, but O(n log n) to build it unsorted and
-	 * then sort it.  Note that as we build the list, we do not need to
-	 * check if the existing destination path is already in the list,
-	 * because the structure of diffcore_rename guarantees we won't
-	 * have duplicates.
+	 * Note that as we build the list, we do not need to check if the
+	 * existing destination path is already in the list, because the
+	 * structure of diffcore_rename guarantees we won't have duplicates.
 	 */
 	for (i = 0; i < a_renames->nr; i++) {
 		sre = a_renames->items[i].util;
-		string_list_insert(&a_by_dst, sre->pair->two->path)->util
+		string_list_append(&a_by_dst, sre->pair->two->path)->util
 			= (void *)sre;
 	}
 	for (i = 0; i < b_renames->nr; i++) {
 		sre = b_renames->items[i].util;
-		string_list_insert(&b_by_dst, sre->pair->two->path)->util
+		string_list_append(&b_by_dst, sre->pair->two->path)->util
 			= (void *)sre;
 	}
+	string_list_sort(&a_by_dst);
+	string_list_sort(&b_by_dst);
 
 	for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
 		struct string_list *renames1, *renames2Dst;
-- 
2.34.1


  reply	other threads:[~2025-02-13  9:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-11 19:43 [GSoC][PATCH] merge-recursive: optimize string_list construction Meet Soni
2025-02-11 20:59 ` Elijah Newren
2025-02-13  9:00 ` [RFC PATCH 0/2] merge-recursive: optimize time complexity Meet Soni
2025-02-13  9:00   ` Meet Soni [this message]
2025-02-13 17:06     ` [RFC PATCH 1/2] merge-recursive: optimize time complexity for process_renames Elijah Newren
2025-02-13  9:00   ` [RFC PATCH 2/2] merge-recursive: optimize time complexity for get_unmerged Meet Soni
2025-02-13 17:11     ` Elijah Newren
2025-02-13 18:30       ` Junio C Hamano
2025-02-13 18:45         ` Elijah Newren
2025-02-14  4:28       ` Meet Soni
2025-02-14  6:04         ` Elijah Newren
2025-02-14  8:24           ` Meet Soni
2025-02-14 19:00             ` Elijah Newren
2025-02-15  8:42               ` Meet Soni
2025-02-13 11:02   ` [RFC PATCH 0/2] merge-recursive: optimize time complexity Meet Soni
2025-02-13 18:30   ` Elijah Newren
2025-02-14  4:41   ` [GSoC][PATCH v2] merge-recursive: optimize time complexity for process_renames Meet Soni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250213090040.16133-2-meetsoni3017@gmail.com \
    --to=meetsoni3017@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    --cc=stolee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox