mirror of
https://github.com/transmission/transmission
synced 2025-02-23 14:40:43 +00:00
(trunk libT) optimize out some unnecessary cycles when there are missing files in a torrent being verified
This commit is contained in:
parent
4524029ab4
commit
c0cf694b85
1 changed files with 17 additions and 20 deletions
|
@ -47,6 +47,10 @@ enum
|
||||||
|
|
||||||
/* #define STOPWATCH */
|
/* #define STOPWATCH */
|
||||||
|
|
||||||
|
#ifndef HAVE_VALLOC
|
||||||
|
#define valloc malloc
|
||||||
|
#endif
|
||||||
|
|
||||||
static tr_bool
|
static tr_bool
|
||||||
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
{
|
{
|
||||||
|
@ -59,9 +63,10 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
uint32_t piecePos = 0;
|
uint32_t piecePos = 0;
|
||||||
uint32_t pieceBytesRead = 0;
|
uint32_t pieceBytesRead = 0;
|
||||||
tr_file_index_t fileIndex = 0;
|
tr_file_index_t fileIndex = 0;
|
||||||
|
tr_file_index_t prevFileIndex = !fileIndex;
|
||||||
tr_piece_index_t pieceIndex = 0;
|
tr_piece_index_t pieceIndex = 0;
|
||||||
uint8_t * buffer = NULL;
|
const int64_t buflen = 4096;
|
||||||
int64_t buflen = 0;
|
uint8_t * buffer = valloc( buflen );
|
||||||
const time_t begin = tr_time( );
|
const time_t begin = tr_time( );
|
||||||
time_t end;
|
time_t end;
|
||||||
|
|
||||||
|
@ -72,7 +77,6 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
int64_t leftInPiece;
|
int64_t leftInPiece;
|
||||||
int64_t leftInFile;
|
int64_t leftInFile;
|
||||||
int64_t bytesThisPass;
|
int64_t bytesThisPass;
|
||||||
ssize_t numRead;
|
|
||||||
const tr_file * file = &tor->info.files[fileIndex];
|
const tr_file * file = &tor->info.files[fileIndex];
|
||||||
|
|
||||||
/* if we're starting a new piece... */
|
/* if we're starting a new piece... */
|
||||||
|
@ -83,22 +87,13 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if we're starting a new file... */
|
/* if we're starting a new file... */
|
||||||
if( !filePos && (fd<0) )
|
if( !filePos && (fd<0) && (fileIndex!=prevFileIndex) )
|
||||||
{
|
{
|
||||||
char * filename = tr_torrentFindFile( tor, fileIndex );
|
char * filename = tr_torrentFindFile( tor, fileIndex );
|
||||||
fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
|
fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
|
||||||
/* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
|
/* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
|
||||||
if( ( fd >= 0 ) && ( buffer == NULL ) )
|
|
||||||
{
|
|
||||||
struct stat st;
|
|
||||||
buflen = fstat( fd, &st ) ? 4096 : st.st_blksize;
|
|
||||||
#ifdef HAVE_VALLOC
|
|
||||||
buffer = valloc( buflen );
|
|
||||||
#else
|
|
||||||
buffer = malloc( buflen );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
tr_free( filename );
|
tr_free( filename );
|
||||||
|
prevFileIndex = fileIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* figure out how much we can read this pass */
|
/* figure out how much we can read this pass */
|
||||||
|
@ -109,7 +104,8 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
/* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
|
/* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
|
||||||
|
|
||||||
/* read a bit */
|
/* read a bit */
|
||||||
numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
|
if( fd >= 0 ) {
|
||||||
|
const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
|
||||||
if( numRead == bytesThisPass )
|
if( numRead == bytesThisPass )
|
||||||
SHA1_Update( &sha, buffer, numRead );
|
SHA1_Update( &sha, buffer, numRead );
|
||||||
if( numRead > 0 ) {
|
if( numRead > 0 ) {
|
||||||
|
@ -118,6 +114,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
|
posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* move our offsets */
|
/* move our offsets */
|
||||||
leftInPiece -= bytesThisPass;
|
leftInPiece -= bytesThisPass;
|
||||||
|
|
Loading…
Reference in a new issue