Skip to content

Commit d4d5022

Browse files
committed
Use ring buffer of set iterators instead of deque of copies in mruset
1 parent d81cff3 commit d4d5022

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/mruset.h

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// Copyright (c) 2012 The Bitcoin Core developers
1+
// Copyright (c) 2012-2015 The Bitcoin Core developers
22
// 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>
9+
#include <vector>
1010
#include <utility>
1111

1212
/** STL-like set container that only keeps the most recent N elements. */
@@ -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
@@ -1905,7 +1905,10 @@ bool CAddrDB::Read(CAddrMan& addr)
19051905
unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
19061906
unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
19071907

1908-
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), addrKnown(5000, 0.001, insecure_rand())
1908+
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) :
1909+
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
1910+
addrKnown(5000, 0.001, insecure_rand()),
1911+
setInventoryKnown(SendBufferSize() / 1000)
19091912
{
19101913
nServices = 0;
19111914
hSocket = hSocketIn;
@@ -1934,7 +1937,6 @@ CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fIn
19341937
nStartingHeight = -1;
19351938
fGetAddr = false;
19361939
fRelayTxes = false;
1937-
setInventoryKnown.max_size(SendBufferSize() / 1000);
19381940
pfilter = new CBloomFilter();
19391941
nPingNonceSent = 0;
19401942
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)