From 201f8eea1af7d2dc957257dd33c87938ba00ee93 Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Tue, 7 Apr 2020 06:47:16 +0200 Subject: [PATCH] Optimize vInv.reserve in SendMessages 1. Don't call it while holding cs_invetory 2. Also take setInventoryTxToSend.size() into account --- src/net_processing.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index bcf4521ea3..4e0d7322cb 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3812,8 +3812,12 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic& interruptM // std::vector vInv; { + size_t reserve = std::min(pto->setInventoryTxToSend.size(), INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK * MaxBlockSize(true) / 1000000); + reserve = std::max(reserve, pto->vInventoryBlockToSend.size()); + reserve = std::min(reserve, MAX_INV_SZ); + vInv.reserve(reserve); + LOCK(pto->cs_inventory); - vInv.reserve(std::max(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK * MaxBlockSize(true) / 1000000)); // Add blocks for (const uint256& hash : pto->vInventoryBlockToSend) {