Changeset 2164


Ignore:
Timestamp:
Jun 19, 2007, 6:01:30 AM (15 years ago)
Author:
charles
Message:

better algorithm to decide on a good piece size. (SoftwareElves? & charles_)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/makemeta.c

    r2162 r2164  
    99 */
    1010
     11#include <math.h>
    1112#include <sys/types.h>
    1213#include <sys/stat.h>
     
    7475
    7576static int
    76 bestPieceSize( uint64_t totalSize )
    77 {
    78     const int MiB = 1048576;
    79     const int GiB = totalSize / (uint64_t)1073741824;
    80 
    81     /* almost always best to have a piee size of 512 or 256 kb.
    82        common practice seems to be to bump up to 1MB pieces at
    83        at total size of around 8GiB or so */
    84 
    85     if( GiB >= 8 )
    86         return MiB;
    87 
    88     if( GiB >= 1 )
    89         return MiB / 2;
    90 
    91     return MiB / 4;
     77bestPieceSize( uint64_t totalSize, uint64_t fileCount )
     78{
     79    int pieceSize = 1;
     80    const int minPieceSize = 65536; /* arbitrary; 2^16 */
     81    const int maxPieceSize = 16777216; /* arbitrary; 2^24 */
     82    const int desiredMinPiecesPerFile = 15; /* arbitrary */
     83    uint64_t log;
     84
     85    /* start off in the range of (1024..2048] pieces
     86       for "normal" torrents... */
     87    log = totalSize;
     88    while( log > 2048 ) {
     89        log >>= 1;
     90        pieceSize <<= 1;
     91    }
     92
     93    /* special case 1: torrents with a lot of small files.
     94       try to have a reasonable number of pieces per file,
     95       which will reduce overhead on selective downloading
     96       and increase swarm speed. */
     97    while( totalSize/pieceSize < (fileCount * desiredMinPiecesPerFile) ) {
     98        const int swap = pieceSize >> 1;
     99        if( swap < minPieceSize )
     100            break;
     101        pieceSize = swap;
     102    }
     103
     104    /* special case 2: enormous single-file torrents. our normal
     105       case creates unwieldly piece sizes in that case */
     106    while( pieceSize < maxPieceSize )
     107        pieceSize >>= 1;
     108
     109    return pieceSize;
    92110}
    93111
     
    149167           builderFileCompare );
    150168
    151     ret->pieceSize = bestPieceSize( ret->totalSize );
     169    ret->pieceSize = bestPieceSize( ret->totalSize, ret->fileCount );
    152170    ret->pieceCount = (int)( ret->totalSize / ret->pieceSize);
    153171    if( ret->totalSize % ret->pieceSize )
Note: See TracChangeset for help on using the changeset viewer.