Skip to content

Commit 099327b

Browse files
peffgitster
authored andcommitted
fetch-pack: avoid quadratic behavior in rev_list_push
When we call find_common to start finding common ancestors with the remote side of a fetch, the first thing we do is insert the tip of each ref into our rev_list linked list. We keep the list sorted the whole time with commit_list_insert_by_date, which means our insertion ends up doing O(n^2) timestamp comparisons. We could teach rev_list_push to use an unsorted list, and then sort it once after we have added each ref. However, in get_rev, we process the list by popping commits off the front and adding parents back in timestamp-sorted order. So that procedure would still operate on the large list. Instead, we can replace the linked list with a heap-based priority queue, which can do O(log n) insertion, making the whole insertion procedure O(n log n). As a result of switching to the prio_queue struct, we fix two minor bugs: 1. When we "pop" a commit in get_rev, and when we clear the rev_list in find_common, we do not take care to free the "struct commit_list", and just leak its memory. With the prio_queue implementation, the memory management is handled for us. 2. In get_rev, we look at the head commit of the list, possibly push its parents onto the list, and then "pop" the front of the list off, assuming it is the same element that we just peeked at. This is typically going to be the case, but would not be in the face of clock skew: the parents are inserted by date, and could potentially be inserted at the head of the list if they have a timestamp newer than their descendent. In this case, we would accidentally pop the parent, and never process it at all. The new implementation pulls the commit off of the queue as we examine it, and so does not suffer from this problem. With this patch, a fetch of a single commit into a repository with 50,000 refs went from: real 0m7.984s user 0m7.852s sys 0m0.120s to: real 0m2.017s user 0m1.884s sys 0m0.124s Before this patch, a larger case with 370K refs still had not completed after tens of minutes; with this patch, it completes in about 12 seconds. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 727377f commit 099327b

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

fetch-pack.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "run-command.h"
1212
#include "transport.h"
1313
#include "version.h"
14+
#include "prio-queue.h"
1415

1516
static int transfer_unpack_limit = -1;
1617
static int fetch_unpack_limit = -1;
@@ -37,7 +38,7 @@ static int marked;
3738
*/
3839
#define MAX_IN_VAIN 256
3940

40-
static struct commit_list *rev_list;
41+
static struct prio_queue rev_list = { compare_commits_by_commit_date };
4142
static int non_common_revs, multi_ack, use_sideband, allow_tip_sha1_in_want;
4243

4344
static void rev_list_push(struct commit *commit, int mark)
@@ -49,7 +50,7 @@ static void rev_list_push(struct commit *commit, int mark)
4950
if (parse_commit(commit))
5051
return;
5152

52-
commit_list_insert_by_date(commit, &rev_list);
53+
prio_queue_put(&rev_list, commit);
5354

5455
if (!(commit->object.flags & COMMON))
5556
non_common_revs++;
@@ -122,10 +123,10 @@ static const unsigned char *get_rev(void)
122123
unsigned int mark;
123124
struct commit_list *parents;
124125

125-
if (rev_list == NULL || non_common_revs == 0)
126+
if (rev_list.nr == 0 || non_common_revs == 0)
126127
return NULL;
127128

128-
commit = rev_list->item;
129+
commit = prio_queue_get(&rev_list);
129130
if (!commit->object.parsed)
130131
parse_commit(commit);
131132
parents = commit->parents;
@@ -152,8 +153,6 @@ static const unsigned char *get_rev(void)
152153
mark_common(parents->item, 1, 0);
153154
parents = parents->next;
154155
}
155-
156-
rev_list = rev_list->next;
157156
}
158157

159158
return commit->object.sha1;
@@ -442,7 +441,7 @@ static int find_common(struct fetch_pack_args *args,
442441
in_vain = 0;
443442
got_continue = 1;
444443
if (ack == ACK_ready) {
445-
rev_list = NULL;
444+
clear_prio_queue(&rev_list);
446445
got_ready = 1;
447446
}
448447
break;

0 commit comments

Comments
 (0)