]> granicus.if.org Git - transmission/commitdiff
(trunk libT) memory cache should use evbuffers to avoid unnecessary calls to memcpy...
authorJordan Lee <jordan@transmissionbt.com>
Sat, 29 Jan 2011 18:56:53 +0000 (18:56 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sat, 29 Jan 2011 18:56:53 +0000 (18:56 +0000)
libtransmission/cache.c
libtransmission/cache.h
libtransmission/peer-msgs.c
libtransmission/webseed.c

index a144c1808dc2602be69c18237d5b32318970b90d..837716421555f25673bba3483c032ec6bb4f0b71 100644 (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 );
 
index b63f891bb53adec64572a148ce44e79d81d6983a..8a0feb29a9571ca9f0e9484804dd4b8e69f95547 100644 (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,
index 3f63d4ab3edc7eb79741a6bc4dfe4c8eb1ac0ef8..1cc87c66d9c60e888422b5cbc7f6f64f32ff06f9 100644 (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;
index 76d30be7dfc7b4e0fe83b05db103fef686ba8840..6e27a13422b2f601cc74a4600077b1603a6cfd2e 100644 (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 );