Changeset 7559


Ignore:
Timestamp:
Dec 31, 2008, 6:08:13 PM (12 years ago)
Author:
charles
Message:

(trunk libT) improve piece verification speed by providing a larger buffer, and add a mechanism to reuse it when verifying an entire torrent at once.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/inout.c

    r7543 r7559  
    211211****/
    212212
    213 static int
     213static tr_bool
    214214recalculateHash( const tr_torrent * tor,
    215215                 tr_piece_index_t   pieceIndex,
     216                 void             * buffer,
     217                 size_t             buflen,
    216218                 uint8_t *          setme )
    217219{
    218220    size_t   bytesLeft;
    219221    uint32_t offset = 0;
    220     int      success = TRUE;
     222    tr_bool  success = TRUE;
     223    uint8_t  stackbuf[MAX_STACK_ARRAY_SIZE];
    221224    SHA_CTX  sha;
    222225
    223     assert( tor );
    224     assert( setme );
     226    /* fallback buffer */
     227    if( ( buffer == NULL ) || ( buflen < 1 ) )
     228    {
     229        buffer = stackbuf;
     230        buflen = sizeof( stackbuf );
     231    }
     232
     233    assert( tor != NULL );
    225234    assert( pieceIndex < tor->info.pieceCount );
     235    assert( buffer != NULL );
     236    assert( buflen > 0 );
     237    assert( setme != NULL );
    226238
    227239    SHA1_Init( &sha );
     
    230242    while( bytesLeft )
    231243    {
    232         uint8_t buf[MAX_STACK_ARRAY_SIZE];
    233         const int len = MIN( bytesLeft, sizeof( buf ) );
    234         success = !tr_ioRead( tor, pieceIndex, offset, len, buf );
     244        const int len = MIN( bytesLeft, buflen );
     245        success = !tr_ioRead( tor, pieceIndex, offset, len, buffer );
    235246        if( !success )
    236247            break;
    237         SHA1_Update( &sha, buf, len );
     248        SHA1_Update( &sha, buffer, len );
    238249        offset += len;
    239250        bytesLeft -= len;
     
    246257}
    247258
    248 int
    249 tr_ioTestPiece( const tr_torrent * tor,
    250                 int                pieceIndex )
     259tr_bool
     260tr_ioTestPiece( const tr_torrent  * tor,
     261                tr_piece_index_t    pieceIndex,
     262                void              * buffer,
     263                size_t              buflen )
    251264{
    252265    uint8_t hash[SHA_DIGEST_LENGTH];
    253     const int recalculated = recalculateHash( tor, pieceIndex, hash );
    254     return recalculated && !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
    255 }
    256 
     266
     267    return recalculateHash( tor, pieceIndex, buffer, buflen, hash )
     268           && !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
     269}
  • trunk/libtransmission/inout.h

    r7151 r7559  
    5353
    5454/**
    55  * returns nonzero if the piece matches its metainfo's SHA1 checksum.
     55 * @brief Test to see if the piece matches its metainfo's SHA1 checksum.
     56 *
     57 * @param optionalBuffer if calling tr_ioTestPiece() repeatedly, you can
     58 *                       get best performance by providing a buffer with
     59 *                       tor->info.pieceSize bytes.
    5660 */
    57 int tr_ioTestPiece( const tr_torrent*,
    58                     int   piece );
     61tr_bool tr_ioTestPiece( const tr_torrent * tor,
     62                        tr_piece_index_t   piece,
     63                        void             * optionalBuffer,
     64                        size_t             optionalBufferLen );
    5965
    6066
  • trunk/libtransmission/peer-mgr.c

    r7551 r7559  
    10901090            {
    10911091                const tr_piece_index_t p = e->pieceIndex;
    1092                 const tr_bool ok = tr_ioTestPiece( tor, p );
     1092                const tr_bool ok = tr_ioTestPiece( tor, p, NULL, 0 );
    10931093
    10941094                if( !ok )
  • trunk/libtransmission/verify.c

    r7404 r7559  
    6161
    6262static int
    63 checkFile( tr_torrent *    tor,
    64            tr_file_index_t fileIndex,
    65            int *           abortFlag )
     63checkFile( tr_torrent      * tor,
     64           void            * buffer,
     65           size_t            buflen,
     66           tr_file_index_t   fileIndex,
     67           int             * abortFlag )
    6668{
    6769    tr_piece_index_t i;
     
    8587        else if( !tr_torrentIsPieceChecked( tor, i ) )
    8688        {
    87             const int      wasComplete = tr_cpPieceIsComplete(
    88                 tor->completion, i );
    89 
    90             if( tr_ioTestPiece( tor, i ) ) /* yay */
     89            const int wasComplete = tr_cpPieceIsComplete( tor->completion, i );
     90
     91            if( tr_ioTestPiece( tor, i, buffer, buflen ) ) /* yay */
    9192            {
    9293                tr_torrentSetHasPiece( tor, i, TRUE );
     
    120121verifyThreadFunc( void * unused UNUSED )
    121122{
    122     for( ; ; )
     123    for( ;; )
    123124    {
    124125        int                  changed = 0;
    125126        tr_file_index_t      i;
    126         tr_torrent *        tor;
     127        tr_torrent         * tor;
    127128        struct verify_node * node;
     129        void               * buffer;
    128130
    129131        tr_lockLock( getVerifyLock( ) );
     
    142144        tr_lockUnlock( getVerifyLock( ) );
    143145
     146        tr_torinf( tor, _( "Verifying torrent" ) );
    144147        tor->verifyState = TR_VERIFY_NOW;
    145 
    146         tr_torinf( tor, _( "Verifying torrent" ) );
     148        buffer = tr_new( uint8_t, tor->info.pieceSize );
    147149        for( i = 0; i < tor->info.fileCount && !stopCurrent; ++i )
    148             changed |= checkFile( tor, i, &stopCurrent );
    149 
     150            changed |= checkFile( tor, buffer, tor->info.pieceSize, i, &stopCurrent );
     151        tr_free( buffer );
    150152        tor->verifyState = TR_VERIFY_NONE;
    151153
Note: See TracChangeset for help on using the changeset viewer.