mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
Reduce cache lookups in CCoinsViewCache::FetchCoin
Enhanced efficiency and readability of CCoinsViewCache::FetchCoin by replacing separate find() and emplace() calls with a single try_emplace(), reducing map lookups and potential insertions.
This commit is contained in:
parent
27a770b34b
commit
204ca67bba
1 changed files with 11 additions and 12 deletions
|
@ -43,19 +43,18 @@ size_t CCoinsViewCache::DynamicMemoryUsage() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
|
CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
|
||||||
CCoinsMap::iterator it = cacheCoins.find(outpoint);
|
const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
|
||||||
if (it != cacheCoins.end())
|
if (inserted) {
|
||||||
return it;
|
if (!base->GetCoin(outpoint, ret->second.coin)) {
|
||||||
Coin tmp;
|
cacheCoins.erase(ret);
|
||||||
if (!base->GetCoin(outpoint, tmp))
|
return cacheCoins.end();
|
||||||
return cacheCoins.end();
|
}
|
||||||
CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
|
if (ret->second.coin.IsSpent()) {
|
||||||
if (ret->second.coin.IsSpent()) {
|
// The parent only has an empty entry for this outpoint; we can consider our version as fresh.
|
||||||
// The parent only has an empty entry for this outpoint; we can consider our
|
ret->second.AddFlags(CCoinsCacheEntry::FRESH, *ret, m_sentinel);
|
||||||
// version as fresh.
|
}
|
||||||
ret->second.AddFlags(CCoinsCacheEntry::FRESH, *ret, m_sentinel);
|
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
|
||||||
}
|
}
|
||||||
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue