Skip to content

Commit 0557656

Browse files
MadCodergitster
authored andcommitted
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>
1 parent eec813c commit 0557656

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

fast-import.c

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,10 @@ struct mark_set
182182

183183
struct last_object
184184
{
185-
void *data;
186-
unsigned long len;
185+
struct strbuf data;
187186
uint32_t offset;
188187
unsigned int depth;
189-
unsigned no_free:1;
188+
unsigned no_swap : 1;
190189
};
191190

192191
struct mem_pool
@@ -310,7 +309,7 @@ static struct mark_set *marks;
310309
static const char* mark_file;
311310

312311
/* Our last blob */
313-
static struct last_object last_blob;
312+
static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
314313

315314
/* Tree management */
316315
static unsigned int tree_entry_alloc = 1000;
@@ -950,9 +949,7 @@ static void end_packfile(void)
950949
free(old_p);
951950

952951
/* 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);
956953
last_blob.offset = 0;
957954
last_blob.depth = 0;
958955
}
@@ -1024,8 +1021,8 @@ static int store_object(
10241021
return 1;
10251022
}
10261023

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,
10291026
dat->buf, dat->len,
10301027
&deltalen, 0);
10311028
if (delta && deltalen >= dat->len) {
@@ -1111,11 +1108,14 @@ static int store_object(
11111108
free(out);
11121109
free(delta);
11131110
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+
}
11161118
last->offset = e->offset;
1117-
last->data = dat->buf;
1118-
last->len = dat->len;
11191119
}
11201120
return 0;
11211121
}
@@ -1242,7 +1242,7 @@ static void store_tree(struct tree_entry *root)
12421242
{
12431243
struct tree_content *t = root->tree;
12441244
unsigned int i, j, del;
1245-
struct last_object lo;
1245+
struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
12461246
struct object_entry *le;
12471247

12481248
if (!is_null_sha1(root->versions[1].sha1))
@@ -1254,19 +1254,11 @@ static void store_tree(struct tree_entry *root)
12541254
}
12551255

12561256
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) {
12641258
mktree(t, 0, &old_tree);
1265-
lo.len = old_tree.len;
1266-
lo.data = old_tree.buf;
1259+
lo.data = old_tree;
12671260
lo.offset = le->offset;
12681261
lo.depth = t->delta_depth;
1269-
lo.no_free = 1;
12701262
}
12711263

12721264
mktree(t, 1, &new_tree);
@@ -1714,14 +1706,12 @@ static char *parse_ident(const char *buf)
17141706

17151707
static void cmd_new_blob(void)
17161708
{
1717-
struct strbuf buf;
1709+
static struct strbuf buf = STRBUF_INIT;
17181710

17191711
read_next_command();
17201712
cmd_mark();
1721-
strbuf_init(&buf, 0);
17221713
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);
17251715
}
17261716

17271717
static void unload_one_branch(void)
@@ -1817,15 +1807,13 @@ static void file_change_m(struct branch *b)
18171807
}
18181808

18191809
if (inline_data) {
1820-
struct strbuf buf;
1810+
static struct strbuf buf = STRBUF_INIT;
18211811

18221812
if (!p_uq)
18231813
p = p_uq = xstrdup(p);
18241814
read_next_command();
1825-
strbuf_init(&buf, 0);
18261815
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);
18291817
} else if (oe) {
18301818
if (oe->type != OBJ_BLOB)
18311819
die("Not a blob (actually a %s): %s",

0 commit comments

Comments
 (0)