@matheusbsbs / matheus-bagliano / commits / 243f801d1d
Fork 0

Reuse the same buffer for all commits/tags in fast-import.

Since most commits and tag objects are around the same size and we only generate one at a time we can reuse the same buffer rather than xmalloc'ing and free'ing the buffer every time we generate a commit. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 28, 2006 at 13:15 UTC 243f801d1d08753cd4eff2a23e245f7575c37ad5
1 file changed +23 -20
fast-import.c
+23 -20
@@ -267,6 +267,7 @@ static struct tag *last_tag;
267 /* Input stream parsing */
268 static struct strbuf command_buf;
269 static unsigned long next_mark;
270 +static struct dbuf new_data;
271 static FILE* branch_log;
272
273
@@ -381,6 +382,17 @@ static char* pool_strdup(const char *s)
382 return r;
383 }
384
385 +static void size_dbuf(struct dbuf *b, size_t maxlen)
386 +{
387 + if (b->buffer) {
388 + if (b->capacity >= maxlen)
389 + return;
390 + free(b->buffer);
391 + }
392 + b->capacity = ((maxlen / 1024) + 1) * 1024;
393 + b->buffer = xmalloc(b->capacity);
394 +}
395 +
396 static void insert_mark(unsigned long idnum, struct object_entry *oe)
397 {
398 struct mark_set *s = marks;
@@ -925,15 +937,7 @@ static void mktree(struct tree_content *t,
937 maxlen += t->entries[i]->name->str_len + 34;
938 }
939
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 -
940 + size_dbuf(b, maxlen);
941 c = b->buffer;
942 for (i = 0; i < t->entry_count; i++) {
943 struct tree_entry *e = t->entries[i];
@@ -1515,7 +1519,6 @@ static void cmd_new_commit()
1519 char *sp;
1520 char *author = NULL;
1521 char *committer = NULL;
1518 - char *body;
1522
1523 /* Obtain the branch name from the rest of our command */
1524 sp = strchr(command_buf.buf, ' ') + 1;
@@ -1568,11 +1571,11 @@ static void cmd_new_commit()
1571
1572 /* build the tree and the commit */
1573 store_tree(&b->branch_tree);
1571 - body = xmalloc(97 + msglen
1574 + size_dbuf(&new_data, 97 + msglen
1575 + (author
1576 ? strlen(author) + strlen(committer)
1577 : 2 * strlen(committer)));
1575 - sp = body;
1578 + sp = new_data.buffer;
1579 sp += sprintf(sp, "tree %s\n",
1580 sha1_to_hex(b->branch_tree.versions[1].sha1));
1581 if (!is_null_sha1(b->sha1))
@@ -1589,8 +1592,9 @@ static void cmd_new_commit()
1592 free(committer);
1593 free(msg);
1594
1592 - store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark);
1593 - free(body);
1595 + store_object(OBJ_COMMIT,
1596 + new_data.buffer, sp - (char*)new_data.buffer,
1597 + NULL, b->sha1, next_mark);
1598 b->last_commit = object_count_by_type[OBJ_COMMIT];
1599
1600 if (branch_log) {
@@ -1616,7 +1620,6 @@ static void cmd_new_tag()
1620 struct branch *s;
1621 void *msg;
1622 size_t msglen;
1619 - char *body;
1623 struct tag *t;
1624 unsigned long from_mark = 0;
1625 unsigned char sha1[20];
@@ -1688,8 +1691,8 @@ static void cmd_new_tag()
1691 msg = cmd_data(&msglen);
1692
1693 /* build the tag object */
1691 - body = xmalloc(67 + strlen(t->name) + strlen(tagger) + msglen);
1692 - sp = body;
1694 + size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1695 + sp = new_data.buffer;
1696 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1697 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1698 sp += sprintf(sp, "tag %s\n", t->name);
@@ -1699,8 +1702,8 @@ static void cmd_new_tag()
1702 free(tagger);
1703 free(msg);
1704
1702 - store_object(OBJ_TAG, body, sp - body, NULL, t->sha1, 0);
1703 - free(body);
1705 + store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1706 + NULL, t->sha1, 0);
1707
1708 if (branch_log) {
1709 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
@@ -1749,7 +1752,7 @@ int main(int argc, const char **argv)
1752 {
1753 const char *base_name;
1754 int i;
1752 - unsigned long est_obj_cnt = 1000;
1755 + unsigned long est_obj_cnt = object_entry_alloc;
1756 char *pack_name;
1757 char *idx_name;
1758 struct stat sb;