Ignore:
Timestamp:
Jul 27, 2007, 1:22:49 AM (14 years ago)
Author:
charles
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r2506 r2509  
    271271}
    272272
    273 uint64_t
    274 tr_cpLeftUntilDone ( const tr_completion_t * cp )
    275 {
    276     int i;
    277     uint64_t b=0;
    278     const tr_torrent_t * tor;
    279     const tr_info_t * info;
    280 
    281     assert( cp != NULL );
    282     assert( cp->tor != NULL );
    283 
    284     tor = cp->tor;
    285     info = &tor->info;
    286 
    287     for( i=0; i<info->pieceCount; ++i )
    288         if( !tr_cpPieceIsComplete( cp, i ) && !info->pieces[i].dnd )
    289             b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
    290 
    291     b *= tor->blockSize;
    292 
    293     if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount-1 )
    294                         && !info->pieces[info->pieceCount-1].dnd )
    295           b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
    296 
    297     return b;
     273void
     274tr_cpDoneStats( const tr_completion_t  * cp ,
     275                uint64_t               * setmeHaveBytes,
     276                uint64_t               * setmeTotalBytes )
     277{
     278    const tr_torrent_t * tor = cp->tor;
     279    const tr_info_t * info = &tor->info;
     280    uint64_t have=0, total=0;
     281    int i;
     282
     283    for( i=0; i<info->pieceCount; ++i ) {
     284        if( !info->pieces[i].dnd ) {
     285            total += info->pieceSize;
     286            have += cp->completeBlocks[ i ];
     287        }
     288    }
     289
     290    have *= tor->blockSize;
     291
     292    /* the last piece/block is probably smaller than the others */
     293    if( !info->pieces[info->pieceCount-1].dnd ) {
     294        total -= ( info->pieceSize - ( info->totalSize % info->pieceSize ) );
     295        if( tr_cpBlockIsComplete( cp, tor->blockCount-1 ) )
     296            have -= ( tor->blockSize - ( info->totalSize % tor->blockSize ) );
     297    }
     298
     299    assert( have < total );
     300    assert( total <= info->totalSize );
     301
     302    *setmeHaveBytes = have;
     303    *setmeTotalBytes = total;
    298304}
    299305
     
    307313}
    308314
    309 float
    310 tr_cpPercentDone( const tr_completion_t * cp )
    311 {
    312     const uint64_t tilDone = tr_cpLeftUntilDone( cp );
    313     const uint64_t total = cp->tor->info.totalSize;
    314     const float f = 1.0 - (double)tilDone / total;
    315     return MAX(0.0, f);
    316 }
    317 
    318315uint64_t
    319316tr_cpDownloadedValid( const tr_completion_t * cp )
Note: See TracChangeset for help on using the changeset viewer.