speedup allocation in pack-redundant.c
Reuse discarded nodes of llists Signed-off-by: Alex Riesen <ariesen@harmanbecker.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Alex Riesen committed
Nov 22, 2005 at 15:56 UTC
60435f68bb1ff319f9a718c91c0efff7c495dcc4
1 file changed
+26
-6
pack-redundant.c
+26
-6
@@ -36,11 +36,31 @@ struct pll {
36
size_t pl_size;
37
};
38
39
-static inline void llist_free(struct llist *list)
39
+static struct llist_item *free_nodes = NULL;
40
+
41
+static inline struct llist_item *llist_item_get()
42
+{
43
+ struct llist_item *new;
44
+ if ( free_nodes ) {
45
+ new = free_nodes;
46
+ free_nodes = free_nodes->next;
47
+ } else
48
+ new = xmalloc(sizeof(struct llist_item));
49
+
50
+ return new;
51
+}
52
+
53
+static inline void llist_item_put(struct llist_item *item)
54
+{
55
+ item->next = free_nodes;
56
+ free_nodes = item;
57
+}
58
+
59
+static void llist_free(struct llist *list)
60
{
61
while((list->back = list->front)) {
62
list->front = list->front->next;
43
- free(list->back);
63
+ llist_item_put(list->back);
64
}
65
free(list);
66
}
@@ -62,13 +82,13 @@ static struct llist * llist_copy(struct llist *list)
82
if ((ret->size = list->size) == 0)
83
return ret;
84
65
- new = ret->front = xmalloc(sizeof(struct llist_item));
85
+ new = ret->front = llist_item_get();
86
new->sha1 = list->front->sha1;
87
88
old = list->front->next;
89
while (old) {
90
prev = new;
71
- new = xmalloc(sizeof(struct llist_item));
91
+ new = llist_item_get();
92
prev->next = new;
93
new->sha1 = old->sha1;
94
old = old->next;
@@ -82,7 +102,7 @@ static struct llist * llist_copy(struct llist *list)
102
static inline struct llist_item * llist_insert(struct llist *list,
103
struct llist_item *after, char *sha1)
104
{
85
- struct llist_item *new = xmalloc(sizeof(struct llist_item));
105
+ struct llist_item *new = llist_item_get();
106
new->sha1 = sha1;
107
new->next = NULL;
108
@@ -153,7 +173,7 @@ redo_from_start:
173
prev->next = l->next;
174
if (l == list->back)
175
list->back = prev;
156
- free(l);
176
+ llist_item_put(l);
177
list->size--;
178
return prev;
179
}