From: Meet Soni <meetsoni3017@gmail.com>
To: git@vger.kernel.org
Cc: Meet Soni <meetsoni3017@gmail.com>, Junio C Hamano <gitster@pobox.com>
Subject: [RFC PATCH 2/2] merge-recursive: optimize time complexity for get_unmerged
Date: Thu, 13 Feb 2025 14:30:40 +0530 [thread overview]
Message-ID: <20250213090040.16133-3-meetsoni3017@gmail.com> (raw)
In-Reply-To: <20250213090040.16133-1-meetsoni3017@gmail.com>
Previously, `get_unmerged()` used `string_list_insert()`, which has an
O(n^2) complexity due to shifting elements on each insertion. It also
called `string_list_lookup()` before insertion, which performs a binary
search in O(log n). This combination made insertion costly, especially
for large index states, as each new entry required both a search and
potentially shifting many elements.
Replace `string_list_insert()` with `string_list_append()` to achieve
O(n) insertion. After all entries are added, sort the list in O(n log n)
and remove duplicates in O(n), reducing the overall complexity to
O(n log n). This improves performance significantly for large datasets
while maintaining correctness.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
merge-recursive.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/merge-recursive.c b/merge-recursive.c
index 884ccf99a5..6165993429 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -547,15 +547,15 @@ static struct string_list *get_unmerged(struct index_state *istate)
if (!ce_stage(ce))
continue;
- item = string_list_lookup(unmerged, ce->name);
- if (!item) {
- item = string_list_insert(unmerged, ce->name);
- item->util = xcalloc(1, sizeof(struct stage_data));
- }
+ item = string_list_append(unmerged, ce->name);
+ item->util = xcalloc(1, sizeof(struct stage_data));
+
e = item->util;
e->stages[ce_stage(ce)].mode = ce->ce_mode;
oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
}
+ string_list_sort(unmerged);
+ string_list_remove_duplicates(unmerged, 1);
return unmerged;
}
--
2.34.1
next prev parent 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 ` [RFC PATCH 1/2] merge-recursive: optimize time complexity for process_renames Meet Soni
2025-02-13 17:06 ` Elijah Newren
2025-02-13 9:00 ` Meet Soni [this message]
2025-02-13 17:11 ` [RFC PATCH 2/2] merge-recursive: optimize time complexity for get_unmerged 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-3-meetsoni3017@gmail.com \
--to=meetsoni3017@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.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