git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] fetch-pack: binary search when storing wanted-refs

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Mar 27, 2019, 21:11 UTC
Message-ID
<20190327211110.46327-1-jonathantanmy@google.com>

In do_fetch_pack_v2(), the "sought" array is sorted by name, and it is not subsequently reordered (within the function). Therefore, receive_wanted_refs() can assume that "sought" is sorted, and can thus use a binary search when storing wanted-refs retrieved from the server.

Replace the existing linear search with a binary search. This improves performance significantly when mirror cloning a repository with more than 1 million refs.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 fetch-pack.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/fetch-pack.c b/fetch-pack.c
index e69993b2eb..e8266bd45a 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1298,6 +1298,11 @@ static void receive_shallow_info(struct fetch_pack_args *args,
 	}
 }
 
+static int cmp_name_ref(const void *name, const void *ref)
+{
+	return strcmp(name, (*(struct ref **)ref)->name);
+}
+
 static void receive_wanted_refs(struct packet_reader *reader,
 				struct ref **sought, int nr_sought)
 {
@@ -1305,20 +1310,16 @@ static void receive_wanted_refs(struct packet_reader *reader,
 	while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
 		struct object_id oid;
 		const char *end;
-		int i;
+		struct ref **found;
 
 		if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
 			die(_("expected wanted-ref, got '%s'"), reader->line);
 
-		for (i = 0; i < nr_sought; i++) {
-			if (!strcmp(end, sought[i]->name)) {
-				oidcpy(&sought[i]->old_oid, &oid);
-				break;
-			}
-		}
-
-		if (i == nr_sought)
+		found = bsearch(end, sought, nr_sought, sizeof(*sought),
+				cmp_name_ref);
+		if (!found)
 			die(_("unexpected wanted-ref: '%s'"), reader->line);
+		oidcpy(&(*found)->old_oid, &oid);
 	}
 
 	if (reader->status != PACKET_READ_DELIM)
-- 
2.21.0.197.gd478713db0
Next: Jeff King
Message 1 of 2 in “fetch-pack: binary search when storing wanted-refs”
  1. fetch-pack: binary search when storing wanted-refsJonathan Tan, Mar 27, 2019
  2. Jeff KingMar 28, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.