From 94642f90666feacb5d49bb5e423e40d2e1b7cb50 Mon Sep 17 00:00:00 2001 From: brain5lug Date: Wed, 10 Aug 2016 01:16:24 +0300 Subject: [PATCH] copy elimination for ranges #part4 --- Tunnel.cpp | 15 +++++++-------- TunnelGateway.cpp | 2 +- TunnelPool.cpp | 29 ++++++++++++++--------------- UPnP.cpp | 8 ++++---- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Tunnel.cpp b/Tunnel.cpp index fd971628..6a6accd6 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -345,7 +345,7 @@ namespace tunnel { std::shared_ptr tunnel; size_t minReceived = 0; - for (auto it : m_InboundTunnels) + for (const auto& it : m_InboundTunnels) { if (!it->IsEstablished ()) continue; if (!tunnel || it->GetNumReceivedBytes () < minReceived) @@ -362,7 +362,7 @@ namespace tunnel if (m_OutboundTunnels.empty ()) return nullptr; uint32_t ind = rand () % m_OutboundTunnels.size (), i = 0; std::shared_ptr tunnel; - for (auto it: m_OutboundTunnels) + for (const auto& it: m_OutboundTunnels) { if (it->IsEstablished ()) { @@ -586,7 +586,7 @@ namespace tunnel m_NumFailedTunnelCreations++; } else - it++; + ++it; break; case eTunnelStateBuildFailed: LogPrint (eLogDebug, "Tunnel: pending build request ", it->first, " failed, deleted"); @@ -595,7 +595,7 @@ namespace tunnel break; case eTunnelStateBuildReplyReceived: // intermediate state, will be either established of build failed - it++; + ++it; break; default: // success @@ -635,7 +635,7 @@ namespace tunnel if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT) tunnel->SetState (eTunnelStateExpiring); } - it++; + ++it; } } } @@ -738,9 +738,8 @@ namespace tunnel void Tunnels::ManageTunnelPools () { std::unique_lock l(m_PoolsMutex); - for (auto it: m_Pools) + for (auto& pool : m_Pools) { - auto pool = it; if (pool && pool->IsActive ()) { pool->CreateTunnels (); @@ -856,7 +855,7 @@ namespace tunnel int timeout = 0; uint32_t ts = i2p::util::GetSecondsSinceEpoch (); // TODO: possible race condition with I2PControl - for (auto it: m_TransitTunnels) + for (const auto& it : m_TransitTunnels) { int t = it->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT - ts; if (t > timeout) timeout = t; diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index d701e24b..ad423fc0 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -197,7 +197,7 @@ namespace tunnel { m_Buffer.CompleteCurrentTunnelDataMessage (); auto tunnelMsgs = m_Buffer.GetTunnelDataMsgs (); - for (auto tunnelMsg : tunnelMsgs) + for (auto& tunnelMsg : tunnelMsgs) { m_Tunnel->EncryptTunnelMsg (tunnelMsg, tunnelMsg); tunnelMsg->FillI2NPMessageHeader (eI2NPTunnelData); diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 4bd116cb..119556aa 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -49,13 +49,13 @@ namespace tunnel { { std::unique_lock l(m_InboundTunnelsMutex); - for (auto it: m_InboundTunnels) + for (auto& it: m_InboundTunnels) it->SetTunnelPool (nullptr); m_InboundTunnels.clear (); } { std::unique_lock l(m_OutboundTunnelsMutex); - for (auto it: m_OutboundTunnels) + for (auto& it: m_OutboundTunnels) it->SetTunnelPool (nullptr); m_OutboundTunnels.clear (); } @@ -78,7 +78,7 @@ namespace tunnel if (expiredTunnel) { expiredTunnel->SetTunnelPool (nullptr); - for (auto it: m_Tests) + for (auto& it: m_Tests) if (it.second.second == expiredTunnel) it.second.second = nullptr; std::unique_lock l(m_InboundTunnelsMutex); @@ -101,7 +101,7 @@ namespace tunnel if (expiredTunnel) { expiredTunnel->SetTunnelPool (nullptr); - for (auto it: m_Tests) + for (auto& it: m_Tests) if (it.second.first == expiredTunnel) it.second.first = nullptr; std::unique_lock l(m_OutboundTunnelsMutex); @@ -114,7 +114,7 @@ namespace tunnel std::vector > v; int i = 0; std::unique_lock l(m_InboundTunnelsMutex); - for (auto it : m_InboundTunnels) + for (const auto& it : m_InboundTunnels) { if (i >= num) break; if (it->IsEstablished ()) @@ -144,7 +144,7 @@ namespace tunnel if (tunnels.empty ()) return nullptr; uint32_t ind = rand () % (tunnels.size ()/2 + 1), i = 0; typename TTunnels::value_type tunnel = nullptr; - for (auto it: tunnels) + for (const auto& it: tunnels) { if (it->IsEstablished () && it != excluded) { @@ -164,7 +164,7 @@ namespace tunnel if (old) { std::unique_lock l(m_OutboundTunnelsMutex); - for (auto it: m_OutboundTunnels) + for (const auto& it: m_OutboundTunnels) if (it->IsEstablished () && old->GetEndpointIdentHash () == it->GetEndpointIdentHash ()) { tunnel = it; @@ -182,7 +182,7 @@ namespace tunnel int num = 0; { std::unique_lock l(m_InboundTunnelsMutex); - for (auto it : m_InboundTunnels) + for (const auto& it : m_InboundTunnels) if (it->IsEstablished ()) num++; } for (int i = num; i < m_NumInboundTunnels; i++) @@ -191,7 +191,7 @@ namespace tunnel num = 0; { std::unique_lock l(m_OutboundTunnelsMutex); - for (auto it : m_OutboundTunnels) + for (const auto& it : m_OutboundTunnels) if (it->IsEstablished ()) num++; } for (int i = num; i < m_NumOutboundTunnels; i++) @@ -203,11 +203,10 @@ namespace tunnel decltype(m_Tests) tests; { std::unique_lock l(m_TestsMutex); - tests = m_Tests; - m_Tests.clear (); + tests.swap(m_Tests); } - for (auto it: tests) + for (auto& it: tests) { LogPrint (eLogWarning, "Tunnels: test of tunnel ", it.first, " failed"); // if test failed again with another tunnel we consider it failed @@ -248,12 +247,12 @@ namespace tunnel if ((*it1)->IsFailed ()) { failed = true; - it1++; + ++it1; } if ((*it2)->IsFailed ()) { failed = true; - it2++; + ++it2; } if (!failed) { @@ -265,7 +264,7 @@ namespace tunnel } (*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (), CreateDeliveryStatusMsg (msgID)); - it1++; it2++; + ++it1; ++it2; } } } diff --git a/UPnP.cpp b/UPnP.cpp index db0d3683..612b1441 100644 --- a/UPnP.cpp +++ b/UPnP.cpp @@ -122,8 +122,8 @@ namespace transport void UPnP::PortMapping () { - auto a = context.GetRouterInfo().GetAddresses(); - for (auto address : a) + const auto& a = context.GetRouterInfo().GetAddresses(); + for (const auto& address : a) { if (!address->host.is_v6 ()) TryPortMapping (address); @@ -139,8 +139,8 @@ namespace transport void UPnP::CloseMapping () { - auto a = context.GetRouterInfo().GetAddresses(); - for (auto address : a) + const auto& a = context.GetRouterInfo().GetAddresses(); + for (const auto& address : a) { if (!address->host.is_v6 ()) CloseMapping (address);