aboutsummaryrefslogtreecommitdiff
path: root/src/node/blockstorage.cpp
diff options
context:
space:
mode:
authorCarl Dong <contact@carldong.me>2022-01-13 12:37:06 -0500
committerCarl Dong <contact@carldong.me>2022-02-22 11:56:49 -0500
commitdd79dad17545424d145e846026518d70da594380 (patch)
treeaad844f3d95c773387fdecceae3f9ddbad00cc0d /src/node/blockstorage.cpp
parent531dce034718523967808a89c18ba69a1e3e5a1f (diff)
downloadbitcoin-dd79dad17545424d145e846026518d70da594380.tar.xz
refactor: Rewrite InsertBlockIndex with try_emplace
Credit to ajtowns for this suggestion, thanks!
Diffstat (limited to 'src/node/blockstorage.cpp')
-rw-r--r--src/node/blockstorage.cpp18
1 files changed, 6 insertions, 12 deletions
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(