Ignore:
Timestamp:
Feb 13, 2010, 5:46:31 AM (12 years ago)
Author:
charles
Message:

(trunk libT) optimize out some unnecessary cycles when there are missing files in a torrent being verified

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/verify.c

    r10187 r10191  
    4848/* #define STOPWATCH */
    4949
     50#ifndef HAVE_VALLOC
     51 #define valloc malloc
     52#endif
     53
    5054static tr_bool
    5155verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
     
    6064    uint32_t pieceBytesRead = 0;
    6165    tr_file_index_t fileIndex = 0;
     66    tr_file_index_t prevFileIndex = !fileIndex;
    6267    tr_piece_index_t pieceIndex = 0;
    63     uint8_t * buffer = NULL;
    64     int64_t buflen = 0;
     68    const int64_t buflen = 4096;
     69    uint8_t * buffer = valloc( buflen );
    6570    const time_t begin = tr_time( );
    6671    time_t end;
     
    7378        int64_t leftInFile;
    7479        int64_t bytesThisPass;
    75         ssize_t numRead;
    7680        const tr_file * file = &tor->info.files[fileIndex];
    7781
     
    8488
    8589        /* if we're starting a new file... */
    86         if( !filePos && (fd<0) )
     90        if( !filePos && (fd<0) && (fileIndex!=prevFileIndex) )
    8791        {
    8892            char * filename = tr_torrentFindFile( tor, fileIndex );
    8993            fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
    9094            /* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
    91             if( ( fd >= 0 ) && ( buffer == NULL ) )
    92             {
    93                 struct stat st;
    94                 buflen = fstat( fd, &st ) ? 4096 : st.st_blksize;
    95 #ifdef HAVE_VALLOC
    96                 buffer = valloc( buflen );
    97 #else
    98                 buffer = malloc( buflen );
    99 #endif
    100             }
    10195            tr_free( filename );
     96            prevFileIndex = fileIndex;
    10297        }
    10398
     
    110105
    111106        /* read a bit */
    112         numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
    113         if( numRead == bytesThisPass )
    114             SHA1_Update( &sha, buffer, numRead );
    115         if( numRead > 0 ) {
    116             pieceBytesRead += numRead;
     107        if( fd >= 0 ) {
     108            const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
     109            if( numRead == bytesThisPass )
     110                SHA1_Update( &sha, buffer, numRead );
     111            if( numRead > 0 ) {
     112                pieceBytesRead += numRead;
    117113#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
    118             posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
    119 #endif
     114                posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
     115#endif
     116            }
    120117        }
    121118
Note: See TracChangeset for help on using the changeset viewer.