1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 01:03:01 +00:00

(trunk libT) memory cache should use evbuffers to avoid unnecessary calls to memcpy -- done.

This commit is contained in:
Jordan Lee 2011-01-29 18:56:53 +00:00
parent bbe51e1faf
commit 5b23aae320
4 changed files with 18 additions and 13 deletions

View file

@ -10,6 +10,8 @@
* $Id$
*/
#include <event2/buffer.h>
#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; i<pos+n; ++i ) {
b = blocks[i];
memcpy( walk, b->buf, 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 );

View file

@ -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,

View file

@ -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;

View file

@ -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 );