From d20a0d27bbe8effee024bf255c8b1485d7f1554e Mon Sep 17 00:00:00 2001 From: Calin Culianu Date: Thu, 22 Apr 2021 19:58:41 +0000 Subject: [PATCH] Prefer emplace to insert in CTxMemPool::addUnchecked for map insertion 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` --- src/txmempool.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/txmempool.cpp b/src/txmempool.cpp index c67a6f16d1..e916bdd769 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include /// Used in various places in this file to signify "no limit" for CalculateMemPoolAncestors inline constexpr uint64_t nNoLimit = std::numeric_limits::max(); @@ -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().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 @@ -289,7 +291,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entryIn, const setEntries & const CTransaction &tx = newit->GetTx(); std::set 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