perf: NodesSnapshot, do not hold m_nodes_mutex while shuffling

This commit is contained in:
pasta 2024-12-09 14:40:36 -06:00
parent 7c00c868d8
commit c005011e84
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38

View File

@ -5118,16 +5118,16 @@ bool CConnman::IsMasternodeOrDisconnectRequested(const CService& addr) {
CConnman::NodesSnapshot::NodesSnapshot(const CConnman& connman, std::function<bool(const CNode* pnode)> filter, CConnman::NodesSnapshot::NodesSnapshot(const CConnman& connman, std::function<bool(const CNode* pnode)> filter,
bool shuffle) bool shuffle)
{ {
{
LOCK(connman.m_nodes_mutex); LOCK(connman.m_nodes_mutex);
m_nodes_copy.reserve(connman.m_nodes.size()); m_nodes_copy.reserve(connman.m_nodes.size());
for (auto& node : connman.m_nodes) { for (auto& node : connman.m_nodes) {
if (!filter(node)) if (!filter(node)) continue;
continue;
node->AddRef(); node->AddRef();
m_nodes_copy.push_back(node); m_nodes_copy.push_back(node);
} }
}
if (shuffle) { if (shuffle) {
Shuffle(m_nodes_copy.begin(), m_nodes_copy.end(), FastRandomContext{}); Shuffle(m_nodes_copy.begin(), m_nodes_copy.end(), FastRandomContext{});
} }