From 949c38f5f00d3cab83ca5957e9132098d0edb890 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 28 Nov 2022 19:16:21 -0500 Subject: [PATCH] reuse list of addresses --- libi2pd/RouterInfo.cpp | 13 ++++++++----- libi2pd/RouterInfo.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index fb952c77..580d1f74 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -206,10 +206,9 @@ namespace data s.read ((char *)&m_Timestamp, sizeof (m_Timestamp)); m_Timestamp = be64toh (m_Timestamp); // read addresses - auto addresses = boost::make_shared(); + if (!m_NewAddresses) m_NewAddresses = boost::make_shared(); uint8_t numAddresses; s.read ((char *)&numAddresses, sizeof (numAddresses)); - addresses->reserve (numAddresses); for (int i = 0; i < numAddresses; i++) { uint8_t supportedTransports = 0; @@ -418,15 +417,19 @@ namespace data if (supportedTransports) { if (!(m_SupportedTransports & supportedTransports)) // avoid duplicates - addresses->push_back(address); + m_NewAddresses->push_back(address); m_SupportedTransports |= supportedTransports; } } + // update addresses + auto prev = m_Addresses; #if (BOOST_VERSION >= 105300) - boost::atomic_store (&m_Addresses, addresses); + boost::atomic_store (&m_Addresses, m_NewAddresses); #else - m_Addresses = addresses; // race condition + m_Addresses = m_NewAddresses; // race condition #endif + if (prev) prev->clear (); + m_NewAddresses = prev; // read peers uint8_t numPeers; s.read ((char *)&numPeers, sizeof (numPeers)); if (!s) return; diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index f3c13a78..6e122d95 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -282,7 +282,7 @@ namespace data std::shared_ptr m_Buffer; size_t m_BufferLen; uint64_t m_Timestamp; - boost::shared_ptr m_Addresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9 + boost::shared_ptr m_Addresses, m_NewAddresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9 bool m_IsUpdated, m_IsUnreachable; CompatibleTransports m_SupportedTransports, m_ReachableTransports; uint8_t m_Caps;