speed improvements to tr_torrentStat() by folding two expensive & redundant functions together.

This commit is contained in:
Charles Kerr 2007-07-27 01:22:49 +00:00
parent a0098b4321
commit 8f4fb4efbe
3 changed files with 32 additions and 31 deletions

View File

@ -270,31 +270,37 @@ tr_cpLeftUntilComplete ( const tr_completion_t * cp )
return b;
}
uint64_t
tr_cpLeftUntilDone ( const tr_completion_t * cp )
void
tr_cpDoneStats( const tr_completion_t * cp ,
uint64_t * setmeHaveBytes,
uint64_t * setmeTotalBytes )
{
const tr_torrent_t * tor = cp->tor;
const tr_info_t * info = &tor->info;
uint64_t have=0, total=0;
int i;
uint64_t b=0;
const tr_torrent_t * tor;
const tr_info_t * info;
assert( cp != NULL );
assert( cp->tor != NULL );
for( i=0; i<info->pieceCount; ++i ) {
if( !info->pieces[i].dnd ) {
total += info->pieceSize;
have += cp->completeBlocks[ i ];
}
}
tor = cp->tor;
info = &tor->info;
have *= tor->blockSize;
for( i=0; i<info->pieceCount; ++i )
if( !tr_cpPieceIsComplete( cp, i ) && !info->pieces[i].dnd )
b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
/* the last piece/block is probably smaller than the others */
if( !info->pieces[info->pieceCount-1].dnd ) {
total -= ( info->pieceSize - ( info->totalSize % info->pieceSize ) );
if( tr_cpBlockIsComplete( cp, tor->blockCount-1 ) )
have -= ( tor->blockSize - ( info->totalSize % tor->blockSize ) );
}
b *= tor->blockSize;
assert( have < total );
assert( total <= info->totalSize );
if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount-1 )
&& !info->pieces[info->pieceCount-1].dnd )
b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
return b;
*setmeHaveBytes = have;
*setmeTotalBytes = total;
}
float
@ -306,15 +312,6 @@ tr_cpPercentComplete ( const tr_completion_t * cp )
return MAX(0.0, f);
}
float
tr_cpPercentDone( const tr_completion_t * cp )
{
const uint64_t tilDone = tr_cpLeftUntilDone( cp );
const uint64_t total = cp->tor->info.totalSize;
const float f = 1.0 - (double)tilDone / total;
return MAX(0.0, f);
}
uint64_t
tr_cpDownloadedValid( const tr_completion_t * cp )
{

View File

@ -37,9 +37,11 @@ cp_status_t tr_cpGetStatus ( const tr_completion_t * );
uint64_t tr_cpDownloadedValid( const tr_completion_t * );
uint64_t tr_cpLeftUntilComplete( const tr_completion_t * );
uint64_t tr_cpLeftUntilDone( const tr_completion_t * );
float tr_cpPercentComplete( const tr_completion_t * );
float tr_cpPercentDone( const tr_completion_t * );
void tr_cpDoneStats( const tr_completion_t * cp,
uint64_t * setmeHaveBytes,
uint64_t * setmeTotalBytes );
/* Pieces */
int tr_cpPieceHasAllBlocks( const tr_completion_t *, int piece );

View File

@ -596,6 +596,7 @@ tr_torrentStat( tr_torrent_t * tor )
tr_stat_t * s;
tr_tracker_t * tc;
int i;
uint64_t doneHave, doneTotal;
tr_torrentReaderLock( tor );
@ -636,10 +637,11 @@ tr_torrentStat( tr_torrent_t * tor )
}
}
s->percentDone = tr_cpPercentDone ( tor->completion );
s->percentComplete = tr_cpPercentComplete ( tor->completion );
s->left = tr_cpLeftUntilDone ( tor->completion );
tr_cpDoneStats( tor->completion, &doneHave, &doneTotal );
s->percentDone = (float)doneHave / (float)doneTotal;
s->left = doneTotal - doneHave;
if( tor->uncheckedPieces )
s->status = tor->runStatus==TR_RUN_CHECKING