Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | size_t selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | ||||
// Try to find the slot using dichotomic search. | // Try to find the slot using dichotomic search. | ||||
while ((end - begin) > 8) { | while ((end - begin) > 8) { | ||||
// The slot we picked in not allocated. | // The slot we picked in not allocated. | ||||
if (slot < bottom || slot >= top) { | if (slot < bottom || slot >= top) { | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
// Guesstimate the position of the slot. | // Guesstimate the position of the slot. | ||||
size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom)); | size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom)); | ||||
deadalnix: Adding an assert that i is in the right range is a good idea here. | |||||
// 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 undershot. | ||||
if (slots[i].precedes(slot)) { | if (slots[i].precedes(slot)) { | ||||
begin = i + 1; | begin = i + 1; | ||||
if (begin >= slots.size()) { | |||||
FabienUnsubmitted Not Done Inline Actionsslots.size() could be end here, and you could simply return NO_PEER instead of a break. In the end the result is the same: after the break you will exit the while loop, jump over the for loop (because end - begin is necessarily <= 0) and return NO_PEER anyway. Fabien: `slots.size()` could be `end` here, and you could simply return `NO_PEER` instead of a `break`. | |||||
deadalnixUnsubmitted Not Done Inline ActionsIndeed, this has nothing to do with size and everything to do with the need to early bail. deadalnix: Indeed, this has nothing to do with size and everything to do with the need to early bail. | |||||
break; | |||||
} | |||||
bottom = slots[begin].start; | bottom = slots[begin].start; | ||||
continue; | continue; | ||||
} | } | ||||
// We overshooted. | // We overshot. | ||||
if (slots[i].follows(slot)) { | if (slots[i].follows(slot)) { | ||||
end = i; | end = i; | ||||
top = slots[end].start; | top = slots[end].start; | ||||
continue; | continue; | ||||
} | } | ||||
// We have an unalocated slot. | // We have an unallocated 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. | ||||
for (size_t i = begin; i < end; i++) { | for (size_t i = begin; i < end; i++) { | ||||
// We have a match. | // We have a match. | ||||
if (slots[i].contains(slot)) { | if (slots[i].contains(slot)) { | ||||
return i; | return i; | ||||
} | } | ||||
} | } | ||||
// We failed to find a slot, retry. | // We failed to find a slot, retry. | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } |
Adding an assert that i is in the right range is a good idea here.