mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 13:27:17 +01:00
use memory pool for SSU2 received packets arrays
This commit is contained in:
parent
64e4b3871a
commit
62b811c2c1
|
@ -368,9 +368,9 @@ namespace transport
|
|||
size_t moreBytes = socket.available (ec);
|
||||
if (!ec && moreBytes)
|
||||
{
|
||||
std::vector<Packet *> packets;
|
||||
packets.push_back (packet);
|
||||
while (moreBytes && packets.size () < SSU2_MAX_NUM_PACKETS_PER_BATCH)
|
||||
auto packets = m_PacketsArrayPool.AcquireMt ();
|
||||
packets->AddPacket (packet);
|
||||
while (moreBytes && packets->numPackets < SSU2_MAX_NUM_PACKETS_PER_BATCH)
|
||||
{
|
||||
packet = m_PacketsPool.AcquireMt ();
|
||||
packet->len = socket.receive_from (boost::asio::buffer (packet->buf, SSU2_MAX_PACKET_SIZE), packet->from, 0, ec);
|
||||
|
@ -378,7 +378,13 @@ namespace transport
|
|||
{
|
||||
i2p::transport::transports.UpdateReceivedBytes (packet->len);
|
||||
if (packet->len >= SSU2_MIN_RECEIVED_PACKET_SIZE)
|
||||
packets.push_back (packet);
|
||||
{
|
||||
if (!packets->AddPacket (packet))
|
||||
{
|
||||
LogPrint (eLogError, "SSU2: Received packets array is full");
|
||||
m_PacketsPool.ReleaseMt (packet);
|
||||
}
|
||||
}
|
||||
else // drop too short packets
|
||||
m_PacketsPool.ReleaseMt (packet);
|
||||
moreBytes = socket.available(ec);
|
||||
|
@ -391,10 +397,7 @@ namespace transport
|
|||
break;
|
||||
}
|
||||
}
|
||||
GetService ().post ([packets = std::move (packets), this]() mutable
|
||||
{
|
||||
HandleReceivedPackets (std::move (packets));
|
||||
});
|
||||
GetService ().post (std::bind (&SSU2Server::HandleReceivedPackets, this, packets));
|
||||
}
|
||||
else
|
||||
GetService ().post (std::bind (&SSU2Server::HandleReceivedPacket, this, packet));
|
||||
|
@ -438,15 +441,23 @@ namespace transport
|
|||
}
|
||||
}
|
||||
|
||||
void SSU2Server::HandleReceivedPackets (std::vector<Packet *>&& packets)
|
||||
void SSU2Server::HandleReceivedPackets (Packets * packets)
|
||||
{
|
||||
if (!packets) return;
|
||||
if (m_IsThroughProxy)
|
||||
for (auto& packet: packets)
|
||||
for (size_t i = 0; i < packets->numPackets; i++)
|
||||
{
|
||||
auto& packet = (*packets)[i];
|
||||
ProcessNextPacketFromProxy (packet->buf, packet->len);
|
||||
}
|
||||
else
|
||||
for (auto& packet: packets)
|
||||
for (size_t i = 0; i < packets->numPackets; i++)
|
||||
{
|
||||
auto& packet = (*packets)[i];
|
||||
ProcessNextPacket (packet->buf, packet->len, packet->from);
|
||||
m_PacketsPool.ReleaseMt (std::move (packets));
|
||||
}
|
||||
m_PacketsPool.ReleaseMt (packets->data (), packets->numPackets);
|
||||
m_PacketsArrayPool.ReleaseMt (packets);
|
||||
if (m_LastSession && m_LastSession->GetState () != eSSU2SessionStateTerminated)
|
||||
m_LastSession->FlushData ();
|
||||
}
|
||||
|
@ -1098,6 +1109,7 @@ namespace transport
|
|||
}
|
||||
|
||||
m_PacketsPool.CleanUpMt ();
|
||||
m_PacketsArrayPool.CleanUpMt ();
|
||||
m_SentPacketsPool.CleanUp ();
|
||||
m_IncompleteMessagesPool.CleanUp ();
|
||||
m_FragmentsPool.CleanUp ();
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include "util.h"
|
||||
|
@ -51,6 +52,20 @@ namespace transport
|
|||
boost::asio::ip::udp::endpoint from;
|
||||
};
|
||||
|
||||
struct Packets: public std::array<Packet *, SSU2_MAX_NUM_PACKETS_PER_BATCH>
|
||||
{
|
||||
size_t numPackets = 0;
|
||||
bool AddPacket (Packet *p)
|
||||
{
|
||||
if (p && numPackets < size ())
|
||||
{
|
||||
data()[numPackets] = p; numPackets++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class ReceiveService: public i2p::util::RunnableService
|
||||
{
|
||||
public:
|
||||
|
@ -127,7 +142,7 @@ namespace transport
|
|||
void HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred,
|
||||
Packet * packet, boost::asio::ip::udp::socket& socket);
|
||||
void HandleReceivedPacket (Packet * packet);
|
||||
void HandleReceivedPackets (std::vector<Packet *>&& packets);
|
||||
void HandleReceivedPackets (Packets * packets);
|
||||
void ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
||||
|
||||
void ScheduleTermination ();
|
||||
|
@ -172,6 +187,7 @@ namespace transport
|
|||
std::unordered_map<uint32_t, std::weak_ptr<SSU2Session> > m_Relays; // we are introducer, relay tag -> session
|
||||
std::list<std::pair<i2p::data::IdentHash, uint32_t> > m_Introducers, m_IntroducersV6; // introducers we are connected to
|
||||
i2p::util::MemoryPoolMt<Packet> m_PacketsPool;
|
||||
i2p::util::MemoryPoolMt<Packets> m_PacketsArrayPool;
|
||||
i2p::util::MemoryPool<SSU2SentPacket> m_SentPacketsPool;
|
||||
i2p::util::MemoryPool<SSU2IncompleteMessage> m_IncompleteMessagesPool;
|
||||
i2p::util::MemoryPool<SSU2IncompleteMessage::Fragment> m_FragmentsPool;
|
||||
|
|
|
@ -131,14 +131,22 @@ namespace util
|
|||
this->Release (t);
|
||||
}
|
||||
|
||||
void ReleaseMt (T * * arr, size_t num)
|
||||
{
|
||||
if (!arr || !num) return;
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
for (size_t i = 0; i < num; i++)
|
||||
this->Release (arr[i]);
|
||||
}
|
||||
|
||||
template<template<typename, typename...>class C, typename... R>
|
||||
void ReleaseMt(C<T *, R...>&& c)
|
||||
void ReleaseMt(const C<T *, R...>& c)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
for (auto& it: c)
|
||||
this->Release (it);
|
||||
}
|
||||
|
||||
|
||||
template<typename... TArgs>
|
||||
std::shared_ptr<T> AcquireSharedMt (TArgs&&... args)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue