Changeset 12013


Ignore:
Timestamp:
Feb 23, 2011, 6:01:16 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) #4051 "Use composition for the tr_history fields in tr_peer" -- fixed.

If we use composition on these objects we can save a handful of pointers per peer. This isn't a big deal, but it's an easy/safe change to do.

Location:
trunk/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/history-test.c

    r10390 r12013  
    3333test1( void )
    3434{
    35     tr_recentHistory * h;
     35    tr_recentHistory h;
    3636
    37     h = tr_historyNew( 60, 10 );
    38     tr_historyAdd( h, 10000, 1 );
    39     check( (int)tr_historyGet( h, 12000, 1000 ) == 0 )
    40     check( (int)tr_historyGet( h, 12000, 3000 ) == 1 )
    41     check( (int)tr_historyGet( h, 12000, 5000 ) == 1 )
    42     tr_historyAdd( h, 20000, 1 );
    43     check( (int)tr_historyGet( h, 22000,  1000 ) == 0 )
    44     check( (int)tr_historyGet( h, 22000,  3000 ) == 1 )
    45     check( (int)tr_historyGet( h, 22000, 15000 ) == 2 )
    46     check( (int)tr_historyGet( h, 22000, 20000 ) == 2 )
    47     tr_historyFree( h );
     37    tr_historyConstruct( &h, 60, 10 );
     38    tr_historyAdd( &h, 10000, 1 );
     39    check( (int)tr_historyGet( &h, 12000, 1000 ) == 0 )
     40    check( (int)tr_historyGet( &h, 12000, 3000 ) == 1 )
     41    check( (int)tr_historyGet( &h, 12000, 5000 ) == 1 )
     42    tr_historyAdd( &h, 20000, 1 );
     43    check( (int)tr_historyGet( &h, 22000,  1000 ) == 0 )
     44    check( (int)tr_historyGet( &h, 22000,  3000 ) == 1 )
     45    check( (int)tr_historyGet( &h, 22000, 15000 ) == 2 )
     46    check( (int)tr_historyGet( &h, 22000, 20000 ) == 2 )
     47    tr_historyDestruct( &h );
    4848
    4949    return 0;
  • trunk/libtransmission/history.c

    r11709 r12013  
    1616#include "history.h"
    1717#include "utils.h"
    18 
    19 struct history_slice
    20 {
    21     unsigned int n;
    22     time_t date;
    23 };
    24 
    25 struct tr_recentHistory
    26 {
    27     int newest;
    28     int sliceCount;
    29     unsigned int precision;
    30     struct history_slice * slices;
    31 };
    3218
    3319void
     
    6450}
    6551
    66 tr_recentHistory *
    67 tr_historyNew( unsigned int seconds, unsigned int precision )
     52void
     53tr_historyConstruct( tr_recentHistory * h, unsigned int seconds, unsigned int precision )
    6854{
    69     tr_recentHistory * h;
     55    memset( h, 0, sizeof( tr_recentHistory ) );
    7056
    7157    assert( precision <= seconds );
    7258
    73     h = tr_new0( tr_recentHistory, 1 );
    7459    h->precision = precision;
    7560    h->sliceCount = seconds / precision;
    76     h->slices = tr_new0( struct history_slice, h->sliceCount );
    77 
    78     return h;
     61    h->slices = tr_new0( struct tr_history_slice, h->sliceCount );
    7962}
    8063
    8164void
    82 tr_historyFree( tr_recentHistory * h )
     65tr_historyDestruct( tr_recentHistory * h )
    8366{
    8467    tr_free( h->slices );
    85     tr_free( h );
    8668}
  • trunk/libtransmission/history.h

    r11709 r12013  
    2525 * to estimate the speed over the last N seconds.
    2626 */
    27 typedef struct tr_recentHistory tr_recentHistory;
     27
     28struct tr_history_slice
     29{
     30    unsigned int n;
     31    time_t date;
     32};
     33typedef struct tr_recentHistory
     34{
     35    /* these are PRIVATE IMPLEMENTATION details included for composition only.
     36     * Don't access these directly! */
     37    int newest;
     38    int sliceCount;
     39    unsigned int precision;
     40    struct tr_history_slice * slices;
     41}
     42tr_recentHistory;
    2843
    2944/**
    30  * @brief create a new tr_recentHistory object.
     45 * @brief construct a new tr_recentHistory object
    3146 * @param seconds how many seconds of history this object should remember
    3247 * @param precision how precise the history should be, in seconds
    3348 *        For a precision of 10 seconds and a history of 2 minutes, makes 12 bins.
    3449 */
    35 tr_recentHistory * tr_historyNew( unsigned int seconds, unsigned int precision );
     50void tr_historyConstruct( tr_recentHistory *, unsigned int seconds, unsigned int precision );
    3651
    37 /** @brief destroy an existing tr_recentHistory object. */
    38 void tr_historyFree( tr_recentHistory * );
     52/** @brief destruct an existing tr_recentHistory object. */
     53void tr_historyDestruct( tr_recentHistory * );
    3954
    4055/**
  • trunk/libtransmission/peer-mgr.c

    r12012 r12013  
    389389    atom->peer = peer;
    390390
    391     peer->blocksSentToClient  = tr_historyNew( CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    392     peer->blocksSentToPeer    = tr_historyNew( CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    393     peer->cancelsSentToClient = tr_historyNew( CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    394     peer->cancelsSentToPeer   = tr_historyNew( CANCEL_HISTORY_SEC, ( REFILL_UPKEEP_PERIOD_MSEC / 1000 ) );
     391    tr_historyConstruct( &peer->blocksSentToClient, CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
     392    tr_historyConstruct( &peer->blocksSentToPeer,    CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
     393    tr_historyConstruct( &peer->cancelsSentToClient, CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
     394    tr_historyConstruct( &peer->cancelsSentToPeer,  CANCEL_HISTORY_SEC, ( REFILL_UPKEEP_PERIOD_MSEC / 1000 ) );
    395395
    396396    return peer;
     
    430430    tr_peerIoUnref( peer->io ); /* balanced by the ref in handshakeDoneCB() */
    431431
    432     tr_historyFree( peer->blocksSentToClient  );
    433     tr_historyFree( peer->blocksSentToPeer    );
    434     tr_historyFree( peer->cancelsSentToClient );
    435     tr_historyFree( peer->cancelsSentToPeer   );
     432    tr_historyDestruct( &peer->blocksSentToClient  );
     433    tr_historyDestruct( &peer->blocksSentToPeer    );
     434    tr_historyDestruct( &peer->cancelsSentToClient );
     435    tr_historyDestruct( &peer->cancelsSentToPeer   );
    436436
    437437    tr_bitsetDestruct( &peer->have );
     
    14751475            for( it=cancel, end=it+cancelCount; it!=end; ++it ) {
    14761476                if( ( it->peer != NULL ) && ( it->peer->msgs != NULL ) ) {
    1477                     tr_historyAdd( it->peer->cancelsSentToPeer, now, 1 );
     1477                    tr_historyAdd( &it->peer->cancelsSentToPeer, now, 1 );
    14781478                    tr_peerMsgsCancel( it->peer->msgs, it->block );
    14791479                    decrementPendingReqCount( it );
     
    17311731                assert( p != peer );
    17321732                if( p->msgs ) {
    1733                     tr_historyAdd( p->cancelsSentToPeer, tr_time( ), 1 );
     1733                    tr_historyAdd( &p->cancelsSentToPeer, tr_time( ), 1 );
    17341734                    tr_peerMsgsCancel( p->msgs, block );
    17351735                }
     
    17391739            tr_ptrArrayDestruct( &peerArr, FALSE );
    17401740
    1741             if( peer && peer->blocksSentToClient )
    1742                 tr_historyAdd( peer->blocksSentToClient, tr_time( ), 1 );
     1741            if( peer )
     1742                tr_historyAdd( &peer->blocksSentToClient, tr_time( ), 1 );
    17431743
    17441744            if( tr_cpBlockIsComplete( &tor->completion, block ) )
     
    26432643        stat->isSeed              = ( atom->uploadOnly == UPLOAD_ONLY_YES ) || ( peer->progress >= 1.0 );
    26442644
    2645         stat->blocksToPeer        = tr_historyGet( peer->blocksSentToPeer,    now, CANCEL_HISTORY_SEC );
    2646         stat->blocksToClient      = tr_historyGet( peer->blocksSentToClient,  now, CANCEL_HISTORY_SEC );
    2647         stat->cancelsToPeer       = tr_historyGet( peer->cancelsSentToPeer,   now, CANCEL_HISTORY_SEC );
    2648         stat->cancelsToClient     = tr_historyGet( peer->cancelsSentToClient, now, CANCEL_HISTORY_SEC );
     2645        stat->blocksToPeer        = tr_historyGet( &peer->blocksSentToPeer,    now, CANCEL_HISTORY_SEC );
     2646        stat->blocksToClient      = tr_historyGet( &peer->blocksSentToClient,  now, CANCEL_HISTORY_SEC );
     2647        stat->cancelsToPeer       = tr_historyGet( &peer->cancelsSentToPeer,   now, CANCEL_HISTORY_SEC );
     2648        stat->cancelsToClient     = tr_historyGet( &peer->cancelsSentToClient, now, CANCEL_HISTORY_SEC );
    26492649
    26502650        stat->pendingReqsToPeer   = peer->pendingReqsToPeer;
     
    27612761        {
    27622762            const tr_peer * peer = tr_ptrArrayNth( &t->peers, i );
    2763             const int b = tr_historyGet( peer->blocksSentToClient, now, CANCEL_HISTORY_SEC );
    2764             const int c = tr_historyGet( peer->cancelsSentToPeer, now, CANCEL_HISTORY_SEC );
     2763            const int b = tr_historyGet( &peer->blocksSentToClient, now, CANCEL_HISTORY_SEC );
     2764            const int c = tr_historyGet( &peer->cancelsSentToPeer, now, CANCEL_HISTORY_SEC );
    27652765
    27662766            if( b == 0 ) /* ignore unresponsive peers, as described above */
     
    28252825            else
    28262826            {
    2827                 const int blocks = tr_historyGet( peer->blocksSentToClient, now, CANCEL_HISTORY_SEC );
    2828                 const int cancels = tr_historyGet( peer->cancelsSentToPeer, now, CANCEL_HISTORY_SEC );
     2827                const int blocks = tr_historyGet( &peer->blocksSentToClient, now, CANCEL_HISTORY_SEC );
     2828                const int cancels = tr_historyGet( &peer->cancelsSentToPeer, now, CANCEL_HISTORY_SEC );
    28292829
    28302830                if( !blocks && !cancels )
  • trunk/libtransmission/peer-mgr.h

    r11950 r12013  
    123123    time_t                   chokeChangedAt;
    124124
    125     tr_recentHistory       * blocksSentToClient;
    126     tr_recentHistory       * blocksSentToPeer;
    127 
    128     tr_recentHistory       * cancelsSentToClient;
    129     tr_recentHistory       * cancelsSentToPeer;
     125    tr_recentHistory         blocksSentToClient;
     126    tr_recentHistory         blocksSentToPeer;
     127
     128    tr_recentHistory         cancelsSentToClient;
     129    tr_recentHistory         cancelsSentToPeer;
    130130
    131131    struct tr_peermsgs     * msgs;
  • trunk/libtransmission/peer-msgs.c

    r12012 r12013  
    14771477            tr_peerIoReadUint32( msgs->peer->io, inbuf, &r.offset );
    14781478            tr_peerIoReadUint32( msgs->peer->io, inbuf, &r.length );
    1479             tr_historyAdd( msgs->peer->cancelsSentToClient, tr_time( ), 1 );
     1479            tr_historyAdd( &msgs->peer->cancelsSentToClient, tr_time( ), 1 );
    14801480            dbgmsg( msgs, "got a Cancel %u:%u->%u", r.index, r.offset, r.length );
    14811481
     
    19591959                bytesWritten += n;
    19601960                msgs->clientSentAnythingAt = now;
    1961                 tr_historyAdd( msgs->peer->blocksSentToPeer, tr_time( ), 1 );
     1961                tr_historyAdd( &msgs->peer->blocksSentToPeer, tr_time( ), 1 );
    19621962            }
    19631963
Note: See TracChangeset for help on using the changeset viewer.