Browse Source

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).
0.13
Pieter Wuille 9 years ago
parent
commit
dbfb426b96
  1. 33
      src/main.cpp

33
src/main.cpp

@ -80,9 +80,6 @@ uint64_t nPruneTarget = 0;
int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE; int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT; bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
std::map<uint256, CTransaction> mapRelay;
std::deque<std::pair<int64_t, uint256> > vRelayExpiration;
CCriticalSection cs_mapRelay;
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE); CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE; CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
@ -215,6 +212,12 @@ namespace {
/** Number of peers from which we're downloading blocks. */ /** Number of peers from which we're downloading blocks. */
int nPeersWithValidatedDownloads = 0; int nPeersWithValidatedDownloads = 0;
/** 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 } // anon namespace
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -4505,31 +4508,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 // Send stream from relay memory
bool push = false; bool push = false;
{ auto mi = mapRelay.find(inv.hash);
LOCK(cs_mapRelay);
map<uint256, CTransaction>::iterator mi = mapRelay.find(inv.hash);
if (mi != mapRelay.end()) { if (mi != mapRelay.end()) {
tx = (*mi).second; pfrom->PushMessage(NetMsgType::TX, *mi->second);
push = true; push = true;
} } else {
}
if (!push && inv.type == MSG_TX) {
auto txinfo = mempool.info(inv.hash); auto txinfo = mempool.info(inv.hash);
// To protect privacy, do not answer getdata using the mempool when // To protect privacy, do not answer getdata using the mempool when
// that TX couldn't have been INVed in reply to a MEMPOOL request. // that TX couldn't have been INVed in reply to a MEMPOOL request.
if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) { if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
tx = *txinfo.tx; pfrom->PushMessage(NetMsgType::TX, *txinfo.tx);
push = true; push = true;
} }
} }
if (push) { if (!push) {
pfrom->PushMessage(inv.GetCommand(), tx);
} else {
vNotFound.push_back(inv); vNotFound.push_back(inv);
} }
} }
@ -5978,7 +5974,6 @@ bool SendMessages(CNode* pto)
vInv.push_back(CInv(MSG_TX, hash)); vInv.push_back(CInv(MSG_TX, hash));
nRelayedTransactions++; nRelayedTransactions++;
{ {
LOCK(cs_mapRelay);
// Expire old relay messages // Expire old relay messages
while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime()) while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
{ {
@ -5986,9 +5981,9 @@ bool SendMessages(CNode* pto)
vRelayExpiration.pop_front(); 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) { 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) { if (vInv.size() == MAX_INV_SZ) {

Loading…
Cancel
Save