Changeset 7628


Ignore:
Timestamp:
Jan 6, 2009, 7:45:15 AM (12 years ago)
Author:
charles
Message:

(trunk libT) faster tr_cpBlockBitfieldSet()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r7578 r7628  
    3636    tr_bitfieldClear( &cp->pieceBitfield );
    3737    tr_bitfieldClear( &cp->blockBitfield );
    38     memset( cp->completeBlocks, 0,
    39             sizeof( uint16_t ) * cp->tor->info.pieceCount );
     38    memset( cp->completeBlocks, 0, sizeof( uint16_t ) * cp->tor->info.pieceCount );
    4039    cp->sizeNow = 0;
    4140    cp->sizeWhenDoneIsDirty = 1;
     
    179178}
    180179
     180#if 0
    181181int
    182182tr_cpBlockBitfieldSet( tr_completion * cp,
     
    196196                tr_cpBlockAdd( cp, i );
    197197        success = TRUE;
     198    }
     199
     200    return success;
     201}
     202#endif
     203
     204int
     205tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * blockBitfield )
     206{
     207    int success = FALSE;
     208
     209    assert( cp );
     210    assert( blockBitfield );
     211
     212    if(( success = tr_bitfieldTestFast( blockBitfield, cp->tor->blockCount - 1 )))
     213    {
     214        tr_piece_index_t p;
     215        const tr_torrent * tor = cp->tor;
     216
     217        tr_cpReset( cp );
     218
     219        for( p=0; p<tor->info.pieceCount; ++p )
     220        {
     221            tr_block_index_t i;
     222            uint16_t completeBlocksInPiece = 0;
     223
     224            const tr_block_index_t start = tr_torPieceFirstBlock( tor, p );
     225            const tr_block_index_t end = start + tr_torPieceCountBlocks( tor, p );
     226
     227            for( i=start; i!=end; ++i ) {
     228                if( tr_bitfieldTestFast( blockBitfield, i ) ) {
     229                    ++completeBlocksInPiece;
     230                    cp->sizeNow += tr_torBlockCountBytes( tor, i );
     231                }
     232            }
     233
     234            cp->completeBlocks[p] = completeBlocksInPiece;
     235
     236            if( completeBlocksInPiece == end - start )
     237                tr_bitfieldAdd( &cp->pieceBitfield, p );
     238        }
     239
     240        memcpy( cp->blockBitfield->bits, blockBitfield->bits, cp->blockBitfield->byteCount );
     241
     242        cp->haveValidIsDirty = 1;
     243        cp->sizeWhenDoneIsDirty = 1;
    198244    }
    199245
Note: See TracChangeset for help on using the changeset viewer.