]> granicus.if.org Git - transmission/commitdiff
(trunk libT) faster tr_cpBlockBitfieldSet()
authorCharles Kerr <charles@transmissionbt.com>
Tue, 6 Jan 2009 07:45:15 +0000 (07:45 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Tue, 6 Jan 2009 07:45:15 +0000 (07:45 +0000)
libtransmission/completion.c

index f0850051a73293f1a29495bba663f4e9a1984120..042c11d7ed1615403bf2ca5aa48774ebc04a89d6 100644 (file)
@@ -35,8 +35,7 @@ tr_cpReset( tr_completion * cp )
 {
     tr_bitfieldClear( &cp->pieceBitfield );
     tr_bitfieldClear( &cp->blockBitfield );
-    memset( cp->completeBlocks, 0,
-            sizeof( uint16_t ) * cp->tor->info.pieceCount );
+    memset( cp->completeBlocks, 0, sizeof( uint16_t ) * cp->tor->info.pieceCount );
     cp->sizeNow = 0;
     cp->sizeWhenDoneIsDirty = 1;
     cp->haveValidIsDirty = 1;
@@ -178,6 +177,7 @@ tr_cpBlockAdd( tr_completion *  cp,
     }
 }
 
+#if 0
 int
 tr_cpBlockBitfieldSet( tr_completion * cp,
                        tr_bitfield *   bitfield )
@@ -199,6 +199,52 @@ tr_cpBlockBitfieldSet( tr_completion * cp,
 
     return success;
 }
+#endif
+
+int
+tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * blockBitfield )
+{
+    int success = FALSE;
+
+    assert( cp );
+    assert( blockBitfield );
+
+    if(( success = tr_bitfieldTestFast( blockBitfield, cp->tor->blockCount - 1 )))
+    {
+        tr_piece_index_t p;
+        const tr_torrent * tor = cp->tor;
+
+        tr_cpReset( cp );
+
+        for( p=0; p<tor->info.pieceCount; ++p )
+        {
+            tr_block_index_t i;
+            uint16_t completeBlocksInPiece = 0;
+
+            const tr_block_index_t start = tr_torPieceFirstBlock( tor, p );
+            const tr_block_index_t end = start + tr_torPieceCountBlocks( tor, p );
+
+            for( i=start; i!=end; ++i ) {
+                if( tr_bitfieldTestFast( blockBitfield, i ) ) {
+                    ++completeBlocksInPiece;
+                    cp->sizeNow += tr_torBlockCountBytes( tor, i );
+                }
+            }
+
+            cp->completeBlocks[p] = completeBlocksInPiece;
+
+            if( completeBlocksInPiece == end - start )
+                tr_bitfieldAdd( &cp->pieceBitfield, p );
+        }
+
+        memcpy( cp->blockBitfield->bits, blockBitfield->bits, cp->blockBitfield->byteCount );
+
+        cp->haveValidIsDirty = 1;
+        cp->sizeWhenDoneIsDirty = 1;
+    }
+
+    return success;
+}
 
 /***
 ****