1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 01:57:52 +00:00

in bencode.c's makeroom(), make the containers' sizes scale in powers of 2 to avoid excess reallocs.

This commit is contained in:
Jordan Lee 2012-12-11 05:23:48 +00:00
parent 959a8c2f8b
commit 84f2cf8fa2

View file

@ -158,30 +158,32 @@ tr_bencParseStr (const uint8_t * buf,
return 0; return 0;
} }
/* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
#define LIST_SIZE 4 /* number of items to increment list/dict buffer by */
static int static int
makeroom (tr_benc * val, makeroom (tr_benc * container, size_t count)
size_t count)
{ {
assert (isContainer (val)); const size_t needed = container->val.l.count + count;
if (val->val.l.count + count > val->val.l.alloc) assert (isContainer (container));
if (needed > container->val.l.alloc)
{ {
/* We need a bigger boat */ size_t n;
const int len = val->val.l.alloc + count + void * tmp;
(count % LIST_SIZE ? LIST_SIZE -
(count % LIST_SIZE) : 0);
void * tmp = realloc (val->val.l.vals, len * sizeof (tr_benc));
if (!tmp)
return 1;
val->val.l.alloc = len; /* scale the alloc size in powers-of-2 */
val->val.l.vals = tmp; n = container->val.l.alloc ? container->val.l.alloc : 8;
while (n < needed)
n *= 2u;
tmp = realloc (container->val.l.vals, n * sizeof (tr_benc));
if (tmp == NULL)
return 1;
container->val.l.alloc = n;
container->val.l.vals = tmp;
} }
return 0; return 0;
} }
static tr_benc* static tr_benc*
@ -676,7 +678,7 @@ tr_bencListAdd (tr_benc * list)
assert (tr_bencIsList (list)); assert (tr_bencIsList (list));
if (list->val.l.count == list->val.l.alloc) if (list->val.l.count == list->val.l.alloc)
tr_bencListReserve (list, LIST_SIZE); tr_bencListReserve (list, 4);
assert (list->val.l.count < list->val.l.alloc); assert (list->val.l.count < list->val.l.alloc);