From dd79dad17545424d145e846026518d70da594380 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Thu, 13 Jan 2022 12:37:06 -0500 Subject: refactor: Rewrite InsertBlockIndex with try_emplace Credit to ajtowns for this suggestion, thanks! --- src/node/blockstorage.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'src/node') diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index a13c1a9956..0619af5426 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -208,19 +208,13 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash) return nullptr; } - // Return existing - BlockMap::iterator mi = m_block_index.find(hash); - if (mi != m_block_index.end()) { - return &(*mi).second; + // Return existing or create new + auto [mi, inserted] = m_block_index.try_emplace(hash); + CBlockIndex* pindex = &(*mi).second; + if (inserted) { + pindex->phashBlock = &((*mi).first); } - - // Create new - CBlockIndex new_index{}; - mi = m_block_index.insert(std::make_pair(hash, std::move(new_index))).first; - CBlockIndex* pindexNew = &(*mi).second; - pindexNew->phashBlock = &((*mi).first); - - return pindexNew; + return pindex; } bool BlockManager::LoadBlockIndex( -- cgit v1.2.3