(trunk libT) when reading piece data in from a socket, avoid two unnecessary calls to memcpy()

This commit is contained in:
Jordan Lee 2011-04-04 04:45:41 +00:00
parent 3e451b2bf9
commit 525d854016
6 changed files with 32 additions and 54 deletions

View File

@ -1060,6 +1060,29 @@ evbuffer_add_uint64( struct evbuffer * outbuf, uint64_t addme_hll )
****
***/
void
tr_peerIoReadBytesToBuf( tr_peerIo * io, struct evbuffer * inbuf, struct evbuffer * outbuf, size_t byteCount )
{
const size_t old_length = evbuffer_get_length( outbuf );
assert( tr_isPeerIo( io ) );
assert( evbuffer_get_length( inbuf ) >= byteCount );
/* append it to outbuf */
evbuffer_remove_buffer( inbuf, outbuf, byteCount );
/* decrypt if needed */
if( io->encryption_type == PEER_ENCRYPTION_RC4 ) {
struct evbuffer_ptr pos;
struct evbuffer_iovec iovec;
evbuffer_ptr_set( outbuf, &pos, old_length, EVBUFFER_PTR_SET );
do {
evbuffer_peek( outbuf, byteCount, &pos, &iovec, 1 );
tr_cryptoDecrypt( io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
} while( !evbuffer_ptr_set( outbuf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD ) );
}
}
void
tr_peerIoReadBytes( tr_peerIo * io, struct evbuffer * inbuf, void * bytes, size_t byteCount )
{
@ -1106,8 +1129,8 @@ tr_peerIoDrain( tr_peerIo * io,
struct evbuffer * inbuf,
size_t byteCount )
{
void * buf = tr_sessionGetBuffer( io->session );
const size_t buflen = SESSION_BUFFER_SIZE;
char buf[4096];
const size_t buflen = sizeof( buf );
while( byteCount > 0 )
{
@ -1115,8 +1138,6 @@ tr_peerIoDrain( tr_peerIo * io,
tr_peerIoReadBytes( io, inbuf, buf, thisPass );
byteCount -= thisPass;
}
tr_sessionReleaseBuffer( io->session );
}
/***

View File

@ -316,6 +316,11 @@ evbuffer_add_hton_64( struct evbuffer * buf, uint64_t val )
evbuffer_add_uint64( buf, val );
}
void tr_peerIoReadBytesToBuf( tr_peerIo * io,
struct evbuffer * inbuf,
struct evbuffer * outbuf,
size_t byteCount );
void tr_peerIoReadBytes( tr_peerIo * io,
struct evbuffer * inbuf,
void * bytes,

View File

@ -3157,7 +3157,7 @@ rechokePulse( int foo UNUSED, short bar UNUSED, void * vmgr )
while(( tor = tr_torrentNext( mgr->session, tor ))) {
if( tor->isRunning ) {
Torrent * t = tor->torrentPeers;
if( tr_ptrArraySize( &t->peers ) == 0 )
if( tr_ptrArrayEmpty( &t->peers ) )
continue;
rechokeUploads( t, now );
if( !tr_torrentIsSeed( tor ) )

View File

@ -1308,20 +1308,8 @@ readBtPiece( tr_peermsgs * msgs,
/* read in another chunk of data */
const size_t nLeft = req->length - evbuffer_get_length( msgs->incoming.block );
size_t n = MIN( nLeft, inlen );
size_t i = n;
void * buf = tr_sessionGetBuffer( getSession( msgs ) );
const size_t buflen = SESSION_BUFFER_SIZE;
while( i > 0 )
{
const size_t thisPass = MIN( i, buflen );
tr_peerIoReadBytes( msgs->peer->io, inbuf, buf, thisPass );
evbuffer_add( msgs->incoming.block, buf, thisPass );
i -= thisPass;
}
tr_sessionReleaseBuffer( getSession( msgs ) );
buf = NULL;
tr_peerIoReadBytesToBuf( msgs->peer->io, inbuf, msgs->incoming.block, n );
fireClientGotData( msgs, n, true );
*setme_piece_bytes_read += n;

View File

@ -571,7 +571,6 @@ tr_sessionInit( const char * tag,
session->cache = tr_cacheNew( 1024*1024*2 );
session->tag = tr_strdup( tag );
session->magicNumber = SESSION_MAGIC_NUMBER;
session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
tr_bandwidthConstruct( &session->bandwidth, session, NULL );
tr_peerIdInit( session->peer_id );
tr_bencInitList( &session->removedTorrents, 0 );
@ -1009,27 +1008,6 @@ tr_sessionIsIncompleteDirEnabled( const tr_session * session )
****
***/
void*
tr_sessionGetBuffer( tr_session * session )
{
assert( tr_isSession( session ) );
assert( !session->bufferInUse );
assert( tr_amInEventThread( session ) );
session->bufferInUse = true;
return session->buffer;
}
void
tr_sessionReleaseBuffer( tr_session * session )
{
assert( tr_isSession( session ) );
assert( session->bufferInUse );
assert( tr_amInEventThread( session ) );
session->bufferInUse = false;
}
void
tr_sessionLock( tr_session * session )
{
@ -1862,7 +1840,6 @@ tr_sessionClose( tr_session * session )
tr_free( session->metainfoLookup );
}
tr_free( session->torrentDoneScript );
tr_free( session->buffer );
tr_free( session->tag );
tr_free( session->configDir );
tr_free( session->resumeDir );

View File

@ -206,12 +206,6 @@ struct tr_session
struct tr_bindinfo * public_ipv4;
struct tr_bindinfo * public_ipv6;
/* a page-aligned buffer for use by the libtransmission thread.
* @see SESSION_BUFFER_SIZE */
void * buffer;
bool bufferInUse;
tr_web_config_func * curl_easy_config_func;
uint8_t peer_id[PEER_ID_LEN+1];
@ -261,15 +255,8 @@ int tr_sessionCountTorrents( const tr_session * session );
enum
{
SESSION_MAGIC_NUMBER = 3845,
/* @see tr_session.buffer */
SESSION_BUFFER_SIZE = (16*1024)
};
void* tr_sessionGetBuffer( tr_session * session );
void tr_sessionReleaseBuffer( tr_session * session );
static inline bool tr_isSession( const tr_session * session )
{
return ( session != NULL ) && ( session->magicNumber == SESSION_MAGIC_NUMBER );