Use ring buffer of set iterators instead of deque of copies in mruset
This commit is contained in:
parent
d81cff32e5
commit
d4d5022cfc
3 changed files with 21 additions and 23 deletions
36
src/mruset.h
36
src/mruset.h
|
@ -1,12 +1,12 @@
|
|||
// Copyright (c) 2012 The Bitcoin Core developers
|
||||
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_MRUSET_H
|
||||
#define BITCOIN_MRUSET_H
|
||||
|
||||
#include <deque>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
/** STL-like set container that only keeps the most recent N elements. */
|
||||
|
@ -22,11 +22,13 @@ public:
|
|||
|
||||
protected:
|
||||
std::set<T> set;
|
||||
std::deque<T> queue;
|
||||
size_type nMaxSize;
|
||||
std::vector<iterator> order;
|
||||
size_type first_used;
|
||||
size_type first_unused;
|
||||
const size_type nMaxSize;
|
||||
|
||||
public:
|
||||
mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; }
|
||||
mruset(size_type nMaxSizeIn = 1) : nMaxSize(nMaxSizeIn) { clear(); }
|
||||
iterator begin() const { return set.begin(); }
|
||||
iterator end() const { return set.end(); }
|
||||
size_type size() const { return set.size(); }
|
||||
|
@ -36,7 +38,9 @@ public:
|
|||
void clear()
|
||||
{
|
||||
set.clear();
|
||||
queue.clear();
|
||||
order.assign(nMaxSize, set.end());
|
||||
first_used = 0;
|
||||
first_unused = 0;
|
||||
}
|
||||
bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
|
||||
bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
|
||||
|
@ -45,25 +49,17 @@ public:
|
|||
{
|
||||
std::pair<iterator, bool> ret = set.insert(x);
|
||||
if (ret.second) {
|
||||
if (nMaxSize && queue.size() == nMaxSize) {
|
||||
set.erase(queue.front());
|
||||
queue.pop_front();
|
||||
if (set.size() == nMaxSize + 1) {
|
||||
set.erase(order[first_used]);
|
||||
order[first_used] = set.end();
|
||||
if (++first_used == nMaxSize) first_used = 0;
|
||||
}
|
||||
queue.push_back(x);
|
||||
order[first_unused] = ret.first;
|
||||
if (++first_unused == nMaxSize) first_unused = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
size_type max_size() const { return nMaxSize; }
|
||||
size_type max_size(size_type s)
|
||||
{
|
||||
if (s)
|
||||
while (queue.size() > s) {
|
||||
set.erase(queue.front());
|
||||
queue.pop_front();
|
||||
}
|
||||
nMaxSize = s;
|
||||
return nMaxSize;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_MRUSET_H
|
||||
|
|
|
@ -1905,7 +1905,10 @@ bool CAddrDB::Read(CAddrMan& addr)
|
|||
unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
|
||||
unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
|
||||
|
||||
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), addrKnown(5000, 0.001, insecure_rand())
|
||||
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) :
|
||||
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
|
||||
addrKnown(5000, 0.001, insecure_rand()),
|
||||
setInventoryKnown(SendBufferSize() / 1000)
|
||||
{
|
||||
nServices = 0;
|
||||
hSocket = hSocketIn;
|
||||
|
@ -1934,7 +1937,6 @@ CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fIn
|
|||
nStartingHeight = -1;
|
||||
fGetAddr = false;
|
||||
fRelayTxes = false;
|
||||
setInventoryKnown.max_size(SendBufferSize() / 1000);
|
||||
pfilter = new CBloomFilter();
|
||||
nPingNonceSent = 0;
|
||||
nPingUsecStart = 0;
|
||||
|
|
|
@ -24,7 +24,7 @@ private:
|
|||
std::set<int> set;
|
||||
|
||||
public:
|
||||
mrutester() { mru.max_size(MAX_SIZE); }
|
||||
mrutester() : mru(MAX_SIZE) {}
|
||||
int size() const { return set.size(); }
|
||||
|
||||
void insert(int n)
|
||||
|
|
Loading…
Reference in a new issue