Skip to content

Commit

Permalink
Prefer emplace to insert in CTxMemPool::addUnchecked for map insertion
Browse files Browse the repository at this point in the history
Summary
---

This is a small code quality & performance nit.

By using `emplace` instead of `insert`, we avoid the potential creation
of temporary objects (the `std::pair`) on the stack, and instead we constructs
the `std::pair` in-place by passing arguments directly to the final allocated
`std::pair` that lives in the map node.

Test Plan
---

No semantic or behavioral changes are introduced.

- `ninja all check`
  • Loading branch information
cculianu authored and ftrader committed Apr 22, 2021
1 parent cc5d181 commit d20a0d2
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/txmempool.cpp
Expand Up @@ -32,6 +32,8 @@
#include <limits>
#include <optional>
#include <stdexcept>
#include <tuple>
#include <utility>

/// Used in various places in this file to signify "no limit" for CalculateMemPoolAncestors
inline constexpr uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
Expand Down Expand Up @@ -279,7 +281,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entryIn, const setEntries &
// Sanity check: We should always end up inserting at the end of the entry_id index
assert(&*mapTx.get<entry_id>().rbegin() == &*newit);

mapLinks.insert(make_pair(newit, TxLinks()));
mapLinks.try_emplace(newit);

// Update cachedInnerUsage to include contained transaction's usage.
// (When we update the entry for in-mempool parents, memory usage will be
Expand All @@ -289,7 +291,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entryIn, const setEntries &
const CTransaction &tx = newit->GetTx();
std::set<TxId> setParentTransactions;
for (const CTxIn &in : tx.vin) {
mapNextTx.insert(std::make_pair(&in.prevout, &tx));
mapNextTx.emplace(&in.prevout, &tx);
setParentTransactions.insert(in.prevout.GetTxId());
}
// Don't bother worrying about child transactions of this one. It is
Expand Down

0 comments on commit d20a0d2

Please sign in to comment.