Changeset 93


Ignore:
Timestamp:
Feb 7, 2006, 3:10:42 AM (15 years ago)
Author:
titer
Message:

Cleaning and minor optims

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/choking.c

    r90 r93  
    5050    else
    5151        /* Reckon a number of slots from the upload limit. There is no
    52            official right way to do this, the formula above e.g. gives:
     52           official right way to do this, the formula below e.g. gives:
    5353            10  KB/s -> 4  * 2.50 KB/s
    5454            20  KB/s -> 6  * 3.33 KB/s
     
    5959}
    6060
    61 static inline void sortPeers( tr_peer_t ** peers, int count )
    62 {
    63     int i, j;
     61#define sortPeersAscending(p,c)  sortPeers(p,c,0)
     62#define sortPeersDescending(p,c) sortPeers(p,c,1)
     63static inline void sortPeers( tr_peer_t ** peers, int count, int order )
     64{
     65    int i, j, sorted;
     66    float rate1, rate2;
    6467    tr_peer_t * tmp;
    6568
    6669    for( i = count - 1; i > 0; i-- )
     70    {
     71        sorted = 1;
    6772        for( j = 0; j < i; j++ )
    6873        {
    69             if( tr_peerDownloadRate( peers[j] ) >
    70                     tr_peerDownloadRate( peers[j+1] ) )
     74            rate1 = tr_peerDownloadRate( peers[j] );
     75            rate2 = tr_peerDownloadRate( peers[j+1] );
     76            if( order ? ( rate1 < rate2 ) : ( rate1 > rate2 ) )
    7177            {
    7278                tmp        = peers[j];
    7379                peers[j]   = peers[j+1];
    7480                peers[j+1] = tmp;
     81                sorted     = 0;
    7582            }
    7683        }
     84        if( sorted )
     85            break;
     86    }
    7787}
    7888
     
    140150
    141151    /* Sort peers by the rate we are downloading from them. */
    142     sortPeers( peersCanChoke, peersCanChokeCount );
    143     sortPeers( peersCanUnchoke, peersCanUnchokeCount );
     152    sortPeersDescending( peersCanChoke, peersCanChokeCount );
     153    sortPeersAscending( peersCanUnchoke, peersCanUnchokeCount );
    144154
    145155    if( unchokedCount > c->slots && peersCanChokeCount > 0 )
    146156    {
    147157        /* We have more open slots than what we should have (the user
    148            has just lower his upload limit. We need to choke some of the
     158           has just lowered his upload limit. We need to choke some of the
    149159           peers we are uploading to. */
    150160        int willChoke;
    151161        willChoke = MIN( peersCanChokeCount, unchokedCount - c->slots );
    152162        for( i = 0; i < willChoke; i++ )
    153             tr_peerChoke( peersCanChoke[i] );
    154         peersCanChokeCount -= willChoke;
    155         memmove( &peersCanChoke[0], &peersCanChoke[willChoke],
    156                  peersCanChokeCount * sizeof( tr_peer_t * ) );
     163            tr_peerChoke( peersCanChoke[--peersCanChokeCount] );
    157164    }
    158165    else if( unchokedCount < c->slots && peersCanUnchokeCount > 0 )
     
    162169        willUnchoke = MIN( peersCanUnchokeCount, c->slots - unchokedCount );
    163170        for( i = 0; i < willUnchoke; i++ )
    164             tr_peerUnchoke( peersCanUnchoke[peersCanUnchokeCount - i - 1] );
    165         peersCanUnchokeCount -= willUnchoke;
     171            tr_peerUnchoke( peersCanUnchoke[--peersCanUnchokeCount] );
    166172    }
    167173
     
    172178           uploading more to us. */
    173179        if( tr_peerDownloadRate( peersCanUnchoke[peersCanUnchokeCount - 1] )
    174                 < tr_peerDownloadRate( peersCanChoke[0] ) )
     180                < tr_peerDownloadRate( peersCanChoke[peersCanChokeCount - 1] ) )
    175181            break;
    176182
    177         tr_peerChoke( peersCanChoke[0] );
    178         tr_peerUnchoke( peersCanUnchoke[peersCanUnchokeCount - 1] );
    179         peersCanChokeCount--;
    180         peersCanUnchokeCount--;
    181         memmove( &peersCanChoke[0], &peersCanChoke[1],
    182                  peersCanChokeCount * sizeof( tr_peer_t * ) );
     183        tr_peerChoke( peersCanChoke[--peersCanChokeCount] );
     184        tr_peerUnchoke( peersCanUnchoke[--peersCanUnchokeCount] );
    183185    }
    184186
Note: See TracChangeset for help on using the changeset viewer.