faster tr_cpGetStatus() and tr_bitfieldHas(), which were showing up at the top of the CPU profile.

This commit is contained in:
Charles Kerr
2007-07-24 14:51:46 +00:00
parent eefa51c65c
commit 17a8ae9fa2
3 changed files with 11 additions and 16 deletions

View File

@@ -99,7 +99,7 @@ int tr_cpPieceHasAllBlocks( const tr_completion_t * cp, int piece )
int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece )
{
return tr_bitfieldHas( cp->pieceBitfield, piece );
return cp->completeBlocks[piece] >= tr_cpCountBlocks(cp,piece);
}
const tr_bitfield_t * tr_cpPieceBitfield( const tr_completion_t * cp )

View File

@@ -166,10 +166,10 @@ static int isPieceInteresting( const tr_torrent_t * tor,
const tr_peer_t * peer,
int piece )
{
if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we already have it */
if( tor->info.pieces[piece].dnd ) /* we don't want it */
return 0;
if( tor->info.pieces[piece].dnd ) /* we don't want it */
if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we already have it */
return 0;
if( !tr_bitfieldHas( peer->bitfield, piece ) ) /* peer doesn't have it */

View File

@@ -574,22 +574,20 @@ tr_bitfieldIsEmpty( const tr_bitfield_t * bitfield )
return 1;
}
static const uint8_t bitmask[8] = { 128u, 64u, 32u, 16u, 8u, 4u, 2u, 1u };
#define BIN(nth) (nth>>3)
#define BIT(nth) (1<<(7-(nth%8)))
int
tr_bitfieldHas( const tr_bitfield_t * bitfield,
size_t bit )
size_t nth )
{
if ( bitfield == NULL ) return 0;
assert( bit / 8u < bitfield->len );
return ( bitfield->bits[ bit/8u ] & bitmask[bit%8] ) != 0;
return bitfield && (bitfield->bits[ BIN(nth) ] & BIT(nth) );
}
void
tr_bitfieldAdd( tr_bitfield_t * bitfield, size_t bit )
tr_bitfieldAdd( tr_bitfield_t * bitfield, size_t nth )
{
assert( bit / 8u < bitfield->len );
bitfield->bits[ bit/8u ] |= bitmask[bit%8];
bitfield->bits[ BIN(nth) ] |= BIT(nth);
}
void
@@ -605,13 +603,10 @@ tr_bitfieldAddRange( tr_bitfield_t * bitfield,
void
tr_bitfieldRem( tr_bitfield_t * bitfield,
size_t bit )
size_t nth )
{
if( bitfield != NULL )
{
assert( bit / 8u < bitfield->len );
bitfield->bits[bit/8u] &= ~bitmask[bit%8];
}
bitfield->bits[BIN(nth)] &= ~BIT(nth);
}
void