mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 23:07:59 +01:00
Use std::unordered_set instead of std::vector in IsEvicted()
An unordered set can tell if an element is present in ~O(1) time (constant on average, worst case linear to the size of the container), which speeds up and simplifies the lookup in IsEvicted(). Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
This commit is contained in:
parent
41f84d5ecc
commit
ca63b53ecd
1 changed files with 4 additions and 3 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <unordered_set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(net_peer_eviction_tests, BasicTestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(net_peer_eviction_tests, BasicTestingSetup)
|
||||||
|
@ -36,20 +37,20 @@ std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if any of the node ids in node_ids are selected for eviction.
|
// Returns true if any of the node ids in node_ids are selected for eviction.
|
||||||
bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, const std::vector<NodeId>& node_ids, FastRandomContext& random_context)
|
bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, const std::unordered_set<NodeId>& node_ids, FastRandomContext& random_context)
|
||||||
{
|
{
|
||||||
Shuffle(candidates.begin(), candidates.end(), random_context);
|
Shuffle(candidates.begin(), candidates.end(), random_context);
|
||||||
const std::optional<NodeId> evicted_node_id = SelectNodeToEvict(std::move(candidates));
|
const std::optional<NodeId> evicted_node_id = SelectNodeToEvict(std::move(candidates));
|
||||||
if (!evicted_node_id) {
|
if (!evicted_node_id) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return std::find(node_ids.begin(), node_ids.end(), *evicted_node_id) != node_ids.end();
|
return node_ids.count(*evicted_node_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create number_of_nodes random nodes, apply setup function candidate_setup_fn,
|
// Create number_of_nodes random nodes, apply setup function candidate_setup_fn,
|
||||||
// apply eviction logic and then return true if any of the node ids in node_ids
|
// apply eviction logic and then return true if any of the node ids in node_ids
|
||||||
// are selected for eviction.
|
// are selected for eviction.
|
||||||
bool IsEvicted(const int number_of_nodes, std::function<void(NodeEvictionCandidate&)> candidate_setup_fn, const std::vector<NodeId>& node_ids, FastRandomContext& random_context)
|
bool IsEvicted(const int number_of_nodes, std::function<void(NodeEvictionCandidate&)> candidate_setup_fn, const std::unordered_set<NodeId>& node_ids, FastRandomContext& random_context)
|
||||||
{
|
{
|
||||||
std::vector<NodeEvictionCandidate> candidates = GetRandomNodeEvictionCandidates(number_of_nodes, random_context);
|
std::vector<NodeEvictionCandidate> candidates = GetRandomNodeEvictionCandidates(number_of_nodes, random_context);
|
||||||
for (NodeEvictionCandidate& candidate : candidates) {
|
for (NodeEvictionCandidate& candidate : candidates) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue