Merge pull request #3398 from codablock/pr_speedups

A few simple/trivial optimizations
This commit is contained in:
Alexander Block 2020-04-08 22:19:14 +02:00 committed by GitHub
commit e20c63f535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 13 deletions

View File

@ -1239,9 +1239,9 @@ void CConnman::ThreadSocketHandler()
{
LOCK(cs_vNodes);
// Disconnect unused nodes
std::vector<CNode*> vNodesCopy = vNodes;
for (CNode* pnode : vNodesCopy)
for (auto it = vNodes.begin(); it != vNodes.end(); )
{
CNode* pnode = *it;
if (pnode->fDisconnect)
{
if (fLogIPs) {
@ -1253,7 +1253,7 @@ void CConnman::ThreadSocketHandler()
}
// remove from vNodes
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
it = vNodes.erase(it);
// release outbound grant (if any)
pnode->grantOutbound.Release();
@ -1264,17 +1264,20 @@ void CConnman::ThreadSocketHandler()
// hold in disconnected pool until all refs are released
pnode->Release();
vNodesDisconnected.push_back(pnode);
} else {
++it;
}
}
}
{
// Delete disconnected nodes
std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
for (CNode* pnode : vNodesDisconnectedCopy)
for (auto it = vNodesDisconnected.begin(); it != vNodesDisconnected.end(); )
{
CNode* pnode = *it;
// wait until threads are done using it
if (pnode->GetRefCount() <= 0) {
bool fDelete = false;
if (pnode->GetRefCount() <= 0) {
{
TRY_LOCK(pnode->cs_inventory, lockInv);
if (lockInv) {
@ -1285,10 +1288,13 @@ void CConnman::ThreadSocketHandler()
}
}
if (fDelete) {
vNodesDisconnected.remove(pnode);
it = vNodesDisconnected.erase(it);
DeleteNode(pnode);
}
}
if (!fDelete) {
++it;
}
}
}
size_t vNodesSize;
@ -3412,6 +3418,7 @@ std::vector<CNode*> CConnman::CopyNodeVector(std::function<bool(const CNode* pno
{
std::vector<CNode*> vecNodesCopy;
LOCK(cs_vNodes);
vecNodesCopy.reserve(vNodes.size());
for(size_t i = 0; i < vNodes.size(); ++i) {
CNode* pnode = vNodes[i];
if (!cond(pnode))
@ -3429,7 +3436,6 @@ std::vector<CNode*> CConnman::CopyNodeVector()
void CConnman::ReleaseNodeVector(const std::vector<CNode*>& vecNodes)
{
LOCK(cs_vNodes);
for(size_t i = 0; i < vecNodes.size(); ++i) {
CNode* pnode = vecNodes[i];
pnode->Release();

View File

@ -766,7 +766,7 @@ public:
size_t nSendSize; // total size of all vSendMsg entries
size_t nSendOffset; // offset inside the first vSendMsg already sent
uint64_t nSendBytes;
std::deque<std::vector<unsigned char>> vSendMsg;
std::list<std::vector<unsigned char>> vSendMsg;
CCriticalSection cs_vSend;
CCriticalSection cs_hSocket;
CCriticalSection cs_vRecv;

View File

@ -3812,8 +3812,12 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
//
std::vector<CInv> vInv;
{
size_t reserve = std::min<size_t>(pto->setInventoryTxToSend.size(), INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK * MaxBlockSize(true) / 1000000);
reserve = std::max<size_t>(reserve, pto->vInventoryBlockToSend.size());
reserve = std::min<size_t>(reserve, MAX_INV_SZ);
vInv.reserve(reserve);
LOCK(pto->cs_inventory);
vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK * MaxBlockSize(true) / 1000000));
// Add blocks
for (const uint256& hash : pto->vInventoryBlockToSend) {

View File

@ -15,6 +15,8 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <chrono>
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
int64_t GetTime()
@ -39,16 +41,14 @@ int64_t GetMockTime()
int64_t GetTimeMillis()
{
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
int64_t now = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
assert(now > 0);
return now;
}
int64_t GetTimeMicros()
{
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
int64_t now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
assert(now > 0);
return now;
}