From 4d8431907d050cb1340f1ca22fb86e7004a2e1d8 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 3 May 2024 19:09:31 -0400 Subject: [PATCH] use std::sample for exploratory selection if C++17 --- libi2pd/NetDb.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 7cae9bca..76b460a9 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -1457,12 +1457,18 @@ namespace data size_t num, const std::set& excluded) { std::vector ret; - if (!num) return ret; // empty list + if (!num || m_RouterInfos.empty ()) return ret; // empty list auto ts = i2p::util::GetMonotonicSeconds (); if (ts > m_LastExploratorySelectionUpdateTime + NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL) { // update selection m_ExploratorySelection.clear (); +#if (__cplusplus >= 201703L) // C++ 17 or higher + std::vector > eligible; + eligible.reserve (m_RouterInfos.size ()); +#else + auto& eligible = m_ExploratorySelection; +#endif { // collect eligible from current netdb bool checkIsReal = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < NETDB_TUNNEL_CREATION_RATE_THRESHOLD; // too low rate @@ -1470,14 +1476,24 @@ namespace data for (const auto& it: m_RouterInfos) if (!it.second->IsDeclaredFloodfill () && (!checkIsReal || (it.second->HasProfile () && it.second->GetProfile ()->IsReal ()))) - m_ExploratorySelection.push_back (it.second); + eligible.push_back (it.second); } +#if (__cplusplus >= 201703L) // C++ 17 or higher + if (eligible.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE) + { + std::sample (eligible.begin(), eligible.end(), std::back_inserter(m_ExploratorySelection), + NETDB_MAX_EXPLORATORY_SELECTION_SIZE, std::mt19937(std::random_device()())); + } + else + std::swap (m_ExploratorySelection, eligible); +#else if (m_ExploratorySelection.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE) { // reduce number of eligible to max selection size std::shuffle (m_ExploratorySelection.begin(), m_ExploratorySelection.end(), std::mt19937(std::random_device()())); m_ExploratorySelection.resize (NETDB_MAX_EXPLORATORY_SELECTION_SIZE); } +#endif m_LastExploratorySelectionUpdateTime = ts; }