From ca63b53ecdf377ce777fd959d400748912266748 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 28 Feb 2021 15:53:07 +0100 Subject: [PATCH] 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 --- src/test/net_peer_eviction_tests.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/net_peer_eviction_tests.cpp b/src/test/net_peer_eviction_tests.cpp index b80beeac5a..290a6b7eae 100644 --- a/src/test/net_peer_eviction_tests.cpp +++ b/src/test/net_peer_eviction_tests.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include BOOST_FIXTURE_TEST_SUITE(net_peer_eviction_tests, BasicTestingSetup) @@ -36,20 +37,20 @@ std::vector GetRandomNodeEvictionCandidates(const int n_c } // Returns true if any of the node ids in node_ids are selected for eviction. -bool IsEvicted(std::vector candidates, const std::vector& node_ids, FastRandomContext& random_context) +bool IsEvicted(std::vector candidates, const std::unordered_set& node_ids, FastRandomContext& random_context) { Shuffle(candidates.begin(), candidates.end(), random_context); const std::optional evicted_node_id = SelectNodeToEvict(std::move(candidates)); if (!evicted_node_id) { 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, // apply eviction logic and then return true if any of the node ids in node_ids // are selected for eviction. -bool IsEvicted(const int number_of_nodes, std::function candidate_setup_fn, const std::vector& node_ids, FastRandomContext& random_context) +bool IsEvicted(const int number_of_nodes, std::function candidate_setup_fn, const std::unordered_set& node_ids, FastRandomContext& random_context) { std::vector candidates = GetRandomNodeEvictionCandidates(number_of_nodes, random_context); for (NodeEvictionCandidate& candidate : candidates) {