diff --git a/src/routing_table.cpp b/src/routing_table.cpp index 5c1444c1d..495846b28 100644 --- a/src/routing_table.cpp +++ b/src/routing_table.cpp @@ -80,7 +80,8 @@ RoutingTable::depth(const RoutingTable::const_iterator& it) const std::vector> RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) const { - std::vector> nodes {}; + std::vector> nodes; + nodes.reserve(count); auto bucket = findBucket(id); if (bucket == end()) { return nodes; } @@ -100,7 +101,7 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) }; auto itn = bucket; - auto itp = std::prev(bucket); + auto itp = (bucket == begin()) ? end() : std::prev(bucket); while (nodes.size() < count && (itn != end() || itp != end())) { if (itn != end()) { sortedBucketInsert(*itn); @@ -108,11 +109,7 @@ RoutingTable::findClosestNodes(const InfoHash id, time_point now, size_t count) } if (itp != end()) { sortedBucketInsert(*itp); - if (itp == begin()) { - itp = end(); - continue; - } - itp = std::prev(itp); + itp = (itp == begin()) ? end() : std::prev(itp); } }