From 5b23aae320c12f3e94b852d93fca4c47c9e1d9c4 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Sat, 29 Jan 2011 18:56:53 +0000 Subject: [PATCH] (trunk libT) memory cache should use evbuffers to avoid unnecessary calls to memcpy -- done. --- libtransmission/cache.c | 19 +++++++++++-------- libtransmission/cache.h | 4 +++- libtransmission/peer-msgs.c | 6 +++--- libtransmission/webseed.c | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/libtransmission/cache.c b/libtransmission/cache.c index a144c1808..837716421 100644 --- a/libtransmission/cache.c +++ b/libtransmission/cache.c @@ -10,6 +10,8 @@ * $Id$ */ +#include + #include "transmission.h" #include "cache.h" #include "inout.h" @@ -42,7 +44,7 @@ struct cache_block time_t time; tr_block_index_t block; - uint8_t * buf; + struct evbuffer * evbuf; }; struct tr_cache @@ -170,9 +172,9 @@ flushContiguous( tr_cache * cache, int pos, int n ) for( i=pos; ibuf, b->length ); + evbuffer_copyout( b->evbuf, walk, b->length ); walk += b->length; - tr_free( b->buf ); + evbuffer_free( b->evbuf ); tr_free( b ); } tr_ptrArrayErase( &cache->blocks, pos, pos+n ); @@ -319,7 +321,7 @@ tr_cacheWriteBlock( tr_cache * cache, tr_piece_index_t piece, uint32_t offset, uint32_t length, - const uint8_t * writeme ) + struct evbuffer * writeme ) { struct cache_block * cb = findBlock( cache, torrent, piece, offset ); @@ -331,14 +333,15 @@ tr_cacheWriteBlock( tr_cache * cache, cb->offset = offset; cb->length = length; cb->block = _tr_block( torrent, piece, offset ); - cb->buf = NULL; + cb->evbuf = evbuffer_new( ); tr_ptrArrayInsertSorted( &cache->blocks, cb, cache_block_compare ); } cb->time = tr_time(); - tr_free( cb->buf ); - cb->buf = tr_memdup( writeme, cb->length ); + assert( cb->length == length ); + evbuffer_drain( cb->evbuf, evbuffer_get_length( cb->evbuf ) ); + evbuffer_remove_buffer( writeme, cb->evbuf, cb->length ); ++cache->cache_writes; cache->cache_write_bytes += cb->length; @@ -358,7 +361,7 @@ tr_cacheReadBlock( tr_cache * cache, struct cache_block * cb = findBlock( cache, torrent, piece, offset ); if( cb ) - memcpy( setme, cb->buf, len ); + evbuffer_copyout( cb->evbuf, setme, len ); else err = tr_ioRead( torrent, piece, offset, len, setme ); diff --git a/libtransmission/cache.h b/libtransmission/cache.h index b63f891bb..8a0feb29a 100644 --- a/libtransmission/cache.h +++ b/libtransmission/cache.h @@ -17,6 +17,8 @@ #ifndef TR_CACHE_H #define TR_CACHE_H +struct evbuffer; + typedef struct tr_cache tr_cache; /*** @@ -40,7 +42,7 @@ int tr_cacheWriteBlock( tr_cache * cache, tr_piece_index_t piece, uint32_t offset, uint32_t len, - const uint8_t * writeme ); + struct evbuffer * writeme ); int tr_cacheReadBlock( tr_cache * cache, tr_torrent * torrent, diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index 3f63d4ab3..1cc87c66d 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -1279,7 +1279,7 @@ messageLengthIsCorrect( const tr_peermsgs * msg, uint8_t id, uint32_t len ) } static int clientGotBlock( tr_peermsgs * msgs, - const uint8_t * block, + struct evbuffer * block, const struct peer_request * req ); static int @@ -1335,7 +1335,7 @@ readBtPiece( tr_peermsgs * msgs, return READ_LATER; /* we've got the whole block ... process it */ - err = clientGotBlock( msgs, evbuffer_pullup( msgs->incoming.block, -1 ), req ); + err = clientGotBlock( msgs, msgs->incoming.block, req ); /* cleanup */ evbuffer_free( msgs->incoming.block ); @@ -1562,7 +1562,7 @@ addPeerToBlamefield( tr_peermsgs * msgs, uint32_t index ) /* returns 0 on success, or an errno on failure */ static int clientGotBlock( tr_peermsgs * msgs, - const uint8_t * data, + struct evbuffer * data, const struct peer_request * req ) { int err; diff --git a/libtransmission/webseed.c b/libtransmission/webseed.c index 76d30be7d..6e27a1342 100644 --- a/libtransmission/webseed.c +++ b/libtransmission/webseed.c @@ -200,7 +200,7 @@ web_response_func( tr_session * session, tr_cacheWriteBlock( session->cache, tor, t->piece_index, t->piece_offset, t->length, - evbuffer_pullup( t->content, -1 ) ); + t->content ); fire_client_got_block( tor, w, t->block ); tr_list_remove_data( &w->tasks, t );