aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
authorSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-09-06 00:10:31 +0200
committerSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-09-14 18:24:58 +0200
commit330d3aa1a2c740dfa31bed3a6ed6b5f88e5426ad (patch)
tree051962115aa4f1d5841d36e911ce1f7aa0353afb /src/net.cpp
parent0ebd88fe0bf45d872883b4d361147f5c047b1a46 (diff)
downloadbitcoin-330d3aa1a2c740dfa31bed3a6ed6b5f88e5426ad.tar.xz
refactor: net: avoid duplicate map lookups to `mapLocalHost`
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 9b1e17c587..cbd09b770e 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -190,8 +190,8 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
static int GetnScore(const CService& addr)
{
LOCK(cs_mapLocalHost);
- if (mapLocalHost.count(addr) == 0) return 0;
- return mapLocalHost[addr].nScore;
+ const auto it = mapLocalHost.find(addr);
+ return (it != mapLocalHost.end()) ? it->second.nScore : 0;
}
// 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);
- bool fAlready = mapLocalHost.count(addr) > 0;
- LocalServiceInfo &info = mapLocalHost[addr];
- if (!fAlready || nScore >= info.nScore) {
- info.nScore = nScore + (fAlready ? 1 : 0);
+ const auto [it, is_newly_added] = mapLocalHost.emplace(addr, LocalServiceInfo());
+ LocalServiceInfo &info = it->second;
+ if (is_newly_added || nScore >= info.nScore) {
+ info.nScore = nScore + (is_newly_added ? 0 : 1);
info.nPort = addr.GetPort();
}
}
@@ -288,12 +288,10 @@ bool IsReachable(const CNetAddr &addr)
/** vote for a local address */
bool SeenLocal(const CService& addr)
{
- {
- LOCK(cs_mapLocalHost);
- if (mapLocalHost.count(addr) == 0)
- return false;
- mapLocalHost[addr].nScore++;
- }
+ LOCK(cs_mapLocalHost);
+ const auto it = mapLocalHost.find(addr);
+ if (it == mapLocalHost.end()) return false;
+ ++it->second.nScore;
return true;
}