From 53ca5dc67a92ae0e334f23b084e55533245a3354 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 7 Dec 2022 14:08:27 -0500 Subject: [PATCH] memory pool for address vectors --- libi2pd/NetDb.cpp | 3 ++- libi2pd/NetDb.hpp | 12 ++++++++++-- libi2pd/RouterInfo.cpp | 14 ++++++-------- libi2pd/RouterInfo.h | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index e69de0d5..bf9d60de 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -649,7 +649,8 @@ namespace data } // m_RouterInfos iteration m_RouterInfoBuffersPool.CleanUpMt (); - // m_RouterInfoAddressesPool.CleanUpMt (); + m_RouterInfoAddressesPool.CleanUpMt (); + m_RouterInfoAddressVectorsPool.CleanUpMt (); if (updatedCount > 0) LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers"); diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 312792e5..98b5fd86 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -123,7 +123,14 @@ namespace data void ClearRouterInfos () { m_RouterInfos.clear (); }; std::shared_ptr NewRouterInfoBuffer () { return m_RouterInfoBuffersPool.AcquireSharedMt (); }; void PopulateRouterInfoBuffer (std::shared_ptr r); - //std::shared_ptr NewRouterInfoAddress () { return m_RouterInfoAddressesPool.AcquireSharedMt (); }; + std::shared_ptr NewRouterInfoAddress () { return m_RouterInfoAddressesPool.AcquireSharedMt (); }; + boost::shared_ptr NewRouterInfoAddresses () + { + return boost::shared_ptr(m_RouterInfoAddressVectorsPool.AcquireMt (), + std::bind ::*)(RouterInfo::Addresses *)> + (&i2p::util::MemoryPoolMt::ReleaseMt, + &m_RouterInfoAddressVectorsPool, std::placeholders::_1)); + }; std::shared_ptr NewLease (const Lease& lease) { return m_LeasesPool.AcquireSharedMt (lease); }; uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; }; @@ -181,7 +188,8 @@ namespace data uint32_t m_PublishReplyToken = 0; i2p::util::MemoryPoolMt m_RouterInfoBuffersPool; - //i2p::util::MemoryPoolMt m_RouterInfoAddressesPool; + i2p::util::MemoryPoolMt m_RouterInfoAddressesPool; + i2p::util::MemoryPoolMt m_RouterInfoAddressVectorsPool; i2p::util::MemoryPoolMt m_LeasesPool; }; diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index 7ca72a63..13a0731e 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -206,13 +206,14 @@ namespace data s.read ((char *)&m_Timestamp, sizeof (m_Timestamp)); m_Timestamp = be64toh (m_Timestamp); // read addresses - if (!m_NewAddresses) m_NewAddresses = boost::make_shared(); + auto addresses = netdb.NewRouterInfoAddresses (); uint8_t numAddresses; s.read ((char *)&numAddresses, sizeof (numAddresses)); + addresses->reserve (numAddresses); for (int i = 0; i < numAddresses; i++) { uint8_t supportedTransports = 0; - auto address = std::make_shared
(); // netdb.NewRouterInfoAddress (); + auto address = netdb.NewRouterInfoAddress (); uint8_t cost; // ignore s.read ((char *)&cost, sizeof (cost)); s.read ((char *)&address->date, sizeof (address->date)); @@ -417,19 +418,16 @@ namespace data if (supportedTransports) { if (!(m_SupportedTransports & supportedTransports)) // avoid duplicates - m_NewAddresses->push_back(address); + addresses->push_back(address); m_SupportedTransports |= supportedTransports; } } // update addresses - auto prev = m_Addresses; #if (BOOST_VERSION >= 105300) - boost::atomic_store (&m_Addresses, m_NewAddresses); + boost::atomic_store (&m_Addresses, addresses); #else - m_Addresses = m_NewAddresses; // race condition + m_Addresses = addresses; // 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 18e2b336..9e98769f 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -281,7 +281,7 @@ namespace data std::shared_ptr m_Buffer; size_t m_BufferLen; uint64_t m_Timestamp; - boost::shared_ptr m_Addresses, m_NewAddresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9 + boost::shared_ptr m_Addresses; // 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;