fast-import optimization:
Now that cmd_data acts on a strbuf, make last_object stashed buffer be a strbuf as well. On new stash, don't free the last stashed buffer, rather swap it with the one you will stash, this way, callers of store_object can act on static strbufs, and at some point, fast-import won't allocate new memory for objects buffers. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Sep 17, 2007 at 14:00 UTC
0557656930d41f10c90bccf90e3c5bd87bd53661
1 file changed
+20
-32
fast-import.c
+20
-32
@@ -182,11 +182,10 @@ struct mark_set
182
183
struct last_object
184
{
185
- void *data;
186
- unsigned long len;
185
+ struct strbuf data;
186
uint32_t offset;
187
unsigned int depth;
189
- unsigned no_free:1;
188
+ unsigned no_swap : 1;
189
};
190
191
struct mem_pool
@@ -310,7 +309,7 @@ static struct mark_set *marks;
309
static const char* mark_file;
310
311
/* Our last blob */
313
-static struct last_object last_blob;
312
+static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
313
314
/* Tree management */
315
static unsigned int tree_entry_alloc = 1000;
@@ -950,9 +949,7 @@ static void end_packfile(void)
949
free(old_p);
950
951
/* We can't carry a delta across packfiles. */
953
- free(last_blob.data);
954
- last_blob.data = NULL;
955
- last_blob.len = 0;
952
+ strbuf_release(&last_blob.data);
953
last_blob.offset = 0;
954
last_blob.depth = 0;
955
}
@@ -1024,8 +1021,8 @@ static int store_object(
1021
return 1;
1022
}
1023
1027
- if (last && last->data && last->depth < max_depth) {
1028
- delta = diff_delta(last->data, last->len,
1024
+ if (last && last->data.buf && last->depth < max_depth) {
1025
+ delta = diff_delta(last->data.buf, last->data.len,
1026
dat->buf, dat->len,
1027
&deltalen, 0);
1028
if (delta && deltalen >= dat->len) {
@@ -1111,11 +1108,14 @@ static int store_object(
1108
free(out);
1109
free(delta);
1110
if (last) {
1114
- if (!last->no_free)
1115
- free(last->data);
1111
+ if (last->no_swap) {
1112
+ last->data = *dat;
1113
+ } else {
1114
+ struct strbuf tmp = *dat;
1115
+ *dat = last->data;
1116
+ last->data = tmp;
1117
+ }
1118
last->offset = e->offset;
1117
- last->data = dat->buf;
1118
- last->len = dat->len;
1119
}
1120
return 0;
1121
}
@@ -1242,7 +1242,7 @@ static void store_tree(struct tree_entry *root)
1242
{
1243
struct tree_content *t = root->tree;
1244
unsigned int i, j, del;
1245
- struct last_object lo;
1245
+ struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
1246
struct object_entry *le;
1247
1248
if (!is_null_sha1(root->versions[1].sha1))
@@ -1254,19 +1254,11 @@ static void store_tree(struct tree_entry *root)
1254
}
1255
1256
le = find_object(root->versions[0].sha1);
1257
- if (!S_ISDIR(root->versions[0].mode)
1258
- || !le
1259
- || le->pack_id != pack_id) {
1260
- lo.data = NULL;
1261
- lo.depth = 0;
1262
- lo.no_free = 0;
1263
- } else {
1257
+ if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
1258
mktree(t, 0, &old_tree);
1265
- lo.len = old_tree.len;
1266
- lo.data = old_tree.buf;
1259
+ lo.data = old_tree;
1260
lo.offset = le->offset;
1261
lo.depth = t->delta_depth;
1269
- lo.no_free = 1;
1262
}
1263
1264
mktree(t, 1, &new_tree);
@@ -1714,14 +1706,12 @@ static char *parse_ident(const char *buf)
1706
1707
static void cmd_new_blob(void)
1708
{
1717
- struct strbuf buf;
1709
+ static struct strbuf buf = STRBUF_INIT;
1710
1711
read_next_command();
1712
cmd_mark();
1721
- strbuf_init(&buf, 0);
1713
cmd_data(&buf);
1723
- if (store_object(OBJ_BLOB, &buf, &last_blob, NULL, next_mark))
1724
- strbuf_release(&buf);
1714
+ store_object(OBJ_BLOB, &buf, &last_blob, NULL, next_mark);
1715
}
1716
1717
static void unload_one_branch(void)
@@ -1817,15 +1807,13 @@ static void file_change_m(struct branch *b)
1807
}
1808
1809
if (inline_data) {
1820
- struct strbuf buf;
1810
+ static struct strbuf buf = STRBUF_INIT;
1811
1812
if (!p_uq)
1813
p = p_uq = xstrdup(p);
1814
read_next_command();
1825
- strbuf_init(&buf, 0);
1815
cmd_data(&buf);
1827
- if (store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0))
1828
- strbuf_release(&buf);
1816
+ store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0);
1817
} else if (oe) {
1818
if (oe->type != OBJ_BLOB)
1819
die("Not a blob (actually a %s): %s",