Ignore:
Timestamp:
Jun 30, 2007, 9:11:17 PM (14 years ago)
Author:
charles
Message:

don't count too many bytes in the last block in tr_cpDownloadedValid(), tr_cpLeftUntilDone(), tr_cpLeftUntilComplete(). Very minor speedups in tr_bitfieldHas(), tr_bitfieldAdd(), tr_bitfieldRem().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r2242 r2252  
    2828struct tr_completion_s
    2929{
    30     tr_torrent_t      * tor;
    31     tr_bitfield_t     * blockBitfield;
    32     uint8_t           * blockDownloaders;
    33     tr_bitfield_t     * pieceBitfield;
     30    tr_torrent_t * tor;
     31
     32    /* number of peers from whom we've requested this block */
     33    uint8_t * blockDownloaders;
     34
     35    /* do we have this block? */
     36    tr_bitfield_t * blockBitfield;
     37
     38    /* do we have this piece? */
     39    tr_bitfield_t * pieceBitfield;
    3440
    3541    /* a block is complete if and only if we have it */
     
    5763    cp->completeBlocks   = tr_new( int, tor->info.pieceCount );
    5864
    59     cp->nBlocksInLastPiece = tr_pieceCountBlocks ( tor->info.pieceCount - 1 );
     65    cp->nBlocksInLastPiece = tr_pieceCountBlocks( tor->info.pieceCount - 1 );
    6066    cp->nBlocksInPiece = tor->info.pieceCount==1 ? cp->nBlocksInLastPiece
    61                                                 : tr_pieceCountBlocks( 0 );
     67                                                 : tr_pieceCountBlocks( 0 );
    6268
    6369    tr_cpReset( cp );
     
    94100int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece )
    95101{
    96     const int total = tr_cpCountBlocks( cp, piece );
    97     const int have = cp->completeBlocks[piece];
    98     assert( have <= total );
    99     return have == total;
     102    return tr_bitfieldHas( cp->pieceBitfield, piece );
    100103}
    101104
     
    318321            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
    319322
    320     return b * tor->blockSize;;
     323    b *= tor->blockSize;
     324
     325    if( !tr_cpBlockIsComplete( cp, tor->blockCount - 1 ) )
     326        b -= (tor->info.totalSize % tor->blockSize);
     327
     328    return b;
    321329}
    322330
     
    338346        if( !tr_cpPieceIsComplete( cp, i ) && info->pieces[i].priority != TR_PRI_DND )
    339347            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
     348
     349    b *= tor->blockSize;
     350
     351    i = tor->blockCount - 1;
     352    if( !tr_cpBlockIsComplete( cp, tor->blockCount-1 )
     353                  && info->pieces[info->pieceCount-1].priority != TR_PRI_DND )
     354        b -= (tor->info.totalSize % tor->blockSize);
    340355
    341356    return b * tor->blockSize;
     
    363378tr_cpDownloadedValid( const tr_completion_t * cp )
    364379{
    365     int i, n;
    366     uint64_t b=0;
    367 
    368     for( i=0, n=cp->tor->info.pieceCount; i<n; ++i )
    369         b += cp->completeBlocks[ i ];
    370 
    371    return b * cp->tor->blockSize;
    372 }
     380    const tr_torrent_t * tor = cp->tor;
     381
     382    uint64_t b = tr_bitfieldCountTrueBits( cp->blockBitfield ) * tor->blockSize;
     383
     384    if( tr_bitfieldHas( cp->blockBitfield, tor->blockCount - 1 ) )
     385        b -= (tor->info.totalSize % tor->blockSize);
     386
     387   return b;
     388}
Note: See TracChangeset for help on using the changeset viewer.