From 7cfafc5371c2f258eca426317747a8895cecd5b6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 28 Jul 2007 22:47:10 +0000 Subject: [PATCH] * now that we've got a new function at the top of the CPU hog list, tweak it too. * reduce large torrents' memory consumption in tr_completion_t. --- libtransmission/completion.c | 20 ++++++++++---------- libtransmission/utils.c | 7 ------- libtransmission/utils.h | 4 ++-- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/libtransmission/completion.c b/libtransmission/completion.c index f63df2284..c660116bf 100644 --- a/libtransmission/completion.c +++ b/libtransmission/completion.c @@ -29,8 +29,8 @@ struct tr_completion_s { tr_torrent_t * tor; - /* number of peers from whom we've requested this block */ - uint8_t * blockDownloaders; + /* true if a peer is requesting this block */ + tr_bitfield_t * blockRequested; /* do we have this block? */ tr_bitfield_t * blockBitfield; @@ -54,7 +54,7 @@ tr_completion_t * tr_cpInit( tr_torrent_t * tor ) cp = tr_new( tr_completion_t, 1 ); cp->tor = tor; cp->blockBitfield = tr_bitfieldNew( tor->blockCount ); - cp->blockDownloaders = tr_new( uint8_t, tor->blockCount ); + cp->blockRequested = tr_bitfieldNew( tor->blockCount ); cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount ); cp->completeBlocks = tr_new( uint16_t, tor->info.pieceCount ); @@ -67,7 +67,7 @@ void tr_cpClose( tr_completion_t * cp ) { tr_free( cp->completeBlocks ); tr_bitfieldFree( cp->pieceBitfield ); - tr_free( cp->blockDownloaders ); + tr_bitfieldFree( cp->blockRequested ); tr_bitfieldFree( cp->blockBitfield ); tr_free( cp ); } @@ -76,9 +76,9 @@ void tr_cpReset( tr_completion_t * cp ) { tr_torrent_t * tor = cp->tor; - tr_bitfieldClear( cp->blockBitfield ); - memset( cp->blockDownloaders, 0, tor->blockCount ); tr_bitfieldClear( cp->pieceBitfield ); + tr_bitfieldClear( cp->blockBitfield ); + tr_bitfieldClear( cp->blockRequested ); memset( cp->completeBlocks, 0, sizeof(uint16_t) * tor->info.pieceCount ); cp->doneDirty = TRUE; @@ -194,12 +194,12 @@ void tr_cpPieceRem( tr_completion_t * cp, int piece ) /* Blocks */ void tr_cpDownloaderAdd( tr_completion_t * cp, int block ) { - ++cp->blockDownloaders[block]; + tr_bitfieldAdd( cp->blockRequested, block ); } void tr_cpDownloaderRem( tr_completion_t * cp, int block ) { - --cp->blockDownloaders[block]; + tr_bitfieldRem( cp->blockRequested, block ); } int tr_cpBlockIsComplete( const tr_completion_t * cp, int block ) @@ -271,7 +271,7 @@ tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece ) n = 0; for( i = start; i < end; ++i ) - if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] ) + if( !tr_cpBlockIsComplete( cp, i ) && !tr_bitfieldHas( cp->blockRequested, i ) ) ++n; return n; @@ -285,7 +285,7 @@ int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece ) const int end = start + tr_torPieceCountBlocks(tor,piece); for( i = start; i < end; ++i ) - if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] ) + if( !tr_cpBlockIsComplete( cp, i ) && !tr_bitfieldHas( cp->blockRequested, i ) ) return i; return -1; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index d6d9f1170..7948ca70e 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -580,13 +580,6 @@ tr_bitfieldIsEmpty( const tr_bitfield_t * bitfield ) #define BIN(nth) ((nth>>3)) #define BIT(nth) (1<<(7-(nth%8))) -int -tr_bitfieldHas( const tr_bitfield_t * bitfield, - size_t nth ) -{ - return bitfield && (bitfield->bits[ BIN(nth) ] & BIT(nth) ); -} - void tr_bitfieldAdd( tr_bitfield_t * bitfield, size_t nth ) { diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 73cabf964..2cf4857c0 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -139,12 +139,12 @@ void tr_bitfieldAddRange( tr_bitfield_t *, size_t begin, size_t end ); void tr_bitfieldRemRange ( tr_bitfield_t*, size_t begin, size_t end ); int tr_bitfieldIsEmpty( const tr_bitfield_t* ); -int tr_bitfieldHas( const tr_bitfield_t *, size_t bit ); size_t tr_bitfieldCountTrueBits( const tr_bitfield_t* ); tr_bitfield_t* tr_bitfieldNegate( tr_bitfield_t* ); tr_bitfield_t* tr_bitfieldAnd( tr_bitfield_t*, const tr_bitfield_t* ); - +#define tr_bitfieldHas(bitfield,nth) \ + ( ( bitfield ) && ( (bitfield)->bits[(nth)>>3] & 128 >>( (nth) & 7 ) ) ) #endif