diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -197,10 +197,8 @@ 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? @@ -255,10 +253,11 @@ { 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; info.nPort = addr.GetPort(); } } @@ -296,10 +295,11 @@ /** vote for a local address */ bool SeenLocal(const CService &addr) { LOCK(cs_mapLocalHost); - if (mapLocalHost.count(addr) == 0) { + const auto it = mapLocalHost.find(addr); + if (it == mapLocalHost.end()) { return false; } - mapLocalHost[addr].nScore++; + ++it->second.nScore; return true; }