copy elimination for ranges #part2

This commit is contained in:
brain5lug 2016-08-08 00:52:18 +03:00
parent d5075d706c
commit 8b53ded53a
4 changed files with 39 additions and 39 deletions

View File

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

View File

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

View File

@ -369,19 +369,19 @@ namespace data
SetProperty ("caps", caps);
}
void RouterInfo::WriteToStream (std::ostream& s)
void RouterInfo::WriteToStream (std::ostream& s) const
{
uint64_t ts = htobe64 (m_Timestamp);
s.write ((char *)&ts, sizeof (ts));
s.write ((const char *)&ts, sizeof (ts));
// addresses
uint8_t numAddresses = m_Addresses->size ();
s.write ((char *)&numAddresses, sizeof (numAddresses));
for (auto addr : *m_Addresses)
for (const auto& addr_ptr : *m_Addresses)
{
Address& address = *addr;
s.write ((char *)&address.cost, sizeof (address.cost));
s.write ((char *)&address.date, sizeof (address.date));
const Address& address = *addr_ptr;
s.write ((const char *)&address.cost, sizeof (address.cost));
s.write ((const char *)&address.date, sizeof (address.date));
std::stringstream properties;
if (address.transportStyle == eTransportNTCP)
WriteString ("NTCP", s);
@ -410,7 +410,7 @@ namespace data
if (address.introducers.size () > 0)
{
int i = 0;
for (auto introducer: address.introducers)
for (const auto& introducer: address.introducers)
{
WriteString ("ihost" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
@ -419,7 +419,7 @@ namespace data
i++;
}
i = 0;
for (auto introducer: address.introducers)
for (const auto& introducer: address.introducers)
{
WriteString ("ikey" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
@ -431,7 +431,7 @@ namespace data
i++;
}
i = 0;
for (auto introducer: address.introducers)
for (const auto& introducer: address.introducers)
{
WriteString ("iport" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
@ -440,7 +440,7 @@ namespace data
i++;
}
i = 0;
for (auto introducer: address.introducers)
for (const auto& introducer: address.introducers)
{
WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
@ -482,7 +482,7 @@ namespace data
// properties
std::stringstream properties;
for (auto& p : m_Properties)
for (const auto& p : m_Properties)
{
WriteString (p.first, properties);
properties << '=';
@ -570,10 +570,10 @@ namespace data
addr->cost = 2;
addr->date = 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;
m_Addresses->push_back(addr);
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)
@ -586,21 +586,22 @@ namespace data
addr->date = 0;
addr->mtu = mtu;
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;
m_Addresses->push_back(addr);
m_SupportedTransports |= addr->host.is_v6 () ? eSSUV6 : eSSUV4;
m_Addresses->push_back(std::move(addr));
m_Caps |= eSSUTesting;
m_Caps |= eSSUIntroducer;
}
bool RouterInfo::AddIntroducer (const Introducer& introducer)
{
for (auto addr : *m_Addresses)
for (auto& addr : *m_Addresses)
{
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
addr->introducers.push_back (introducer);
return true;
@ -611,11 +612,11 @@ namespace data
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 ())
{
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)
{
addr->introducers.erase (it);
@ -707,7 +708,7 @@ namespace data
if (addr->host.is_v6 ())
it = m_Addresses->erase (it);
else
it++;
++it;
}
}
}
@ -723,7 +724,7 @@ namespace data
if (addr->host.is_v4 ())
it = m_Addresses->erase (it);
else
it++;
++it;
}
}
}
@ -751,8 +752,7 @@ namespace data
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const
{
auto addresses = m_Addresses;
for (auto address : *addresses)
for (const auto& address : *m_Addresses)
{
if (address->transportStyle == s)
{

View File

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