Skip to content

Commit 330d3aa

Browse files
committed
refactor: net: avoid duplicate map lookups to mapLocalHost
1 parent 0ebd88f commit 330d3aa

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/net.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
190190
static int GetnScore(const CService& addr)
191191
{
192192
LOCK(cs_mapLocalHost);
193-
if (mapLocalHost.count(addr) == 0) return 0;
194-
return mapLocalHost[addr].nScore;
193+
const auto it = mapLocalHost.find(addr);
194+
return (it != mapLocalHost.end()) ? it->second.nScore : 0;
195195
}
196196

197197
// Is our peer's addrLocal potentially useful as an external IP source?
@@ -243,10 +243,10 @@ bool AddLocal(const CService& addr, int nScore)
243243

244244
{
245245
LOCK(cs_mapLocalHost);
246-
bool fAlready = mapLocalHost.count(addr) > 0;
247-
LocalServiceInfo &info = mapLocalHost[addr];
248-
if (!fAlready || nScore >= info.nScore) {
249-
info.nScore = nScore + (fAlready ? 1 : 0);
246+
const auto [it, is_newly_added] = mapLocalHost.emplace(addr, LocalServiceInfo());
247+
LocalServiceInfo &info = it->second;
248+
if (is_newly_added || nScore >= info.nScore) {
249+
info.nScore = nScore + (is_newly_added ? 0 : 1);
250250
info.nPort = addr.GetPort();
251251
}
252252
}
@@ -288,12 +288,10 @@ bool IsReachable(const CNetAddr &addr)
288288
/** vote for a local address */
289289
bool SeenLocal(const CService& addr)
290290
{
291-
{
292-
LOCK(cs_mapLocalHost);
293-
if (mapLocalHost.count(addr) == 0)
294-
return false;
295-
mapLocalHost[addr].nScore++;
296-
}
291+
LOCK(cs_mapLocalHost);
292+
const auto it = mapLocalHost.find(addr);
293+
if (it == mapLocalHost.end()) return false;
294+
++it->second.nScore;
297295
return true;
298296
}
299297

0 commit comments

Comments
 (0)