Optimize the relay map to use shared_ptr's

* Switch mapRelay to use shared_ptr<CTransaction>
* Switch the relay code to copy mempool shared_ptr's, rather than copying
  the transaction itself.
* Change vRelayExpiration to store mapRelay iterators rather than hashes
  (smaller and faster).

(cherry picked from commit bitcoin/bitcoin@dbfb426b96)
This commit is contained in:
Pieter Wuille 2016-05-31 19:51:45 +02:00 committed by Jack Grigg
parent e469547f3d
commit b9fdf44d43
1 changed files with 16 additions and 22 deletions

View File

@ -89,9 +89,6 @@ int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
std::optional<unsigned int> expiryDeltaArg = std::nullopt;
std::map<uint256, CTransaction> mapRelay;
std::deque<std::pair<int64_t, uint256> > vRelayExpiration;
CCriticalSection cs_mapRelay;
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
@ -269,6 +266,12 @@ namespace {
/** Dirty block file entries. */
set<int> setDirtyFileInfo;
/** Relay map, protected by cs_main. */
typedef std::map<uint256, std::shared_ptr<const CTransaction>> MapRelay;
MapRelay mapRelay;
/** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
} // anon namespace
//////////////////////////////////////////////////////////////////////////////
@ -6134,32 +6137,24 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
}
}
}
else if (inv.IsKnownType())
else if (inv.type == MSG_TX)
{
CTransaction tx;
// Send stream from relay memory
bool push = false;
{
LOCK(cs_mapRelay);
map<uint256, CTransaction>::iterator mi = mapRelay.find(inv.hash);
if (mi != mapRelay.end() && !IsExpiringSoonTx((*mi).second, currentHeight + 1)) {
tx = (*mi).second;
push = true;
}
}
if (!push && inv.type == MSG_TX) {
auto mi = mapRelay.find(inv.hash);
if (mi != mapRelay.end() && !IsExpiringSoonTx(*mi->second, currentHeight + 1)) {
pfrom->PushMessage("tx", *mi->second);
push = true;
} else {
auto txinfo = mempool.info(inv.hash);
// To protect privacy, do not answer getdata using the mempool when
// that TX couldn't have been INVed in reply to a MEMPOOL request.
if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq && !IsExpiringSoonTx(*txinfo.tx, currentHeight + 1)) {
tx = *txinfo.tx;
pfrom->PushMessage("tx", *txinfo.tx);
push = true;
}
}
if (push) {
pfrom->PushMessage(inv.GetCommand(), tx);
} else {
if (!push) {
vNotFound.push_back(inv);
}
}
@ -7513,7 +7508,6 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
vInv.push_back(CInv(MSG_TX, hash));
nRelayedTransactions++;
{
LOCK(cs_mapRelay);
// Expire old relay messages
while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
{
@ -7521,9 +7515,9 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
vRelayExpiration.pop_front();
}
auto ret = mapRelay.insert(std::make_pair(hash, *txinfo.tx));
auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
if (ret.second) {
vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, hash));
vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, ret.first));
}
}
if (vInv.size() == MAX_INV_SZ) {