Changeset 13650


Ignore:
Timestamp:
Dec 11, 2012, 5:23:48 AM (8 years ago)
Author:
jordan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode.c

    r13640 r13650  
    159159}
    160160
    161 /* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
    162 #define LIST_SIZE 4 /* number of items to increment list/dict buffer by */
    163 
    164161static int
    165 makeroom (tr_benc * val,
    166           size_t    count)
    167 {
    168     assert (isContainer (val));
    169 
    170     if (val->val.l.count + count > val->val.l.alloc)
     162makeroom (tr_benc * container, size_t count)
     163{
     164  const size_t needed = container->val.l.count + count;
     165
     166  assert (isContainer (container));
     167
     168  if (needed > container->val.l.alloc)
    171169    {
    172         /* We need a bigger boat */
    173         const int len = val->val.l.alloc + count +
    174                       (count % LIST_SIZE ? LIST_SIZE -
    175                         (count % LIST_SIZE) : 0);
    176         void * tmp = realloc (val->val.l.vals, len * sizeof (tr_benc));
    177         if (!tmp)
    178             return 1;
    179 
    180         val->val.l.alloc = len;
    181         val->val.l.vals  = tmp;
    182     }
    183 
    184     return 0;
     170      size_t n;
     171      void * tmp;
     172
     173      /* scale the alloc size in powers-of-2 */
     174      n = container->val.l.alloc ? container->val.l.alloc : 8;
     175      while (n < needed)
     176        n *= 2u;
     177
     178      tmp = realloc (container->val.l.vals, n * sizeof (tr_benc));
     179      if (tmp == NULL)
     180        return 1;
     181
     182      container->val.l.alloc = n;
     183      container->val.l.vals = tmp;
     184    }
     185
     186  return 0;
    185187}
    186188
     
    677679
    678680    if (list->val.l.count == list->val.l.alloc)
    679         tr_bencListReserve (list, LIST_SIZE);
     681        tr_bencListReserve (list, 4);
    680682
    681683    assert (list->val.l.count < list->val.l.alloc);
Note: See TracChangeset for help on using the changeset viewer.