diff --git a/ChangeLog b/ChangeLog index cb53b796..2d8f9dab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-03-20 Tatsuhiro Tsujikawa + + Refactored getMissingIndexRandomly, getFirstMissingIndex, + getAllMissingBlockIndexes to reduce a call to + array_function_base::operator[]. + * src/BitfieldMan.cc + 2009-03-20 Tatsuhiro Tsujikawa Cached TransferStat/download speed to avoid to sum them up in each diff --git a/src/BitfieldMan.cc b/src/BitfieldMan.cc index 7421b016..444e93a0 100644 --- a/src/BitfieldMan.cc +++ b/src/BitfieldMan.cc @@ -212,8 +212,10 @@ bool BitfieldMan::getMissingIndexRandomly(size_t& index, } else { mask = 0xff; } - if(bitfield[byte]&mask) { - index = byte*8+getNthBitIndex(bitfield[byte], 1); + unsigned char bits = bitfield[byte]; + + if(bits&mask) { + index = byte*8+getNthBitIndex(bits, 1); return true; } byte++; @@ -270,11 +272,12 @@ template bool BitfieldMan::getFirstMissingIndex(size_t& index, const Array& bitfield, size_t bitfieldLength) const { for(size_t i = 0; i < bitfieldLength; ++i) { - size_t base = i*8; - for(size_t bi = 0; bi < 8 && base+bi < blocks; ++bi) { - unsigned char mask = 128 >> bi; - if(bitfield[i] & mask) { - index = base+bi; + unsigned char bits = bitfield[i]; + unsigned char mask = 128; + size_t tindex = i*8; + for(size_t bi = 0; bi < 8 && tindex < blocks; ++bi, mask >>= 1, ++tindex) { + if(bits & mask) { + index = tindex; return true; } } @@ -395,11 +398,12 @@ bool BitfieldMan::getAllMissingIndexes(std::deque& indexes, size_t bitfieldLength) const { for(size_t i = 0; i < bitfieldLength; ++i) { - size_t base = i*8; - for(size_t bi = 0; bi < 8 && base+bi < blocks; ++bi) { - unsigned char mask = 128 >> bi; - if(bitfield[i] & mask) { - indexes.push_back(base+bi); + unsigned char bits = bitfield[i]; + unsigned char mask = 128; + size_t index = i*8; + for(size_t bi = 0; bi < 8 && index < blocks; ++bi, mask >>= 1, ++index) { + if(bits & mask) { + indexes.push_back(index); } } }