Skip to content

Commit b764300

Browse files
jonathantanmygitster
authored andcommitted
fetch-pack: binary search when storing wanted-refs
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aeb582a commit b764300

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

fetch-pack.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,27 +1295,28 @@ static void receive_shallow_info(struct fetch_pack_args *args,
12951295
}
12961296
}
12971297

1298+
static int cmp_name_ref(const void *name, const void *ref)
1299+
{
1300+
return strcmp(name, (*(struct ref **)ref)->name);
1301+
}
1302+
12981303
static void receive_wanted_refs(struct packet_reader *reader,
12991304
struct ref **sought, int nr_sought)
13001305
{
13011306
process_section_header(reader, "wanted-refs", 0);
13021307
while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
13031308
struct object_id oid;
13041309
const char *end;
1305-
int i;
1310+
struct ref **found;
13061311

13071312
if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
13081313
die(_("expected wanted-ref, got '%s'"), reader->line);
13091314

1310-
for (i = 0; i < nr_sought; i++) {
1311-
if (!strcmp(end, sought[i]->name)) {
1312-
oidcpy(&sought[i]->old_oid, &oid);
1313-
break;
1314-
}
1315-
}
1316-
1317-
if (i == nr_sought)
1315+
found = bsearch(end, sought, nr_sought, sizeof(*sought),
1316+
cmp_name_ref);
1317+
if (!found)
13181318
die(_("unexpected wanted-ref: '%s'"), reader->line);
1319+
oidcpy(&(*found)->old_oid, &oid);
13191320
}
13201321

13211322
if (reader->status != PACKET_READ_DELIM)

0 commit comments

Comments
 (0)