Skip to content

Commit dd79dad

Browse files
committed
refactor: Rewrite InsertBlockIndex with try_emplace
Credit to ajtowns for this suggestion, thanks!
1 parent 531dce0 commit dd79dad

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/node/blockstorage.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,13 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
208208
return nullptr;
209209
}
210210

211-
// Return existing
212-
BlockMap::iterator mi = m_block_index.find(hash);
213-
if (mi != m_block_index.end()) {
214-
return &(*mi).second;
211+
// Return existing or create new
212+
auto [mi, inserted] = m_block_index.try_emplace(hash);
213+
CBlockIndex* pindex = &(*mi).second;
214+
if (inserted) {
215+
pindex->phashBlock = &((*mi).first);
215216
}
216-
217-
// Create new
218-
CBlockIndex new_index{};
219-
mi = m_block_index.insert(std::make_pair(hash, std::move(new_index))).first;
220-
CBlockIndex* pindexNew = &(*mi).second;
221-
pindexNew->phashBlock = &((*mi).first);
222-
223-
return pindexNew;
217+
return pindex;
224218
}
225219

226220
bool BlockManager::LoadBlockIndex(

0 commit comments

Comments
 (0)