(trunk libT) CPU improvement in peer-mgr.c's getPeerCandidates()

getPeerCandiates() used to read through all the torrents to determine the global peer connections, and then again to determine the global peer candidates. Now this is done in one loop instead of two.
This commit is contained in:
Jordan Lee 2011-05-10 01:51:12 +00:00
parent 3c3fda5c66
commit 6a38a368d6
1 changed files with 13 additions and 11 deletions

View File

@ -3808,7 +3808,8 @@ comparePeerCandidates( const void * va, const void * vb )
static struct peer_candidate*
getPeerCandidates( tr_session * session, int * candidateCount )
{
int n;
int atomCount;
int peerCount;
tr_torrent * tor;
struct peer_candidate * candidates;
struct peer_candidate * walk;
@ -3817,22 +3818,23 @@ getPeerCandidates( tr_session * session, int * candidateCount )
/* leave 5% of connection slots for incoming connections -- ticket #2609 */
const int maxCandidates = tr_sessionGetPeerLimit( session ) * 0.95;
/* don't start any new handshakes if we're full up */
n = 0;
/* count how many peers and atoms we've got */
tor= NULL;
while(( tor = tr_torrentNext( session, tor )))
n += tr_ptrArraySize( &tor->torrentPeers->peers );
if( maxCandidates <= n ) {
atomCount = 0;
peerCount = 0;
while(( tor = tr_torrentNext( session, tor ))) {
atomCount += tr_ptrArraySize( &tor->torrentPeers->pool );
peerCount += tr_ptrArraySize( &tor->torrentPeers->peers );
}
/* don't start any new handshakes if we're full up */
if( maxCandidates <= peerCount ) {
*candidateCount = 0;
return NULL;
}
/* allocate an array of candidates */
n = 0;
tor= NULL;
while(( tor = tr_torrentNext( session, tor )))
n += tr_ptrArraySize( &tor->torrentPeers->pool );
walk = candidates = tr_new( struct peer_candidate, n );
walk = candidates = tr_new( struct peer_candidate, atomCount );
/* populate the candidate array */
tor = NULL;