Changeset 7050


Ignore:
Timestamp:
Nov 5, 2008, 4:50:03 AM (13 years ago)
Author:
charles
Message:

(libT) optimize tr_cpHaveValid(), which according to cachegrind is the biggest remaining hotspot function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r7027 r7050  
    3434{
    3535    unsigned int    sizeWhenDoneIsDirty : 1;
     36    unsigned int    haveValidIsDirty : 1;
    3637
    3738    tr_torrent *    tor;
     
    5051       use tr_cpSizeWhenDone() instead! */
    5152    uint64_t    sizeWhenDoneLazy;
     53
     54    /* number of bytes we'll have when done downloading. [0..info.totalSize]
     55       DON'T access this directly; it's a lazy field.
     56       use tr_cpHaveValid() instead! */
     57    uint64_t    haveValidLazy;
    5258
    5359    /* number of bytes we want or have now. [0..sizeWhenDone] */
     
    6470    cp->sizeNow = 0;
    6571    cp->sizeWhenDoneIsDirty = 1;
     72    cp->haveValidIsDirty = 1;
    6673}
    6774
     
    143150                      tr_piece_index_t      piece )
    144151{
    145     return cp->completeBlocks[piece] == tr_torPieceCountBlocks( cp->tor,
    146                                                                 piece );
     152    return cp->completeBlocks[piece] == tr_torPieceCountBlocks( cp->tor, piece );
    147153}
    148154
     
    186192
    187193    cp->sizeWhenDoneIsDirty = 1;
     194    cp->haveValidIsDirty = 1;
    188195    cp->completeBlocks[piece] = 0;
    189196    tr_bitfieldRemRange ( cp->blockBitfield, start, end );
     
    212219        ++cp->completeBlocks[piece];
    213220
    214         if( cp->completeBlocks[piece] == tr_torPieceCountBlocks( tor, piece ) )
     221        if( tr_cpPieceIsComplete( cp, piece ) )
    215222            tr_bitfieldAdd( cp->pieceBitfield, piece );
    216223
     
    219226        cp->sizeNow += blockSize;
    220227
     228        cp->haveValidIsDirty = 1;
    221229        cp->sizeWhenDoneIsDirty = 1;
    222230    }
     
    261269                           tr_piece_index_t      piece )
    262270{
    263     return tr_torPieceCountBlocks( cp->tor,
    264                                    piece ) - cp->completeBlocks[piece];
     271    return tr_torPieceCountBlocks( cp->tor, piece ) - cp->completeBlocks[piece];
    265272}
    266273
     
    301308}
    302309
    303 uint64_t
    304 tr_cpHaveValid( const tr_completion * cp )
     310static uint64_t
     311calculateHaveValid( const tr_completion * ccp )
    305312{
    306313    uint64_t                  b = 0;
    307314    tr_piece_index_t          i;
    308     const tr_torrent        * tor            = cp->tor;
     315    const tr_torrent        * tor            = ccp->tor;
    309316    const uint64_t            pieceSize      = tor->info.pieceSize;
    310317    const uint64_t            lastPieceSize  = tor->lastPieceSize;
     
    312319
    313320    for( i=0; i!=lastPiece; ++i )
    314         if( tr_cpPieceIsComplete( cp, i ) )
     321        if( tr_cpPieceIsComplete( ccp, i ) )
    315322            b += pieceSize;
    316323
    317     if( tr_cpPieceIsComplete( cp, lastPiece ) )
     324    if( tr_cpPieceIsComplete( ccp, lastPiece ) )
    318325        b += lastPieceSize;
    319326
    320327    return b;
     328}
     329
     330uint64_t
     331tr_cpHaveValid( const tr_completion * ccp )
     332{
     333    if( ccp->haveValidIsDirty )
     334    {
     335        tr_completion * cp = (tr_completion *) ccp; /* mutable */
     336        cp->haveValidLazy = calculateHaveValid( ccp );
     337        cp->haveValidIsDirty = 0;
     338    }
     339
     340    return ccp->haveValidLazy;
    321341}
    322342
Note: See TracChangeset for help on using the changeset viewer.