Recycle data buffers for tree generation in fast-import.
We only ever generate at most two tree streams at a time. Since most trees are around the same size we can simply recycle the buffers from one tree generation to the next rather than constantly xmalloc'ing and free'ing them. This should perform slightly better when handling a large number of trees as malloc has less work to do. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Aug 28, 2006 at 13:02 UTC
e2eb469d1ff9595882c8329ad415b1d7246769d0
1 file changed
+32
-14
fast-import.c
+32
-14
@@ -134,6 +134,7 @@ struct last_object
134
void *data;
135
unsigned long len;
136
unsigned int depth;
137
+ int no_free;
138
unsigned char sha1[20];
139
};
140
@@ -195,6 +196,12 @@ struct tag
196
unsigned char sha1[20];
197
};
198
199
+struct dbuf
200
+{
201
+ void *buffer;
202
+ size_t capacity;
203
+};
204
+
205
206
/* Stats and misc. counters */
207
static unsigned long max_depth = 10;
@@ -243,6 +250,8 @@ static unsigned int tree_entry_alloc = 1000;
250
static void *avail_tree_entry;
251
static unsigned int avail_tree_table_sz = 100;
252
static struct avail_tree_content **avail_tree_table;
253
+static struct dbuf old_tree;
254
+static struct dbuf new_tree;
255
256
/* Branch data */
257
static unsigned long max_active_branches = 5;
@@ -680,7 +689,7 @@ static int store_object(
689
if (delta)
690
free(delta);
691
if (last) {
683
- if (last->data)
692
+ if (last->data && !last->no_free)
693
free(last->data);
694
last->data = dat;
695
last->len = datlen;
@@ -897,11 +906,14 @@ static int tecmp1 (const void *_a, const void *_b)
906
b->name->str_dat, b->name->str_len, b->versions[1].mode);
907
}
908
900
-static void* mktree(struct tree_content *t, int v, unsigned long *szp)
909
+static void mktree(struct tree_content *t,
910
+ int v,
911
+ unsigned long *szp,
912
+ struct dbuf *b)
913
{
914
size_t maxlen = 0;
915
unsigned int i;
904
- char *buf, *c;
916
+ char *c;
917
918
if (!v)
919
qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
@@ -913,7 +925,16 @@ static void* mktree(struct tree_content *t, int v, unsigned long *szp)
925
maxlen += t->entries[i]->name->str_len + 34;
926
}
927
916
- buf = c = xmalloc(maxlen);
928
+ if (b->buffer) {
929
+ if (b->capacity < maxlen)
930
+ b->capacity = ((maxlen / 1024) + 1) * 1024;
931
+ b->buffer = xrealloc(b->buffer, b->capacity);
932
+ } else {
933
+ b->capacity = ((maxlen / 1024) + 1) * 1024;
934
+ b->buffer = xmalloc(b->capacity);
935
+ }
936
+
937
+ c = b->buffer;
938
for (i = 0; i < t->entry_count; i++) {
939
struct tree_entry *e = t->entries[i];
940
if (!e->versions[v].mode)
@@ -925,17 +946,14 @@ static void* mktree(struct tree_content *t, int v, unsigned long *szp)
946
hashcpy((unsigned char*)c, e->versions[v].sha1);
947
c += 20;
948
}
928
-
929
- *szp = c - buf;
930
- return buf;
949
+ *szp = c - (char*)b->buffer;
950
}
951
952
static void store_tree(struct tree_entry *root)
953
{
954
struct tree_content *t = root->tree;
955
unsigned int i, j, del;
937
- unsigned long vers1len;
938
- void **vers1dat;
956
+ unsigned long new_len;
957
struct last_object lo;
958
959
if (!is_null_sha1(root->versions[1].sha1))
@@ -951,16 +969,16 @@ static void store_tree(struct tree_entry *root)
969
lo.data = NULL;
970
lo.depth = 0;
971
} else {
954
- lo.data = mktree(t, 0, &lo.len);
972
+ mktree(t, 0, &lo.len, &old_tree);
973
+ lo.data = old_tree.buffer;
974
lo.depth = t->delta_depth;
975
+ lo.no_free = 1;
976
hashcpy(lo.sha1, root->versions[0].sha1);
977
}
958
- vers1dat = mktree(t, 1, &vers1len);
978
+ mktree(t, 1, &new_len, &new_tree);
979
960
- store_object(OBJ_TREE, vers1dat, vers1len,
980
+ store_object(OBJ_TREE, new_tree.buffer, new_len,
981
&lo, root->versions[1].sha1, 0);
962
- /* note: lo.dat (if created) was freed by store_object */
963
- free(vers1dat);
982
983
t->delta_depth = lo.depth;
984
hashcpy(root->versions[0].sha1, root->versions[1].sha1);