Skip to content

Commit abd7ccd

Browse files
committed
Merge branch 'jt/fetch-pack-wanted-refs-optim'
Performance fix around "git fetch" that grabs many refs. * jt/fetch-pack-wanted-refs-optim: fetch-pack: binary search when storing wanted-refs
2 parents 8baf40b + b764300 commit abd7ccd

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
@@ -1298,27 +1298,28 @@ static void receive_shallow_info(struct fetch_pack_args *args,
12981298
}
12991299
}
13001300

1301+
static int cmp_name_ref(const void *name, const void *ref)
1302+
{
1303+
return strcmp(name, (*(struct ref **)ref)->name);
1304+
}
1305+
13011306
static void receive_wanted_refs(struct packet_reader *reader,
13021307
struct ref **sought, int nr_sought)
13031308
{
13041309
process_section_header(reader, "wanted-refs", 0);
13051310
while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
13061311
struct object_id oid;
13071312
const char *end;
1308-
int i;
1313+
struct ref **found;
13091314

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

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

13241325
if (reader->status != PACKET_READ_DELIM)

0 commit comments

Comments
 (0)