Browse Source

copy elimination for ranges #part2

pull/595/head
brain5lug 8 years ago
parent
commit
8b53ded53a
  1. 6
      NTCPSession.cpp
  2. 14
      RouterContext.cpp
  3. 46
      RouterInfo.cpp
  4. 6
      RouterInfo.h

6
NTCPSession.cpp

@ -666,7 +666,7 @@ namespace transport
{ {
m_IsSending = true; m_IsSending = true;
std::vector<boost::asio::const_buffer> bufs; std::vector<boost::asio::const_buffer> bufs;
for (auto it: msgs) for (const auto& it: msgs)
bufs.push_back (CreateMsgBuffer (it)); bufs.push_back (CreateMsgBuffer (it));
boost::asio::async_write (m_Socket, bufs, boost::asio::transfer_all (), boost::asio::async_write (m_Socket, bufs, boost::asio::transfer_all (),
std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msgs)); std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msgs));
@ -716,7 +716,7 @@ namespace transport
{ {
if (m_SendQueue.size () < NTCP_MAX_OUTGOING_QUEUE_SIZE) if (m_SendQueue.size () < NTCP_MAX_OUTGOING_QUEUE_SIZE)
{ {
for (auto it: msgs) for (const auto& it: msgs)
m_SendQueue.push_back (it); m_SendQueue.push_back (it);
} }
else else
@ -767,7 +767,7 @@ namespace transport
m_Thread = new std::thread (std::bind (&NTCPServer::Run, this)); m_Thread = new std::thread (std::bind (&NTCPServer::Run, this));
// create acceptors // create acceptors
auto& addresses = context.GetRouterInfo ().GetAddresses (); auto& addresses = context.GetRouterInfo ().GetAddresses ();
for (auto address: addresses) for (const auto& address: addresses)
{ {
if (address->transportStyle == i2p::data::RouterInfo::eTransportNTCP) if (address->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
{ {

14
RouterContext.cpp

@ -112,7 +112,7 @@ namespace i2p
void RouterContext::UpdatePort (int port) void RouterContext::UpdatePort (int port)
{ {
bool updated = false; bool updated = false;
for (auto address : m_RouterInfo.GetAddresses ()) for (auto& address : m_RouterInfo.GetAddresses ())
{ {
if (address->port != port) if (address->port != port)
{ {
@ -127,7 +127,7 @@ namespace i2p
void RouterContext::UpdateAddress (const boost::asio::ip::address& host) void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
{ {
bool updated = false; bool updated = false;
for (auto address : m_RouterInfo.GetAddresses ()) for (auto& address : m_RouterInfo.GetAddresses ())
{ {
if (address->host != host && address->IsCompatible (host)) if (address->host != host && address->IsCompatible (host))
{ {
@ -244,7 +244,7 @@ namespace i2p
m_RouterInfo.SetCaps (i2p::data::RouterInfo::eUnreachable | i2p::data::RouterInfo::eSSUTesting); // LU, B m_RouterInfo.SetCaps (i2p::data::RouterInfo::eUnreachable | i2p::data::RouterInfo::eSSUTesting); // LU, B
// remove NTCP address // remove NTCP address
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
for (auto it = addresses.begin (); it != addresses.end (); it++) for (auto it = addresses.begin (); it != addresses.end (); ++it)
{ {
if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP &&
(*it)->host.is_v4 ()) (*it)->host.is_v4 ())
@ -254,7 +254,7 @@ namespace i2p
} }
} }
// delete previous introducers // delete previous introducers
for (auto addr : addresses) for (auto& addr : addresses)
addr->introducers.clear (); addr->introducers.clear ();
// update // update
@ -274,7 +274,7 @@ namespace i2p
// insert NTCP back // insert NTCP back
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
for (auto addr : addresses) for (const auto& addr : addresses)
{ {
if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU && if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
addr->host.is_v4 ()) addr->host.is_v4 ())
@ -285,7 +285,7 @@ namespace i2p
} }
} }
// delete previous introducers // delete previous introducers
for (auto addr : addresses) for (auto& addr : addresses)
addr->introducers.clear (); addr->introducers.clear ();
// update // update
@ -316,7 +316,7 @@ namespace i2p
bool updated = false, found = false; bool updated = false, found = false;
int port = 0; int port = 0;
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
for (auto addr: addresses) for (auto& addr: addresses)
{ {
if (addr->host.is_v6 () && addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP) if (addr->host.is_v6 () && addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
{ {

46
RouterInfo.cpp

@ -369,19 +369,19 @@ namespace data
SetProperty ("caps", caps); SetProperty ("caps", caps);
} }
void RouterInfo::WriteToStream (std::ostream& s) void RouterInfo::WriteToStream (std::ostream& s) const
{ {
uint64_t ts = htobe64 (m_Timestamp); uint64_t ts = htobe64 (m_Timestamp);
s.write ((char *)&ts, sizeof (ts)); s.write ((const char *)&ts, sizeof (ts));
// addresses // addresses
uint8_t numAddresses = m_Addresses->size (); uint8_t numAddresses = m_Addresses->size ();
s.write ((char *)&numAddresses, sizeof (numAddresses)); s.write ((char *)&numAddresses, sizeof (numAddresses));
for (auto addr : *m_Addresses) for (const auto& addr_ptr : *m_Addresses)
{ {
Address& address = *addr; const Address& address = *addr_ptr;
s.write ((char *)&address.cost, sizeof (address.cost)); s.write ((const char *)&address.cost, sizeof (address.cost));
s.write ((char *)&address.date, sizeof (address.date)); s.write ((const char *)&address.date, sizeof (address.date));
std::stringstream properties; std::stringstream properties;
if (address.transportStyle == eTransportNTCP) if (address.transportStyle == eTransportNTCP)
WriteString ("NTCP", s); WriteString ("NTCP", s);
@ -410,7 +410,7 @@ namespace data
if (address.introducers.size () > 0) if (address.introducers.size () > 0)
{ {
int i = 0; int i = 0;
for (auto introducer: address.introducers) for (const auto& introducer: address.introducers)
{ {
WriteString ("ihost" + boost::lexical_cast<std::string>(i), properties); WriteString ("ihost" + boost::lexical_cast<std::string>(i), properties);
properties << '='; properties << '=';
@ -419,7 +419,7 @@ namespace data
i++; i++;
} }
i = 0; i = 0;
for (auto introducer: address.introducers) for (const auto& introducer: address.introducers)
{ {
WriteString ("ikey" + boost::lexical_cast<std::string>(i), properties); WriteString ("ikey" + boost::lexical_cast<std::string>(i), properties);
properties << '='; properties << '=';
@ -431,7 +431,7 @@ namespace data
i++; i++;
} }
i = 0; i = 0;
for (auto introducer: address.introducers) for (const auto& introducer: address.introducers)
{ {
WriteString ("iport" + boost::lexical_cast<std::string>(i), properties); WriteString ("iport" + boost::lexical_cast<std::string>(i), properties);
properties << '='; properties << '=';
@ -440,7 +440,7 @@ namespace data
i++; i++;
} }
i = 0; i = 0;
for (auto introducer: address.introducers) for (const auto& introducer: address.introducers)
{ {
WriteString ("itag" + boost::lexical_cast<std::string>(i), properties); WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
properties << '='; properties << '=';
@ -482,7 +482,7 @@ namespace data
// properties // properties
std::stringstream properties; std::stringstream properties;
for (auto& p : m_Properties) for (const auto& p : m_Properties)
{ {
WriteString (p.first, properties); WriteString (p.first, properties);
properties << '='; properties << '=';
@ -570,10 +570,10 @@ namespace data
addr->cost = 2; addr->cost = 2;
addr->date = 0; addr->date = 0;
addr->mtu = 0; addr->mtu = 0;
for (auto it: *m_Addresses) // don't insert same address twice for (const auto& it: *m_Addresses) // don't insert same address twice
if (*it == *addr) return; if (*it == *addr) return;
m_Addresses->push_back(addr);
m_SupportedTransports |= addr->host.is_v6 () ? eNTCPV6 : eNTCPV4; m_SupportedTransports |= addr->host.is_v6 () ? eNTCPV6 : eNTCPV4;
m_Addresses->push_back(std::move(addr));
} }
void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu) void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu)
@ -586,21 +586,22 @@ namespace data
addr->date = 0; addr->date = 0;
addr->mtu = mtu; addr->mtu = mtu;
memcpy (addr->key, key, 32); memcpy (addr->key, key, 32);
for (auto it: *m_Addresses) // don't insert same address twice for (const auto& it: *m_Addresses) // don't insert same address twice
if (*it == *addr) return; if (*it == *addr) return;
m_Addresses->push_back(addr);
m_SupportedTransports |= addr->host.is_v6 () ? eSSUV6 : eSSUV4; m_SupportedTransports |= addr->host.is_v6 () ? eSSUV6 : eSSUV4;
m_Addresses->push_back(std::move(addr));
m_Caps |= eSSUTesting; m_Caps |= eSSUTesting;
m_Caps |= eSSUIntroducer; m_Caps |= eSSUIntroducer;
} }
bool RouterInfo::AddIntroducer (const Introducer& introducer) bool RouterInfo::AddIntroducer (const Introducer& introducer)
{ {
for (auto addr : *m_Addresses) for (auto& addr : *m_Addresses)
{ {
if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ()) if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ())
{ {
for (auto intro: addr->introducers) for (auto& intro: addr->introducers)
if (intro.iTag == introducer.iTag) return false; // already presented if (intro.iTag == introducer.iTag) return false; // already presented
addr->introducers.push_back (introducer); addr->introducers.push_back (introducer);
return true; return true;
@ -611,11 +612,11 @@ namespace data
bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e) bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
{ {
for (auto addr: *m_Addresses) for (auto& addr: *m_Addresses)
{ {
if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ()) if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ())
{ {
for (std::vector<Introducer>::iterator it = addr->introducers.begin (); it != addr->introducers.end (); it++) for (auto it = addr->introducers.begin (); it != addr->introducers.end (); ++it)
if ( boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e) if ( boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e)
{ {
addr->introducers.erase (it); addr->introducers.erase (it);
@ -707,7 +708,7 @@ namespace data
if (addr->host.is_v6 ()) if (addr->host.is_v6 ())
it = m_Addresses->erase (it); it = m_Addresses->erase (it);
else else
it++; ++it;
} }
} }
} }
@ -723,7 +724,7 @@ namespace data
if (addr->host.is_v4 ()) if (addr->host.is_v4 ())
it = m_Addresses->erase (it); it = m_Addresses->erase (it);
else else
it++; ++it;
} }
} }
} }
@ -751,8 +752,7 @@ namespace data
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const
{ {
auto addresses = m_Addresses; for (const auto& address : *m_Addresses)
for (auto address : *addresses)
{ {
if (address->transportStyle == s) if (address->transportStyle == s)
{ {

6
RouterInfo.h

@ -187,9 +187,9 @@ namespace data
void ReadFromFile (); void ReadFromFile ();
void ReadFromStream (std::istream& s); void ReadFromStream (std::istream& s);
void ReadFromBuffer (bool verifySignature); void ReadFromBuffer (bool verifySignature);
void WriteToStream (std::ostream& s); void WriteToStream (std::ostream& s) const;
size_t ReadString (char * str, std::istream& s); static size_t ReadString (char* str, std::istream& s);
void WriteString (const std::string& str, std::ostream& s); static void WriteString (const std::string& str, std::ostream& s);
void ExtractCaps (const char * value); void ExtractCaps (const char * value);
std::shared_ptr<const Address> GetAddress (TransportStyle s, bool v4only, bool v6only = false) const; std::shared_ptr<const Address> GetAddress (TransportStyle s, bool v4only, bool v6only = false) const;
void UpdateCapsProperty (); void UpdateCapsProperty ();

Loading…
Cancel
Save