From 806fd19ecba0d58bf238a7b6f78e080da03eb9c7 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 21 Jun 2014 17:00:38 +0200 Subject: [PATCH] Allocate receive buffers in on the fly --- src/net.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/net.cpp b/src/net.cpp index 757a06aae..707f58f8a 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -678,7 +678,6 @@ int CNetMessage::readHeader(const char *pch, unsigned int nBytes) // switch state to reading message data in_data = true; - vRecv.resize(hdr.nMessageSize); return nCopy; } @@ -688,6 +687,11 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes) unsigned int nRemaining = hdr.nMessageSize - nDataPos; unsigned int nCopy = std::min(nRemaining, nBytes); + if (vRecv.size() < nDataPos + nCopy) { + // Allocate up to 256 KiB ahead, but never more than the total message size. + vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024)); + } + memcpy(&vRecv[nDataPos], pch, nCopy); nDataPos += nCopy;