Changeset 7469


Ignore:
Timestamp:
Dec 22, 2008, 7:16:06 PM (12 years ago)
Author:
charles
Message:

(trunk libT) speed improvments from wereHamster

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/list.c

    r7467 r7469  
    159159}
    160160
     161
     162
     163/*
     164 * Double-linked list with easy memory management and fast
     165 * insert/remove operations
     166 */
     167
     168void
     169__tr_list_init( struct __tr_list * head )
     170{
     171    head->next = head;
     172    head->prev = head;
     173}
     174
     175void
     176__tr_list_insert( struct __tr_list * list,
     177                  struct __tr_list * prev,
     178                  struct __tr_list * next)
     179{
     180    next->prev = list;
     181    list->next = next;
     182    list->prev = prev;
     183    prev->next = list;
     184}
     185
     186void
     187__tr_list_splice( struct __tr_list * prev,
     188                  struct __tr_list * next)
     189{
     190    next->prev = prev;
     191    prev->next = next;
     192}
     193
     194 
     195void
     196__tr_list_append( struct __tr_list * head,
     197                  struct __tr_list * list)
     198{
     199    __tr_list_insert( list, head->prev, head );
     200}
     201
     202void
     203__tr_list_remove( struct __tr_list * head )
     204{
     205    __tr_list_splice( head->prev, head->next );
     206    head->next = head->prev = NULL;
     207}
     208
     209void
     210__tr_list_destroy( struct __tr_list * head,
     211                   __tr_list_free_t   func)
     212{
     213    while ( head->next != head )
     214    {
     215        struct __tr_list * list = head->next;
     216        __tr_list_splice( list->prev, list->next );
     217
     218        func( list );
     219    }
     220}
  • trunk/libtransmission/list.h

    r7467 r7469  
    5353                       TrListCompareFunc compare_func );
    5454
     55
     56/*
     57 * Double-linked list with easy memory management and fast
     58 * insert/remove operations
     59 */
     60
     61struct __tr_list
     62{
     63    struct __tr_list * next, * prev;
     64};
     65
     66#define __tr_list_entry(ptr, type, member) ({            \
     67    const struct __tr_list *__mptr = (ptr);              \
     68    (void *)( (char *)__mptr - offsetof(type,member) );  \
     69})
     70
     71typedef int  ( *__tr_list_cmp_t ) ( const void * a, const void * b );
     72typedef void ( *__tr_list_free_t )( void * );
     73
     74
     75/**
     76 *    __tr_list_init()
     77 *
     78 * Init @head as an empty list.
     79 */
     80void
     81__tr_list_init( struct __tr_list * head );
     82
     83
     84/**
     85 *    __tr_list_insert()
     86 *
     87 * Insert @list between @prev and @next.
     88 */
     89void
     90__tr_list_insert( struct __tr_list * list,
     91                  struct __tr_list * prev,
     92                  struct __tr_list * next);
     93
     94/**
     95 *    __tr_list_splice()
     96 *
     97 * Connect @prev with @next, removing any nodes that were
     98 * in between.
     99 */
     100void
     101__tr_list_splice( struct __tr_list * prev,
     102                  struct __tr_list * next);
     103
     104/**
     105 *    __tr_list_append()
     106 *
     107 * Append @list to the end of @head.
     108 */
     109void
     110__tr_list_append( struct __tr_list * head,
     111                  struct __tr_list * list);
     112
     113/**
     114 *    __tr_list_remove()
     115 *
     116 * Remove @head from the list it is in.
     117 */
     118void
     119__tr_list_remove( struct __tr_list * head );
     120
     121/**
     122 *    __tr_list_destroy()
     123 *
     124 * Destroy the list and free all nodes
     125 */
     126void
     127__tr_list_destroy( struct __tr_list * head,
     128                   __tr_list_free_t   func);
     129
    55130#endif /* TR_LIST_H */
    56131
  • trunk/libtransmission/peer-io.c

    r7458 r7469  
    7474    tr_bool  isPieceData;
    7575    size_t   length;
     76    struct __tr_list head;
    7677};
    7778
     
    107108    struct evbuffer  * inbuf;
    108109    struct evbuffer  * outbuf;
    109     tr_list          * outbuf_datatypes; /* struct tr_datatype */
     110    struct __tr_list  outbuf_datatypes; /* struct tr_datatype */
    110111
    111112    struct event       event_read;
     
    122123    while( bytes_transferred )
    123124    {
    124         struct tr_datatype * next = io->outbuf_datatypes->data;
     125        struct tr_datatype * next = __tr_list_entry( io->outbuf_datatypes.next,
     126                                                     struct tr_datatype, head );
    125127        const size_t payload = MIN( next->length, bytes_transferred );
    126128        const size_t overhead = getPacketOverhead( payload );
     
    136138        bytes_transferred -= payload;
    137139        next->length -= payload;
    138         if( !next->length )
    139             tr_free( tr_list_pop_front( &io->outbuf_datatypes ) );
     140        if( !next->length ) {
     141            __tr_list_remove( io->outbuf_datatypes.next );
     142            tr_free( next );
     143        }
    140144    }
    141145}
     
    361365    io->inbuf = evbuffer_new( );
    362366    io->outbuf = evbuffer_new( );
     367
    363368    event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io );
    364369    event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io );
     370
     371    __tr_list_init( &io->outbuf_datatypes );
     372
    365373    tr_peerIoSetBandwidth( io, session->bandwidth );
     374
    366375    return io;
    367376}
     
    397406           ? NULL
    398407           : tr_peerIoNew( session, addr, port, torrentHash, 0, socket );
     408}
     409
     410static void
     411trDatatypeFree( void * data )
     412{
     413    struct tr_datatype * dt = __tr_list_entry( data, struct tr_datatype, head );
     414    tr_free(dt);
    399415}
    400416
     
    411427    tr_netClose( io->socket );
    412428    tr_cryptoFree( io->crypto );
    413     tr_list_free( &io->outbuf_datatypes, tr_free );
     429    __tr_list_destroy( &io->outbuf_datatypes, trDatatypeFree );
    414430
    415431    io->magicNumber = 0xDEAD;
     
    702718    datatype->isPieceData = isPieceData != 0;
    703719    datatype->length = writemeLen;
    704     tr_list_append( &io->outbuf_datatypes, datatype );
     720
     721    __tr_list_init( &datatype->head );
     722    __tr_list_append( &io->outbuf_datatypes, &datatype->head );
    705723
    706724    evbuffer_add( io->outbuf, writeme, writemeLen );
Note: See TracChangeset for help on using the changeset viewer.