Changeset 8412


Ignore:
Timestamp:
May 15, 2009, 8:00:12 PM (12 years ago)
Author:
charles
Message:

(trunk libT) speed up rpcimpl's torrentGet() by about 75%

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/rpcimpl.c

    r8389 r8412  
    401401}
    402402
     403/* faster-than-strcmp() optimization.  this is kind of clumsy,
     404   but addField() was in the profiler's top 10 list, and this
     405   makes it 4x faster... */
     406#define tr_streq(a,alen,b) ((alen+1==sizeof(b)) && !memcmp(a,b,alen))
     407
    403408static void
    404 addField( const tr_torrent * tor,
    405           tr_benc *          d,
    406           const char *       key )
     409addField( const tr_torrent * tor, tr_benc * d, const char * key )
    407410{
    408411    const tr_info * inf = tr_torrentInfo( tor );
    409412    const tr_stat * st = tr_torrentStat( (tr_torrent*)tor );
    410 
    411     if( !strcmp( key, "activityDate" ) )
     413    const size_t keylen = strlen( key );
     414
     415    if( tr_streq( key, keylen, "activityDate" ) )
    412416        tr_bencDictAddInt( d, key, st->activityDate );
    413     else if( !strcmp( key, "addedDate" ) )
     417    else if( tr_streq( key, keylen, "addedDate" ) )
    414418        tr_bencDictAddInt( d, key, st->addedDate );
    415     else if( !strcmp( key, "announceResponse" ) )
     419    else if( tr_streq( key, keylen, "announceResponse" ) )
    416420        tr_bencDictAddStr( d, key, st->announceResponse );
    417     else if( !strcmp( key, "announceURL" ) )
     421    else if( tr_streq( key, keylen, "announceURL" ) )
    418422        tr_bencDictAddStr( d, key, st->announceURL );
    419     else if( !strcmp( key, "bandwidthPriority" ) )
     423    else if( tr_streq( key, keylen, "bandwidthPriority" ) )
    420424        tr_bencDictAddInt( d, key, tr_torrentGetPriority( tor ) );
    421     else if( !strcmp( key, "comment" ) )
     425    else if( tr_streq( key, keylen, "comment" ) )
    422426        tr_bencDictAddStr( d, key, inf->comment ? inf->comment : "" );
    423     else if( !strcmp( key, "corruptEver" ) )
     427    else if( tr_streq( key, keylen, "corruptEver" ) )
    424428        tr_bencDictAddInt( d, key, st->corruptEver );
    425     else if( !strcmp( key, "creator" ) )
     429    else if( tr_streq( key, keylen, "creator" ) )
    426430        tr_bencDictAddStr( d, key, inf->creator ? inf->creator : "" );
    427     else if( !strcmp( key, "dateCreated" ) )
     431    else if( tr_streq( key, keylen, "dateCreated" ) )
    428432        tr_bencDictAddInt( d, key, inf->dateCreated );
    429     else if( !strcmp( key, "desiredAvailable" ) )
     433    else if( tr_streq( key, keylen, "desiredAvailable" ) )
    430434        tr_bencDictAddInt( d, key, st->desiredAvailable );
    431     else if( !strcmp( key, "doneDate" ) )
     435    else if( tr_streq( key, keylen, "doneDate" ) )
    432436        tr_bencDictAddInt( d, key, st->doneDate );
    433     else if( !strcmp( key, "downloadDir" ) )
     437    else if( tr_streq( key, keylen, "downloadDir" ) )
    434438        tr_bencDictAddStr( d, key, tr_torrentGetDownloadDir( tor ) );
    435     else if( !strcmp( key, "downloadedEver" ) )
     439    else if( tr_streq( key, keylen, "downloadedEver" ) )
    436440        tr_bencDictAddInt( d, key, st->downloadedEver );
    437     else if( !strcmp( key, "downloaders" ) )
     441    else if( tr_streq( key, keylen, "downloaders" ) )
    438442        tr_bencDictAddInt( d, key, st->downloaders );
    439     else if( !strcmp( key, "downloadLimit" ) )
     443    else if( tr_streq( key, keylen, "downloadLimit" ) )
    440444        tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit( tor, TR_DOWN ) );
    441     else if( !strcmp( key, "downloadLimited" ) )
     445    else if( tr_streq( key, keylen, "downloadLimited" ) )
    442446        tr_bencDictAddBool( d, key, tr_torrentUsesSpeedLimit( tor, TR_DOWN ) );
    443     else if( !strcmp( key, "error" ) )
     447    else if( tr_streq( key, keylen, "error" ) )
    444448        tr_bencDictAddInt( d, key, st->error );
    445     else if( !strcmp( key, "errorString" ) )
     449    else if( tr_streq( key, keylen, "errorString" ) )
    446450        tr_bencDictAddStr( d, key, st->errorString );
    447     else if( !strcmp( key, "eta" ) )
     451    else if( tr_streq( key, keylen, "eta" ) )
    448452        tr_bencDictAddInt( d, key, st->eta );
    449     else if( !strcmp( key, "files" ) )
     453    else if( tr_streq( key, keylen, "files" ) )
    450454        addFiles( tor, tr_bencDictAddList( d, key, inf->fileCount ) );
    451     else if( !strcmp( key, "fileStats" ) )
     455    else if( tr_streq( key, keylen, "fileStats" ) )
    452456        addFileStats( tor, tr_bencDictAddList( d, key, inf->fileCount ) );
    453     else if( !strcmp( key, "hashString" ) )
     457    else if( tr_streq( key, keylen, "hashString" ) )
    454458        tr_bencDictAddStr( d, key, tor->info.hashString );
    455     else if( !strcmp( key, "haveUnchecked" ) )
     459    else if( tr_streq( key, keylen, "haveUnchecked" ) )
    456460        tr_bencDictAddInt( d, key, st->haveUnchecked );
    457     else if( !strcmp( key, "haveValid" ) )
     461    else if( tr_streq( key, keylen, "haveValid" ) )
    458462        tr_bencDictAddInt( d, key, st->haveValid );
    459     else if( !strcmp( key, "honorsSessionLimits" ) )
     463    else if( tr_streq( key, keylen, "honorsSessionLimits" ) )
    460464        tr_bencDictAddBool( d, key, tr_torrentUsesSessionLimits( tor ) );
    461     else if( !strcmp( key, "id" ) )
     465    else if( tr_streq( key, keylen, "id" ) )
    462466        tr_bencDictAddInt( d, key, st->id );
    463     else if( !strcmp( key, "isPrivate" ) )
     467    else if( tr_streq( key, keylen, "isPrivate" ) )
    464468        tr_bencDictAddBool( d, key, tr_torrentIsPrivate( tor ) );
    465     else if( !strcmp( key, "lastAnnounceTime" ) )
     469    else if( tr_streq( key, keylen, "lastAnnounceTime" ) )
    466470        tr_bencDictAddInt( d, key, st->lastAnnounceTime );
    467     else if( !strcmp( key, "lastScrapeTime" ) )
     471    else if( tr_streq( key, keylen, "lastScrapeTime" ) )
    468472        tr_bencDictAddInt( d, key, st->lastScrapeTime );
    469     else if( !strcmp( key, "leechers" ) )
     473    else if( tr_streq( key, keylen, "leechers" ) )
    470474        tr_bencDictAddInt( d, key, st->leechers );
    471     else if( !strcmp( key, "leftUntilDone" ) )
     475    else if( tr_streq( key, keylen, "leftUntilDone" ) )
    472476        tr_bencDictAddInt( d, key, st->leftUntilDone );
    473     else if( !strcmp( key, "manualAnnounceTime" ) )
     477    else if( tr_streq( key, keylen, "manualAnnounceTime" ) )
    474478        tr_bencDictAddInt( d, key, st->manualAnnounceTime );
    475     else if( !strcmp( key, "maxConnectedPeers" ) )
     479    else if( tr_streq( key, keylen, "maxConnectedPeers" ) )
    476480        tr_bencDictAddInt( d, key,  tr_torrentGetPeerLimit( tor ) );
    477     else if( !strcmp( key, "name" ) )
     481    else if( tr_streq( key, keylen, "name" ) )
    478482        tr_bencDictAddStr( d, key, inf->name );
    479     else if( !strcmp( key, "nextAnnounceTime" ) )
     483    else if( tr_streq( key, keylen, "nextAnnounceTime" ) )
    480484        tr_bencDictAddInt( d, key, st->nextAnnounceTime );
    481     else if( !strcmp( key, "nextScrapeTime" ) )
     485    else if( tr_streq( key, keylen, "nextScrapeTime" ) )
    482486        tr_bencDictAddInt( d, key, st->nextScrapeTime );
    483     else if( !strcmp( key, "percentDone" ) )
     487    else if( tr_streq( key, keylen, "percentDone" ) )
    484488        tr_bencDictAddReal( d, key, st->percentDone );
    485     else if( !strcmp( key, "peer-limit" ) )
     489    else if( tr_streq( key, keylen, "peer-limit" ) )
    486490        tr_bencDictAddInt( d, key, tr_torrentGetPeerLimit( tor ) );
    487     else if( !strcmp( key, "peers" ) )
     491    else if( tr_streq( key, keylen, "peers" ) )
    488492        addPeers( tor, tr_bencDictAdd( d, key ) );
    489     else if( !strcmp( key, "peersConnected" ) )
     493    else if( tr_streq( key, keylen, "peersConnected" ) )
    490494        tr_bencDictAddInt( d, key, st->peersConnected );
    491     else if( !strcmp( key, "peersFrom" ) )
     495    else if( tr_streq( key, keylen, "peersFrom" ) )
    492496    {
    493497        tr_benc *   tmp = tr_bencDictAddDict( d, key, 4 );
     
    498502        tr_bencDictAddInt( tmp, "fromTracker",  f[TR_PEER_FROM_TRACKER] );
    499503    }
    500     else if( !strcmp( key, "peersGettingFromUs" ) )
     504    else if( tr_streq( key, keylen, "peersGettingFromUs" ) )
    501505        tr_bencDictAddInt( d, key, st->peersGettingFromUs );
    502     else if( !strcmp( key, "peersKnown" ) )
     506    else if( tr_streq( key, keylen, "peersKnown" ) )
    503507        tr_bencDictAddInt( d, key, st->peersKnown );
    504     else if( !strcmp( key, "peersSendingToUs" ) )
     508    else if( tr_streq( key, keylen, "peersSendingToUs" ) )
    505509        tr_bencDictAddInt( d, key, st->peersSendingToUs );
    506     else if( !strcmp( key, "pieces" ) ) {
     510    else if( tr_streq( key, keylen, "pieces" ) ) {
    507511        const tr_bitfield * pieces = tr_cpPieceBitfield( &tor->completion );
    508512        char * str = tr_base64_encode( pieces->bits, pieces->byteCount, NULL );
     
    510514        tr_free( str );
    511515    }
    512     else if( !strcmp( key, "pieceCount" ) )
     516    else if( tr_streq( key, keylen, "pieceCount" ) )
    513517        tr_bencDictAddInt( d, key, inf->pieceCount );
    514     else if( !strcmp( key, "pieceSize" ) )
     518    else if( tr_streq( key, keylen, "pieceSize" ) )
    515519        tr_bencDictAddInt( d, key, inf->pieceSize );
    516     else if( !strcmp( key, "priorities" ) )
     520    else if( tr_streq( key, keylen, "priorities" ) )
    517521    {
    518522        tr_file_index_t i;
     
    521525            tr_bencListAddInt( p, inf->files[i].priority );
    522526    }
    523     else if( !strcmp( key, "rateDownload" ) )
     527    else if( tr_streq( key, keylen, "rateDownload" ) )
    524528        tr_bencDictAddInt( d, key, (int)( st->pieceDownloadSpeed * 1024 ) );
    525     else if( !strcmp( key, "rateUpload" ) )
     529    else if( tr_streq( key, keylen, "rateUpload" ) )
    526530        tr_bencDictAddInt( d, key, (int)( st->pieceUploadSpeed * 1024 ) );
    527     else if( !strcmp( key, "recheckProgress" ) )
     531    else if( tr_streq( key, keylen, "recheckProgress" ) )
    528532        tr_bencDictAddReal( d, key, st->recheckProgress );
    529     else if( !strcmp( key, "scrapeResponse" ) )
     533    else if( tr_streq( key, keylen, "scrapeResponse" ) )
    530534        tr_bencDictAddStr( d, key, st->scrapeResponse );
    531     else if( !strcmp( key, "scrapeURL" ) )
     535    else if( tr_streq( key, keylen, "scrapeURL" ) )
    532536        tr_bencDictAddStr( d, key, st->scrapeURL );
    533     else if( !strcmp( key, "seeders" ) )
     537    else if( tr_streq( key, keylen, "seeders" ) )
    534538        tr_bencDictAddInt( d, key, st->seeders );
    535     else if( !strcmp( key, "seedRatioLimit" ) )
     539    else if( tr_streq( key, keylen, "seedRatioLimit" ) )
    536540        tr_bencDictAddReal( d, key, tr_torrentGetRatioLimit( tor ) );
    537     else if( !strcmp( key, "seedRatioMode" ) )
     541    else if( tr_streq( key, keylen, "seedRatioMode" ) )
    538542        tr_bencDictAddInt( d, key, tr_torrentGetRatioMode( tor ) );
    539     else if( !strcmp( key, "sizeWhenDone" ) )
     543    else if( tr_streq( key, keylen, "sizeWhenDone" ) )
    540544        tr_bencDictAddInt( d, key, st->sizeWhenDone );
    541     else if( !strcmp( key, "startDate" ) )
     545    else if( tr_streq( key, keylen, "startDate" ) )
    542546        tr_bencDictAddInt( d, key, st->startDate );
    543     else if( !strcmp( key, "status" ) )
     547    else if( tr_streq( key, keylen, "status" ) )
    544548        tr_bencDictAddInt( d, key, st->activity );
    545     else if( !strcmp( key, "swarmSpeed" ) )
     549    else if( tr_streq( key, keylen, "swarmSpeed" ) )
    546550        tr_bencDictAddInt( d, key, (int)( st->swarmSpeed * 1024 ) );
    547     else if( !strcmp( key, "timesCompleted" ) )
     551    else if( tr_streq( key, keylen, "timesCompleted" ) )
    548552        tr_bencDictAddInt( d, key, st->timesCompleted );
    549     else if( !strcmp( key, "trackers" ) )
     553    else if( tr_streq( key, keylen, "trackers" ) )
    550554        addTrackers( inf, tr_bencDictAddList( d, key, inf->trackerCount ) );
    551     else if( !strcmp( key, "torrentFile" ) )
     555    else if( tr_streq( key, keylen, "torrentFile" ) )
    552556        tr_bencDictAddStr( d, key, inf->torrent );
    553     else if( !strcmp( key, "totalSize" ) )
     557    else if( tr_streq( key, keylen, "totalSize" ) )
    554558        tr_bencDictAddInt( d, key, inf->totalSize );
    555     else if( !strcmp( key, "uploadedEver" ) )
     559    else if( tr_streq( key, keylen, "uploadedEver" ) )
    556560        tr_bencDictAddInt( d, key, st->uploadedEver );
    557     else if( !strcmp( key, "uploadLimit" ) )
     561    else if( tr_streq( key, keylen, "uploadLimit" ) )
    558562        tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit( tor, TR_UP ) );
    559     else if( !strcmp( key, "uploadLimited" ) )
     563    else if( tr_streq( key, keylen, "uploadLimited" ) )
    560564        tr_bencDictAddBool( d, key, tr_torrentUsesSpeedLimit( tor, TR_UP ) );
    561     else if( !strcmp( key, "uploadRatio" ) )
     565    else if( tr_streq( key, keylen, "uploadRatio" ) )
    562566        tr_bencDictAddReal( d, key, st->ratio );
    563     else if( !strcmp( key, "wanted" ) )
     567    else if( tr_streq( key, keylen, "wanted" ) )
    564568    {
    565569        tr_file_index_t i;
     
    568572            tr_bencListAddInt( w, inf->files[i].dnd ? 0 : 1 );
    569573    }
    570     else if( !strcmp( key, "webseeds" ) )
     574    else if( tr_streq( key, keylen, "webseeds" ) )
    571575        addWebseeds( inf, tr_bencDictAddList( d, key, inf->trackerCount ) );
    572     else if( !strcmp( key, "webseedsSendingToUs" ) )
     576    else if( tr_streq( key, keylen, "webseedsSendingToUs" ) )
    573577        tr_bencDictAddInt( d, key, st->webseedsSendingToUs );
    574578}
Note: See TracChangeset for help on using the changeset viewer.