mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-31 13:01:32 +02:00
refactor: net: avoid duplicate map lookups to mapLocalHost
This commit is contained in:
20
src/net.cpp
20
src/net.cpp
@@ -190,8 +190,8 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
|
|||||||
static int GetnScore(const CService& addr)
|
static int GetnScore(const CService& addr)
|
||||||
{
|
{
|
||||||
LOCK(cs_mapLocalHost);
|
LOCK(cs_mapLocalHost);
|
||||||
if (mapLocalHost.count(addr) == 0) return 0;
|
const auto it = mapLocalHost.find(addr);
|
||||||
return mapLocalHost[addr].nScore;
|
return (it != mapLocalHost.end()) ? it->second.nScore : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is our peer's addrLocal potentially useful as an external IP source?
|
// Is our peer's addrLocal potentially useful as an external IP source?
|
||||||
@@ -243,10 +243,10 @@ bool AddLocal(const CService& addr, int nScore)
|
|||||||
|
|
||||||
{
|
{
|
||||||
LOCK(cs_mapLocalHost);
|
LOCK(cs_mapLocalHost);
|
||||||
bool fAlready = mapLocalHost.count(addr) > 0;
|
const auto [it, is_newly_added] = mapLocalHost.emplace(addr, LocalServiceInfo());
|
||||||
LocalServiceInfo &info = mapLocalHost[addr];
|
LocalServiceInfo &info = it->second;
|
||||||
if (!fAlready || nScore >= info.nScore) {
|
if (is_newly_added || nScore >= info.nScore) {
|
||||||
info.nScore = nScore + (fAlready ? 1 : 0);
|
info.nScore = nScore + (is_newly_added ? 0 : 1);
|
||||||
info.nPort = addr.GetPort();
|
info.nPort = addr.GetPort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -287,13 +287,11 @@ bool IsReachable(const CNetAddr &addr)
|
|||||||
|
|
||||||
/** vote for a local address */
|
/** vote for a local address */
|
||||||
bool SeenLocal(const CService& addr)
|
bool SeenLocal(const CService& addr)
|
||||||
{
|
|
||||||
{
|
{
|
||||||
LOCK(cs_mapLocalHost);
|
LOCK(cs_mapLocalHost);
|
||||||
if (mapLocalHost.count(addr) == 0)
|
const auto it = mapLocalHost.find(addr);
|
||||||
return false;
|
if (it == mapLocalHost.end()) return false;
|
||||||
mapLocalHost[addr].nScore++;
|
++it->second.nScore;
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user