Browse Source

Use ring buffer of set iterators instead of deque of copies in mruset

0.13
Pieter Wuille 10 years ago
parent
commit
d4d5022cfc
  1. 36
      src/mruset.h
  2. 6
      src/net.cpp
  3. 2
      src/test/mruset_tests.cpp

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 // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_MRUSET_H #ifndef BITCOIN_MRUSET_H
#define BITCOIN_MRUSET_H #define BITCOIN_MRUSET_H
#include <deque>
#include <set> #include <set>
#include <vector>
#include <utility> #include <utility>
/** STL-like set container that only keeps the most recent N elements. */ /** STL-like set container that only keeps the most recent N elements. */
@ -22,11 +22,13 @@ public:
protected: protected:
std::set<T> set; std::set<T> set;
std::deque<T> queue; std::vector<iterator> order;
size_type nMaxSize; size_type first_used;
size_type first_unused;
const size_type nMaxSize;
public: public:
mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; } mruset(size_type nMaxSizeIn = 1) : nMaxSize(nMaxSizeIn) { clear(); }
iterator begin() const { return set.begin(); } iterator begin() const { return set.begin(); }
iterator end() const { return set.end(); } iterator end() const { return set.end(); }
size_type size() const { return set.size(); } size_type size() const { return set.size(); }
@ -36,7 +38,9 @@ public:
void clear() void clear()
{ {
set.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 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; } 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); std::pair<iterator, bool> ret = set.insert(x);
if (ret.second) { if (ret.second) {
if (nMaxSize && queue.size() == nMaxSize) { if (set.size() == nMaxSize + 1) {
set.erase(queue.front()); set.erase(order[first_used]);
queue.pop_front(); 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; return ret;
} }
size_type max_size() const { return nMaxSize; } 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 #endif // BITCOIN_MRUSET_H

6
src/net.cpp

@ -1905,7 +1905,10 @@ bool CAddrDB::Read(CAddrMan& addr)
unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); } unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*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; nServices = 0;
hSocket = hSocketIn; hSocket = hSocketIn;
@ -1934,7 +1937,6 @@ CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fIn
nStartingHeight = -1; nStartingHeight = -1;
fGetAddr = false; fGetAddr = false;
fRelayTxes = false; fRelayTxes = false;
setInventoryKnown.max_size(SendBufferSize() / 1000);
pfilter = new CBloomFilter(); pfilter = new CBloomFilter();
nPingNonceSent = 0; nPingNonceSent = 0;
nPingUsecStart = 0; nPingUsecStart = 0;

2
src/test/mruset_tests.cpp

@ -24,7 +24,7 @@ private:
std::set<int> set; std::set<int> set;
public: public:
mrutester() { mru.max_size(MAX_SIZE); } mrutester() : mru(MAX_SIZE) {}
int size() const { return set.size(); } int size() const { return set.size(); }
void insert(int n) void insert(int n)

Loading…
Cancel
Save