Ignore:
Timestamp:
Jan 31, 2011, 4:43:37 PM (11 years ago)
Author:
jordan
Message:

(trunk libt) #3968 "tr_cpFileIsComplete could be faster" -- fixed.

Rewriting this function as described in ticket #3968. This rewrite drops it from 1.77% of cpu use to 0.07%.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r11709 r11797  
    346346    const tr_torrent * tor = cp->tor;
    347347    const tr_file * file = &tor->info.files[fileIndex];
    348     const tr_block_index_t firstBlock = file->offset / tor->blockSize;
    349     const tr_block_index_t lastBlock = file->length ? ( ( file->offset + file->length - 1 ) / tor->blockSize ) : firstBlock;
    350 
    351     assert( tr_torBlockPiece( tor, firstBlock ) == file->firstPiece );
    352     assert( tr_torBlockPiece( tor, lastBlock ) == file->lastPiece );
    353 
    354     for( block=firstBlock; block<=lastBlock; ++block )
    355         if( !tr_cpBlockIsCompleteFast( cp, block ) )
    356             return FALSE;
     348
     349    if( file->firstPiece == file->lastPiece )
     350    {
     351        const tr_block_index_t firstBlock = file->offset / tor->blockSize;
     352        const tr_block_index_t lastBlock = file->length ? ( ( file->offset + file->length - 1 ) / tor->blockSize ) : firstBlock;
     353        for( block=firstBlock; block<=lastBlock; ++block )
     354            if( !tr_cpBlockIsCompleteFast( cp, block ) )
     355                return FALSE;
     356    }
     357    else
     358    {
     359        tr_piece_index_t piece;
     360        tr_block_index_t firstBlock;
     361        tr_block_index_t firstBlockInLastPiece;
     362        tr_block_index_t lastBlock;
     363        tr_block_index_t lastBlockInFirstPiece;
     364        uint64_t lastByteInFirstPiece;
     365        uint64_t firstByteInLastPiece;
     366
     367        /* go piece-by-piece in the middle pieces... it's faster than block-by-block */
     368        for( piece=file->firstPiece+1; piece<file->lastPiece; ++piece )
     369            if( !tr_cpPieceIsComplete( cp, piece ) )
     370                return FALSE;
     371
     372        /* go block-by-block in the first piece */
     373        firstBlock = file->offset / tor->blockSize;
     374        lastByteInFirstPiece = ( (uint64_t)(file->firstPiece+1) * tor->info.pieceSize ) - 1;
     375        lastBlockInFirstPiece = lastByteInFirstPiece / tor->blockSize;
     376        assert( lastBlockInFirstPiece >= firstBlock );
     377        assert( lastBlockInFirstPiece - firstBlock <= tr_torPieceCountBlocks( tor, file->firstPiece ) );
     378        for( block=firstBlock; block<=lastBlockInFirstPiece; ++block )
     379            if( !tr_cpBlockIsCompleteFast( cp, block ) )
     380                return FALSE;
     381
     382        /* go block-by-block in the last piece */
     383        lastBlock = file->length ? ( ( file->offset + file->length - 1 ) / tor->blockSize ) : firstBlock;
     384        firstByteInLastPiece = (uint64_t)file->lastPiece * tor->info.pieceSize;
     385        firstBlockInLastPiece = firstByteInLastPiece / tor->blockSize;
     386        assert( firstBlockInLastPiece <= lastBlock );
     387        assert( firstBlockInLastPiece >= firstBlock );
     388        assert( firstBlockInLastPiece >= lastBlockInFirstPiece );
     389        assert( lastBlock - firstBlockInLastPiece <= tr_torPieceCountBlocks( tor, file->lastPiece ) );
     390        for( block=firstBlockInLastPiece; block<=lastBlock; ++block )
     391            if( !tr_cpBlockIsCompleteFast( cp, block ) )
     392                return FALSE;
     393    }
    357394
    358395    return TRUE;
    359 }
     396
     397}
Note: See TracChangeset for help on using the changeset viewer.