From 525d854016546d041fe37f20ef4de737f097fdd4 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Mon, 4 Apr 2011 04:45:41 +0000 Subject: [PATCH] (trunk libT) when reading piece data in from a socket, avoid two unnecessary calls to memcpy() --- libtransmission/peer-io.c | 29 +++++++++++++++++++++++++---- libtransmission/peer-io.h | 5 +++++ libtransmission/peer-mgr.c | 2 +- libtransmission/peer-msgs.c | 14 +------------- libtransmission/session.c | 23 ----------------------- libtransmission/session.h | 13 ------------- 6 files changed, 32 insertions(+), 54 deletions(-) diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index c3afd378f..630018804 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -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 ); } /*** diff --git a/libtransmission/peer-io.h b/libtransmission/peer-io.h index eff3ac2de..69299a94f 100644 --- a/libtransmission/peer-io.h +++ b/libtransmission/peer-io.h @@ -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, diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 14f2faf2e..ae7843f86 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -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 ) ) diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index ad776acf3..f780d352c 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -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; diff --git a/libtransmission/session.c b/libtransmission/session.c index ead336230..71a6de9b0 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -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 ); diff --git a/libtransmission/session.h b/libtransmission/session.h index 698ee4037..a64268222 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -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 ); -- 2.40.0