Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show All 10 Lines | void PeerManager::addPeer(uint64_t score) { | ||||
const uint64_t stop = start + score; | const uint64_t stop = start + score; | ||||
slots.emplace_back(start, stop); | slots.emplace_back(start, stop); | ||||
slotCount = stop; | slotCount = stop; | ||||
} | } | ||||
void PeerManager::rescorePeer(size_t i, uint64_t score) { | void PeerManager::rescorePeer(size_t i, uint64_t score) { | ||||
assert(i < slots.size()); | assert(i < slots.size()); | ||||
const uint64_t start = slots[i].start; | const uint64_t start = slots[i].getStart(); | ||||
const uint64_t stop = start + score; | |||||
// If this is the last element, we can extend/shrink easily. | // If this is the last element, we can extend/shrink easily. | ||||
if (i + 1 == slots.size()) { | if (i + 1 == slots.size()) { | ||||
slots[i].stop = stop; | slots[i] = slots[i].withScore(score); | ||||
slotCount = stop; | slotCount = slots[i].getStop(); | ||||
return; | return; | ||||
} | } | ||||
const uint64_t nextStart = slots[i + 1].start; | const uint64_t stop = start + score; | ||||
const uint64_t nextStart = slots[i + 1].getStart(); | |||||
// We can extend in place. | // We can extend in place. | ||||
if (stop <= nextStart) { | if (stop <= nextStart) { | ||||
fragmentation += (slots[i].stop - stop); | fragmentation += (slots[i].getStop() - stop); | ||||
slots[i].stop = stop; | slots[i] = slots[i].withScore(score); | ||||
return; | return; | ||||
} | } | ||||
// So we remove and insert a new entry. | // So we remove and insert a new entry. | ||||
addPeer(score); | addPeer(score); | ||||
removePeer(i); | removePeer(i); | ||||
} | } | ||||
Show All 31 Lines | while ((end - begin) > 8) { | ||||
// We have a match. | // We have a match. | ||||
if (slots[i].contains(slot)) { | if (slots[i].contains(slot)) { | ||||
return i; | return i; | ||||
} | } | ||||
// We undershooted. | // We undershooted. | ||||
if (slots[i].precedes(slot)) { | if (slots[i].precedes(slot)) { | ||||
begin = i + 1; | begin = i + 1; | ||||
bottom = slots[begin].start; | bottom = slots[begin].getStart(); | ||||
continue; | continue; | ||||
} | } | ||||
// We overshooted. | // We overshooted. | ||||
if (slots[i].follows(slot)) { | if (slots[i].follows(slot)) { | ||||
end = i; | end = i; | ||||
top = slots[end].start; | top = slots[end].getStart(); | ||||
continue; | continue; | ||||
} | } | ||||
// We have an unalocated slot. | // We have an unalocated slot. | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
// Enough of that nonsense, let fallback to linear search. | // Enough of that nonsense, let fallback to linear search. | ||||
Show All 10 Lines |