Changeset 12328


Ignore:
Timestamp:
Apr 6, 2011, 11:27:11 PM (11 years ago)
Author:
jordan
Message:

(trunk libT) more heap pruning: avoid four unnecessary malloc() + free() calls per tr_peer.

This commit also changes tr_recentHistory from being a general-purpose tool to being a little more hardcoded for the only purpose it's used, in tr_peerMgr. If its files (history.[ch]) don't find any other "customers" in libtransmission, eventually it should be demoted to being a private helper class inside of peer-mgr.c and have the history.[ch] files removed from the build.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

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

    r12013 r12328  
    11#include <stdio.h>
     2#include <string.h> /* memset() */
    23
    34#include "transmission.h"
     
    3536    tr_recentHistory h;
    3637
    37     tr_historyConstruct( &h, 60, 10 );
     38    memset( &h, 0, sizeof( tr_recentHistory ) );
     39
    3840    tr_historyAdd( &h, 10000, 1 );
    3941    check( (int)tr_historyGet( &h, 12000, 1000 ) == 0 )
     
    4547    check( (int)tr_historyGet( &h, 22000, 15000 ) == 2 )
    4648    check( (int)tr_historyGet( &h, 22000, 20000 ) == 2 )
    47     tr_historyDestruct( &h );
    4849
    4950    return 0;
  • trunk/libtransmission/history.c

    r12177 r12328  
    2121tr_historyAdd( tr_recentHistory * h, time_t now, unsigned int n )
    2222{
    23     if( h->slices[h->newest].date + (time_t)h->precision >= now )
     23    if( h->slices[h->newest].date == now )
    2424        h->slices[h->newest].n += n;
    2525    else {
    26         if( ++h->newest == h->sliceCount ) h->newest = 0;
     26        if( ++h->newest == TR_RECENT_HISTORY_PERIOD_SEC ) h->newest = 0;
    2727        h->slices[h->newest].date = now;
    2828        h->slices[h->newest].n = n;
     
    4444        n += h->slices[i].n;
    4545
    46         if( --i == -1 ) i = h->sliceCount - 1; /* circular history */
     46        if( --i == -1 ) i = TR_RECENT_HISTORY_PERIOD_SEC - 1; /* circular history */
    4747        if( i == h->newest ) break; /* we've come all the way around */
    4848    }
     
    5050    return n;
    5151}
    52 
    53 void
    54 tr_historyConstruct( tr_recentHistory * h, unsigned int seconds, unsigned int precision )
    55 {
    56     memset( h, 0, sizeof( tr_recentHistory ) );
    57 
    58     assert( precision <= seconds );
    59 
    60     h->precision = precision;
    61     h->sliceCount = seconds / precision;
    62     h->slices = tr_new0( struct tr_history_slice, h->sliceCount );
    63 }
    64 
    65 void
    66 tr_historyDestruct( tr_recentHistory * h )
    67 {
    68     tr_free( h->slices );
    69 }
  • trunk/libtransmission/history.h

    r12013 r12328  
    2626 */
    2727
    28 struct tr_history_slice
     28enum
    2929{
    30     unsigned int n;
    31     time_t date;
     30    TR_RECENT_HISTORY_PERIOD_SEC = 60
    3231};
     32
     33
    3334typedef struct tr_recentHistory
    3435{
    3536    /* these are PRIVATE IMPLEMENTATION details included for composition only.
    3637     * Don't access these directly! */
     38
    3739    int newest;
    38     int sliceCount;
    39     unsigned int precision;
    40     struct tr_history_slice * slices;
     40
     41    struct {
     42        unsigned int n;
     43        time_t date;
     44    } slices[TR_RECENT_HISTORY_PERIOD_SEC];
    4145}
    4246tr_recentHistory;
    43 
    44 /**
    45  * @brief construct a new tr_recentHistory object
    46  * @param seconds how many seconds of history this object should remember
    47  * @param precision how precise the history should be, in seconds
    48  *        For a precision of 10 seconds and a history of 2 minutes, makes 12 bins.
    49  */
    50 void tr_historyConstruct( tr_recentHistory *, unsigned int seconds, unsigned int precision );
    51 
    52 /** @brief destruct an existing tr_recentHistory object. */
    53 void tr_historyDestruct( tr_recentHistory * );
    5447
    5548/**
  • trunk/libtransmission/peer-mgr.c

    r12308 r12328  
    376376
    377377    peer->have = TR_BITFIELD_INIT;
    378 
    379     tr_historyConstruct( &peer->blocksSentToClient,  CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    380     tr_historyConstruct( &peer->blocksSentToPeer,    CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    381     tr_historyConstruct( &peer->cancelsSentToClient, CANCEL_HISTORY_SEC, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    382     tr_historyConstruct( &peer->cancelsSentToPeer,   CANCEL_HISTORY_SEC, ( REFILL_UPKEEP_PERIOD_MSEC / 1000 ) );
    383378}
    384379
     
    431426        tr_peerIoUnref( peer->io ); /* balanced by the ref in handshakeDoneCB() */
    432427    }
    433 
    434     tr_historyDestruct( &peer->blocksSentToClient  );
    435     tr_historyDestruct( &peer->blocksSentToPeer    );
    436     tr_historyDestruct( &peer->cancelsSentToClient );
    437     tr_historyDestruct( &peer->cancelsSentToPeer   );
    438428
    439429    tr_bitfieldDestruct( &peer->have );
Note: See TracChangeset for help on using the changeset viewer.