Skip to content

Commit 89740ed

Browse files
sipaFuzzbawls
authored andcommitted
Use ring buffer of set iterators instead of deque of copies in mruset
1 parent 14c88ee commit 89740ed

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/mruset.h

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// Copyright (c) 2012-2014 The Bitcoin developers
2-
// Distributed under the MIT/X11 software license, see the accompanying
1+
// Copyright (c) 2012-2015 The Bitcoin developers
2+
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#ifndef BITCOIN_MRUSET_H
66
#define BITCOIN_MRUSET_H
77

8-
#include <deque>
98
#include <set>
109
#include <utility>
10+
#include <vector>
1111

1212
/** STL-like set container that only keeps the most recent N elements. */
1313
template <typename T>
@@ -22,11 +22,13 @@ class mruset
2222

2323
protected:
2424
std::set<T> set;
25-
std::deque<T> queue;
26-
size_type nMaxSize;
25+
std::vector<iterator> order;
26+
size_type first_used;
27+
size_type first_unused;
28+
const size_type nMaxSize;
2729

2830
public:
29-
mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; }
31+
mruset(size_type nMaxSizeIn = 1) : nMaxSize(nMaxSizeIn) { clear(); }
3032
iterator begin() const { return set.begin(); }
3133
iterator end() const { return set.end(); }
3234
size_type size() const { return set.size(); }
@@ -36,7 +38,9 @@ class mruset
3638
void clear()
3739
{
3840
set.clear();
39-
queue.clear();
41+
order.assign(nMaxSize, set.end());
42+
first_used = 0;
43+
first_unused = 0;
4044
}
4145
bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
4246
bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
@@ -45,25 +49,17 @@ class mruset
4549
{
4650
std::pair<iterator, bool> ret = set.insert(x);
4751
if (ret.second) {
48-
if (nMaxSize && queue.size() == nMaxSize) {
49-
set.erase(queue.front());
50-
queue.pop_front();
52+
if (set.size() == nMaxSize + 1) {
53+
set.erase(order[first_used]);
54+
order[first_used] = set.end();
55+
if (++first_used == nMaxSize) first_used = 0;
5156
}
52-
queue.push_back(x);
57+
order[first_unused] = ret.first;
58+
if (++first_unused == nMaxSize) first_unused = 0;
5359
}
5460
return ret;
5561
}
5662
size_type max_size() const { return nMaxSize; }
57-
size_type max_size(size_type s)
58-
{
59-
if (s)
60-
while (queue.size() > s) {
61-
set.erase(queue.front());
62-
queue.pop_front();
63-
}
64-
nMaxSize = s;
65-
return nMaxSize;
66-
}
6763
};
6864

6965
#endif // BITCOIN_MRUSET_H

src/net.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,7 +2319,10 @@ bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
23192319
unsigned int ReceiveFloodSize() { return 1000 * GetArg("-maxreceivebuffer", 5 * 1000); }
23202320
unsigned int SendBufferSize() { return 1000 * GetArg("-maxsendbuffer", 1 * 1000); }
23212321

2322-
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), addrKnown(5000, 0.001, insecure_rand())
2322+
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) :
2323+
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
2324+
addrKnown(5000, 0.001, 0),
2325+
setInventoryKnown(SendBufferSize() / 1000)
23232326
{
23242327
nServices = NODE_NONE;
23252328
nServicesExpected = NODE_NONE;
@@ -2349,7 +2352,6 @@ CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fIn
23492352
nStartingHeight = -1;
23502353
fGetAddr = false;
23512354
fRelayTxes = false;
2352-
setInventoryKnown.max_size(SendBufferSize() / 1000);
23532355
pfilter = new CBloomFilter();
23542356
nPingNonceSent = 0;
23552357
nPingUsecStart = 0;

src/test/mruset_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class mrutester
2424
std::set<int> set;
2525

2626
public:
27-
mrutester() { mru.max_size(MAX_SIZE); }
27+
mrutester() : mru(MAX_SIZE) {}
2828
int size() const { return set.size(); }
2929

3030
void insert(int n)

0 commit comments

Comments
 (0)