http-walker: reduce O(n) ops with doubly-linked list

Using the a Linux-kernel-derived doubly-linked list implementation from the Userspace RCU library allows us to enqueue and delete items from the object request queue in constant time. This change reduces enqueue times in the prefetch() function where object request queue could grow to several thousand objects. I left out the list_for_each_entry* family macros from list.h which relied on the __typeof__ operator as we support platforms without it. Thus, list_entry (aka "container_of") needs to be called explicitly inside macro-wrapped for loops. The downside is this costs us an additional pointer per object request, but this is offset by reduced overhead on queue operations leading to improved performance and shorter queue depths. Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Jul 11, 2016 at 20:51 UTC 94e99012fc7a02c5504214294279fa49b4cc8ce3
2 files changed +179 -27
http-walker.c
+15 -27
@@ -2,6 +2,7 @@
2 #include "commit.h"
3 #include "walker.h"
4 #include "http.h"
5 +#include "list.h"
6
7 struct alt_base {
8 char *base;
@@ -23,7 +24,7 @@ struct object_request {
24 struct alt_base *repo;
25 enum object_request_state state;
26 struct http_object_request *req;
26 - struct object_request *next;
27 + struct list_head node;
28 };
29
30 struct alternates_request {
@@ -41,7 +42,7 @@ struct walker_data {
42 struct alt_base *alt;
43 };
44
44 -static struct object_request *object_queue_head;
45 +static LIST_HEAD(object_queue_head);
46
47 static void fetch_alternates(struct walker *walker, const char *base);
48
@@ -110,19 +111,10 @@ static void process_object_response(void *callback_data)
111
112 static void release_object_request(struct object_request *obj_req)
113 {
113 - struct object_request *entry = object_queue_head;
114 -
114 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
115 error("fd leakage in release: %d", obj_req->req->localfile);
117 - if (obj_req == object_queue_head) {
118 - object_queue_head = obj_req->next;
119 - } else {
120 - while (entry->next != NULL && entry->next != obj_req)
121 - entry = entry->next;
122 - if (entry->next == obj_req)
123 - entry->next = entry->next->next;
124 - }
116
117 + list_del(&obj_req->node);
118 free(obj_req);
119 }
120
@@ -130,8 +122,10 @@ static void release_object_request(struct object_request *obj_req)
122 static int fill_active_slot(struct walker *walker)
123 {
124 struct object_request *obj_req;
125 + struct list_head *pos, *tmp, *head = &object_queue_head;
126
134 - for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
127 + list_for_each_safe(pos, tmp, head) {
128 + obj_req = list_entry(pos, struct object_request, node);
129 if (obj_req->state == WAITING) {
130 if (has_sha1_file(obj_req->sha1))
131 obj_req->state = COMPLETE;
@@ -148,7 +142,6 @@ static int fill_active_slot(struct walker *walker)
142 static void prefetch(struct walker *walker, unsigned char *sha1)
143 {
144 struct object_request *newreq;
151 - struct object_request *tail;
145 struct walker_data *data = walker->data;
146
147 newreq = xmalloc(sizeof(*newreq));
@@ -157,18 +150,9 @@ static void prefetch(struct walker *walker, unsigned char *sha1)
150 newreq->repo = data->alt;
151 newreq->state = WAITING;
152 newreq->req = NULL;
160 - newreq->next = NULL;
153
154 http_is_verbose = walker->get_verbosely;
163 -
164 - if (object_queue_head == NULL) {
165 - object_queue_head = newreq;
166 - } else {
167 - tail = object_queue_head;
168 - while (tail->next != NULL)
169 - tail = tail->next;
170 - tail->next = newreq;
171 - }
155 + list_add_tail(&newreq->node, &object_queue_head);
156
157 #ifdef USE_CURL_MULTI
158 fill_active_slots();
@@ -451,11 +435,15 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
435 {
436 char *hex = sha1_to_hex(sha1);
437 int ret = 0;
454 - struct object_request *obj_req = object_queue_head;
438 + struct object_request *obj_req = NULL;
439 struct http_object_request *req;
440 + struct list_head *pos, *head = &object_queue_head;
441
457 - while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
458 - obj_req = obj_req->next;
442 + list_for_each(pos, head) {
443 + obj_req = list_entry(pos, struct object_request, node);
444 + if (!hashcmp(obj_req->sha1, sha1))
445 + break;
446 + }
447 if (obj_req == NULL)
448 return error("Couldn't find request for %s in the queue", hex);
449
list.h new
+164
@@ -0,0 +1,164 @@
1 +/*
2 + * Copyright (C) 2002 Free Software Foundation, Inc.
3 + * (originally part of the GNU C Library and Userspace RCU)
4 + * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 + *
6 + * Copyright (C) 2009 Pierre-Marc Fournier
7 + * Conversion to RCU list.
8 + * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 + *
10 + * This library is free software; you can redistribute it and/or
11 + * modify it under the terms of the GNU Lesser General Public
12 + * License as published by the Free Software Foundation; either
13 + * version 2.1 of the License, or (at your option) any later version.
14 + *
15 + * This library is distributed in the hope that it will be useful,
16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 + * Lesser General Public License for more details.
19 + *
20 + * You should have received a copy of the GNU Lesser General Public
21 + * License along with this library; if not, see
22 + * <http://www.gnu.org/licenses/>.
23 + */
24 +
25 +#ifndef LIST_H
26 +#define LIST_H 1
27 +
28 +/*
29 + * The definitions of this file are adopted from those which can be
30 + * found in the Linux kernel headers to enable people familiar with the
31 + * latter find their way in these sources as well.
32 + */
33 +
34 +/* Basic type for the double-link list. */
35 +struct list_head {
36 + struct list_head *next, *prev;
37 +};
38 +
39 +/* Define a variable with the head and tail of the list. */
40 +#define LIST_HEAD(name) \
41 + struct list_head name = { &(name), &(name) }
42 +
43 +/* Initialize a new list head. */
44 +#define INIT_LIST_HEAD(ptr) \
45 + (ptr)->next = (ptr)->prev = (ptr)
46 +
47 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
48 +
49 +/* Add new element at the head of the list. */
50 +static inline void list_add(struct list_head *newp, struct list_head *head)
51 +{
52 + head->next->prev = newp;
53 + newp->next = head->next;
54 + newp->prev = head;
55 + head->next = newp;
56 +}
57 +
58 +/* Add new element at the tail of the list. */
59 +static inline void list_add_tail(struct list_head *newp, struct list_head *head)
60 +{
61 + head->prev->next = newp;
62 + newp->next = head;
63 + newp->prev = head->prev;
64 + head->prev = newp;
65 +}
66 +
67 +/* Remove element from list. */
68 +static inline void __list_del(struct list_head *prev, struct list_head *next)
69 +{
70 + next->prev = prev;
71 + prev->next = next;
72 +}
73 +
74 +/* Remove element from list. */
75 +static inline void list_del(struct list_head *elem)
76 +{
77 + __list_del(elem->prev, elem->next);
78 +}
79 +
80 +/* Remove element from list, initializing the element's list pointers. */
81 +static inline void list_del_init(struct list_head *elem)
82 +{
83 + list_del(elem);
84 + INIT_LIST_HEAD(elem);
85 +}
86 +
87 +/* Delete from list, add to another list as head. */
88 +static inline void list_move(struct list_head *elem, struct list_head *head)
89 +{
90 + __list_del(elem->prev, elem->next);
91 + list_add(elem, head);
92 +}
93 +
94 +/* Replace an old entry. */
95 +static inline void list_replace(struct list_head *old, struct list_head *newp)
96 +{
97 + newp->next = old->next;
98 + newp->prev = old->prev;
99 + newp->prev->next = newp;
100 + newp->next->prev = newp;
101 +}
102 +
103 +/* Join two lists. */
104 +static inline void list_splice(struct list_head *add, struct list_head *head)
105 +{
106 + /* Do nothing if the list which gets added is empty. */
107 + if (add != add->next) {
108 + add->next->prev = head;
109 + add->prev->next = head->next;
110 + head->next->prev = add->prev;
111 + head->next = add->next;
112 + }
113 +}
114 +
115 +/* Get typed element from list at a given position. */
116 +#define list_entry(ptr, type, member) \
117 + ((type *) ((char *) (ptr) - offsetof(type, member)))
118 +
119 +/* Get first entry from a list. */
120 +#define list_first_entry(ptr, type, member) \
121 + list_entry((ptr)->next, type, member)
122 +
123 +/* Iterate forward over the elements of the list. */
124 +#define list_for_each(pos, head) \
125 + for (pos = (head)->next; pos != (head); pos = pos->next)
126 +
127 +/*
128 + * Iterate forward over the elements list. The list elements can be
129 + * removed from the list while doing this.
130 + */
131 +#define list_for_each_safe(pos, p, head) \
132 + for (pos = (head)->next, p = pos->next; \
133 + pos != (head); \
134 + pos = p, p = pos->next)
135 +
136 +/* Iterate backward over the elements of the list. */
137 +#define list_for_each_prev(pos, head) \
138 + for (pos = (head)->prev; pos != (head); pos = pos->prev)
139 +
140 +/*
141 + * Iterate backwards over the elements list. The list elements can be
142 + * removed from the list while doing this.
143 + */
144 +#define list_for_each_prev_safe(pos, p, head) \
145 + for (pos = (head)->prev, p = pos->prev; \
146 + pos != (head); \
147 + pos = p, p = pos->prev)
148 +
149 +static inline int list_empty(struct list_head *head)
150 +{
151 + return head == head->next;
152 +}
153 +
154 +static inline void list_replace_init(struct list_head *old,
155 + struct list_head *newp)
156 +{
157 + struct list_head *head = old->next;
158 +
159 + list_del(old);
160 + list_add_tail(newp, head);
161 + INIT_LIST_HEAD(old);
162 +}
163 +
164 +#endif /* LIST_H */