Subject: [RFC PATCH 2/9] merge-recursive: Avoid unnecessary string list lookups

From: Elijah Newren <[email-masked]>
Date: Fri, 10 Nov 2017 14:21:49 -0800

Since we're taking entries from active_cache, which is already in sorted
order with same-named entries adjacent, we can skip a lookup.  Also, we can
just use append instead of insert (avoiding the need to find where to put
the new item) and still end up with a sorted list.

Signed-off-by: Elijah Newren <[email-masked]>
---
Assumed negligible performance change; I didn't even bother measuring it.
But I just happened to be looking around at this code while trying to figure
out some of the performance and figured it was at least speeding it up a
tiny bit.

 merge-recursive.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/merge-recursive.c b/merge-recursive.c
index 6ef1d52f0a..d54466649e 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -451,22 +451,28 @@ static struct stage_data *insert_stage_data(const char *path,
 static struct string_list *get_unmerged(void)
 {
 	struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
+	struct string_list_item *item;
+	const char *last = NULL;
 	int i;
 
 	unmerged->strdup_strings = 1;
 
 	for (i = 0; i < active_nr; i++) {
-		struct string_list_item *item;
 		struct stage_data *e;
 		const struct cache_entry *ce = active_cache[i];
 		if (!ce_stage(ce))
 			continue;
 
-		item = string_list_lookup(unmerged, ce->name);
-		if (!item) {
-			item = string_list_insert(unmerged, ce->name);
+		if (last == NULL || strcmp(last, ce->name)) {
+			/*
+			 * active_cache is in sorted order, so we can just call
+			 * string_list_append instead of string_list_insert and
+			 * still end up with a sorted list.
+			 */
+			item = string_list_append(unmerged, ce->name);
 			item->util = xcalloc(1, sizeof(struct stage_data));
 		}
+		last = ce->name;
 		e = item->util;
 		e->stages[ce_stage(ce)].mode = ce->ce_mode;
 		oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
-- 
2.15.0.46.g41dca04efb